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

@ -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
}