module JosieHealth::Utils::Dosage

Defined in:

dosage.cr

Class Method Summary

Class Method Detail

def self.extract_from_text(text : String) : NamedTuple(dosage: String, unit: String, substance: String, route: String | Nil) | Nil #

Extract medication data from natural language text Pattern: [verb] [route] Examples: "50mg caffeine", "took 2g kratom oral" Returns nil if no match found


def self.format(amount : String, unit : String) : String #

Format dosage for display (amount + unit)


def self.looks_like_dosage?(text : String) : Bool #

Check if a string looks like a dosage Matches: 100mg, 2.5g, 0.5ml, 47.5mg/2, 10mg/25mg, 100, etc. This is used to detect malformed entries where dosage ended up as substance


def self.looks_like_hhmm_timestamp?(text : String) : Bool #

Check if a string looks like an HHMM timestamp (common user error) Only matches bare 4-digit numbers without units: 0005, 0010, 0216, 1544 Does NOT match doses with units like 1200mg, 1500mg (those are valid doses) Used to detect when users confuse timestamp with dosage in ;td commands


def self.parse(dosage : String) : NamedTuple(amount: String, unit: String) | Nil #

Parse a dosage string into amount and unit Supports formats: "100mg", "0.5ml", ".38ml", "100" Returns nil if invalid format


def self.parse_any(dosage : String) : NamedTuple(amount: String, unit: String) | Nil #

Parse dosage, trying math expression first, then simple format


def self.parse_math(dosage : String) : NamedTuple(amount: String, unit: String) | Nil #

Parse dosage with math expressions like "47.5mg/2" or "100mg*0.5" Returns calculated amount and unit, or nil if invalid


def self.validate_dose_substance(dosage : String, substance : String) : NamedTuple(dosage: String, substance: String, swapped: Bool) | Nil #

Validate and potentially correct swapped dosage/substance Returns corrected values or nil if can't be corrected