Fix parsing of drugs with comments.

This commit is contained in:
Daniel Kraus 2015-12-01 14:52:22 +01:00
parent 61f494f10f
commit 84e298e9ad
2 changed files with 70 additions and 20 deletions

View File

@ -49,7 +49,7 @@ namespace Tests.Medication
public void MultiplePrescriptions()
{
IEnumerable<Prescription> list = Prescription.ManyFromLine(
"Ramipril 5 mg 1-0-0 Prograf 1 mg 1-0-1");
"Ramipril 5 mg 1-0-0 \t Prograf 1 mg 1-0-1");
Assert.AreEqual(2, list.Count());
Assert.AreEqual("Ramipril 5 mg\t1-0-0", list.First().ToString());
Assert.AreEqual("Prograf 1 mg\t1-0-1", list.Last().ToString());
@ -70,5 +70,36 @@ namespace Tests.Medication
Prescription p = new Prescription(drug, morning, noon, evening, night);
Assert.AreEqual(formatted, p.ToString());
}
[Test]
public void PrescriptionWithComment()
{
Prescription p = Prescription.FromLine("Ramipril 5 mg 1-0-2 (gesteigert)");
Assert.AreEqual("Ramipril 5 mg", p.Drug);
Assert.AreEqual("1", p.Morning);
Assert.AreEqual("0", p.Noon);
Assert.AreEqual("2", p.Evening);
Assert.AreEqual("(gesteigert)", p.Comment);
}
[Test]
public void PrescriptionsLineWithComment()
{
IList<Prescription> list = Prescription.ManyFromLine(
"Ramipril 5 mg 1-0-2 (gesteigert) \t Concor 2,5 mg 3-2-1-0 neu");
Assert.AreEqual(2, list.Count);
Assert.AreEqual("Ramipril 5 mg", list[0].Drug);
Assert.AreEqual("1", list[0].Morning);
Assert.AreEqual("0", list[0].Noon);
Assert.AreEqual("2", list[0].Evening);
Assert.AreEqual("", list[0].Night);
Assert.AreEqual("(gesteigert)", list[0].Comment);
Assert.AreEqual("Concor 2,5 mg", list[1].Drug);
Assert.AreEqual("3", list[1].Morning);
Assert.AreEqual("2", list[1].Noon);
Assert.AreEqual("1", list[1].Evening);
Assert.AreEqual("0", list[1].Night);
Assert.AreEqual("neu", list[1].Comment);
}
}
}

View File

@ -41,6 +41,7 @@ namespace zaaReloaded2.Medication
}
#endregion
#region Factory
/// <summary>
@ -53,16 +54,17 @@ namespace zaaReloaded2.Medication
{
// Replace any runs of whitespace with a single space
// (from http://stackoverflow.com/a/206946/270712)
line = Regex.Replace(line, @"\s+", " ");
// line = Regex.Replace(line, @"\s+", " ");
Match m = lineRegex.Match(line);
int n = m.Groups[DOSING_GROUP].Captures.Count;
int n = m.Groups[DOSE_GROUP].Captures.Count;
return new Prescription(
m.Groups["drug"].Value,
n > 0 ? m.Groups[DOSING_GROUP].Captures[0].Value : String.Empty,
n > 1 ? m.Groups[DOSING_GROUP].Captures[1].Value : String.Empty,
n > 2 ? m.Groups[DOSING_GROUP].Captures[2].Value : String.Empty,
n > 3 ? m.Groups[DOSING_GROUP].Captures[3].Value : String.Empty
spaceRegex.Replace(m.Groups["drug"].Value, " "),
n > 0 ? m.Groups[DOSE_GROUP].Captures[0].Value : String.Empty,
n > 1 ? m.Groups[DOSE_GROUP].Captures[1].Value : String.Empty,
n > 2 ? m.Groups[DOSE_GROUP].Captures[2].Value : String.Empty,
n > 3 ? m.Groups[DOSE_GROUP].Captures[3].Value : String.Empty,
m.Groups["comment"].Value
);
}
@ -71,19 +73,21 @@ namespace zaaReloaded2.Medication
/// </summary>
/// <param name="line">Line that contains several prescriptions.</param>
/// <returns>Enumerable with <see cref="Prescription"/>s.</returns>
public static IEnumerable<Prescription> ManyFromLine(string line)
public static IList<Prescription> ManyFromLine(string line)
{
// line = Regex.Replace(line, @"\s+", " ");
MatchCollection mc = lineRegex.Matches(line);
List<Prescription> list = new List<Prescription>();
foreach (Match m in mc)
{
int n = m.Groups[DOSING_GROUP].Captures.Count;
int n = m.Groups[DOSE_GROUP].Captures.Count;
list.Add(new Prescription(
m.Groups["drug"].Value,
n > 0 ? m.Groups[DOSING_GROUP].Captures[0].Value : String.Empty,
n > 1 ? m.Groups[DOSING_GROUP].Captures[1].Value : String.Empty,
n > 2 ? m.Groups[DOSING_GROUP].Captures[2].Value : String.Empty,
n > 3 ? m.Groups[DOSING_GROUP].Captures[3].Value : String.Empty
spaceRegex.Replace(m.Groups["drug"].Value, " "),
n > 0 ? m.Groups[DOSE_GROUP].Captures[0].Value : String.Empty,
n > 1 ? m.Groups[DOSE_GROUP].Captures[1].Value : String.Empty,
n > 2 ? m.Groups[DOSE_GROUP].Captures[2].Value : String.Empty,
n > 3 ? m.Groups[DOSE_GROUP].Captures[3].Value : String.Empty,
m.Groups["comment"].Value
)
);
}
@ -104,6 +108,8 @@ namespace zaaReloaded2.Medication
public string Night { get; set; }
public string Comment { get; set; }
#endregion
#region Overrides
@ -168,19 +174,32 @@ namespace zaaReloaded2.Medication
Night = night.Trim();
}
public Prescription(string drug, string morning, string noon,
string evening, string night, string comment)
: this(drug, morning, noon, evening, night)
{
Comment = comment.Trim();
}
#endregion
#region Fields
private const string DOSING_GROUP = "dosing";
private const string DOSING = @"(\d{1,3}((\s1)?/[234])?|[\u00bd\u2153\u00bc])";
private const string SPACER = @"\s*[-\u2012\u2013\u2014]+\s*";
private const string DOSE_GROUP = "dose";
private const string DOSE = @"(\d\s+1/[234]|[\u00bd\u2153\u00bc]|\d+)";
private const string SPACER = @"(\s*[-\u2012\u2013\u2014]+\s*)";
// Enclose entire regular expression in parentheses so we can use it
// to split a line and capture the delimiter.
private static readonly Regex lineRegex = new Regex(
@"((?<drug>.+?)\s(?<dosing>" + DOSING +
@")(\s?[-\u2012\u2013\u2014]+\s?(?<dosing>" + DOSING + @")){1,3})");
@"((?<drug>[^\t]+)\s+" +
@"(?<dose>" + DOSE + @")" + SPACER +
@"(?<dose>" + DOSE + @")" + SPACER +
@"(?<dose>" + DOSE + @")" +
@"(" + SPACER + @"(?<dose>" + DOSE + @"))?" +
@"( +(?<comment>[^\t]+))?\s*)");
private static readonly Regex spaceRegex = new Regex(@"\s+");
#endregion
}