Add multiple prescriptions parsing.

This commit is contained in:
Daniel Kraus 2015-11-26 19:44:33 +01:00
parent a255349672
commit c35c73f56a
2 changed files with 108 additions and 7 deletions

View File

@ -44,5 +44,31 @@ namespace Tests.Medication
Assert.AreEqual(noon, p.Noon, "Noon should be " + noon); Assert.AreEqual(noon, p.Noon, "Noon should be " + noon);
Assert.AreEqual(evening, p.Evening, "Evening should be " + evening); Assert.AreEqual(evening, p.Evening, "Evening should be " + evening);
} }
[Test]
public void MultiplePrescriptions()
{
IEnumerable<Prescription> list = Prescription.ManyFromLine(
"Ramipril 5 mg 1-0-0 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());
}
[Test]
[TestCase("Ramipril 5 mg", "1", "0", "0", "0", "Ramipril 5 mg\t1-0-0-0")]
[TestCase("Ramipril 5 mg", "1", "0", "0", "", "Ramipril 5 mg\t1-0-0")]
[TestCase("Ramipril 5 mg", "1", "0", "", "", "Ramipril 5 mg\t1-0")]
[TestCase("Ramipril 5 mg", "1", "", "", "", "Ramipril 5 mg\t1")]
[TestCase("Ramipril 5 mg", "1", "", "0", "0", "Ramipril 5 mg\t1-0-0-0")]
[TestCase("Ramipril 5 mg", "1", "0", "", "0", "Ramipril 5 mg\t1-0-0-0")]
[TestCase("Ramipril 5 mg", "1", "", "", "0", "Ramipril 5 mg\t1-0-0-0")]
[TestCase("Ramipril 5 mg", "", "", "", "", "Ramipril 5 mg\t")]
public void PrescriptionToString(string drug, string morning, string noon,
string evening, string night, string formatted)
{
Prescription p = new Prescription(drug, morning, noon, evening, night);
Assert.AreEqual(formatted, p.ToString());
}
} }
} }

View File

@ -53,6 +53,30 @@ namespace zaaReloaded2.Medication
); );
} }
/// <summary>
/// Extracts several prescriptions from a given line.
/// </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)
{
MatchCollection mc = lineRegex.Matches(line);
List<Prescription> list = new List<Prescription>();
foreach (Match m in mc)
{
int n = m.Groups[DOSING_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
)
);
}
return list;
}
#endregion #endregion
#region Properties #region Properties
@ -69,6 +93,54 @@ namespace zaaReloaded2.Medication
#endregion #endregion
#region Overrides
public override string ToString()
{
string s = Drug + "\t";
if (!String.IsNullOrEmpty(Morning))
{
s += Morning;
}
else
{
if (!(String.IsNullOrEmpty(Noon) && String.IsNullOrEmpty(Evening) &&
String.IsNullOrEmpty(Night)))
{
s += "0";
}
}
if (!String.IsNullOrEmpty(Noon))
{
s += "-" + Noon;
}
else
{
if (!(String.IsNullOrEmpty(Evening) && String.IsNullOrEmpty(Night)))
{
s += "-0";
}
}
if (!String.IsNullOrEmpty(Evening))
{
s += "-" + Evening;
}
else
{
if (!String.IsNullOrEmpty(Night))
{
s += "-0";
}
}
if (!String.IsNullOrEmpty(Night))
{
s += "-" + Night;
}
return s;
}
#endregion
#region Constructor #region Constructor
public Prescription() { } public Prescription() { }
@ -76,11 +148,11 @@ namespace zaaReloaded2.Medication
public Prescription(string drug, string morning, string noon, public Prescription(string drug, string morning, string noon,
string evening, string night) string evening, string night)
{ {
Drug = drug; Drug = drug.Trim();
Morning = morning; Morning = morning.Trim();
Noon = noon; Noon = noon.Trim();
Evening = evening; Evening = evening.Trim();
Night = night; Night = night.Trim();
} }
#endregion #endregion
@ -90,9 +162,12 @@ namespace zaaReloaded2.Medication
private const string DOSING_GROUP = "dosing"; private const string DOSING_GROUP = "dosing";
private const string DOSING = @"(\d{1,3}((\s1)?/[234])?|[\u00bd\u2153\u00bc])"; private const string DOSING = @"(\d{1,3}((\s1)?/[234])?|[\u00bd\u2153\u00bc])";
private const string SPACER = @"\s*[-\u2012\u2013\u2014]+\s*"; 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( private static readonly Regex lineRegex = new Regex(
@"(?<drug>.+?)\s(?<dosing>" + DOSING + @"((?<drug>.+?)\s(?<dosing>" + DOSING +
@")(\s?[-\u2012\u2013\u2014]+\s?(?<dosing>" + DOSING + @")){1,3}"); @")(\s?[-\u2012\u2013\u2014]+\s?(?<dosing>" + DOSING + @")){1,3})");
#endregion #endregion
} }