zaaReloaded2/zaaReloaded2/Medication/Prescription.cs

175 lines
5.5 KiB
C#
Raw Normal View History

/* Prescription.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace zaaReloaded2.Medication
{
/// <summary>
/// Represents a prescription
/// </summary>
public class Prescription
{
#region Factory
/// <summary>
/// Creates a new Prescription object by parsing a line (e.g.,
/// from a physician's letter).
/// </summary>
/// <param name="line">Line to parse</param>
/// <returns>Prescription created from the <paramref name="Line"/></returns>
public static Prescription FromLine(string line)
{
// Replace any runs of whitespace with a single space
// (from http://stackoverflow.com/a/206946/270712)
line = Regex.Replace(line, @"\s+", " ");
Match m = lineRegex.Match(line);
int n = m.Groups[DOSING_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
);
}
2015-11-26 18:44:33 +00:00
/// <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
#region Properties
public string Drug { get; set; }
public string Morning { get; set; }
public string Noon { get; set; }
public string Evening { get; set; }
public string Night { get; set; }
#endregion
2015-11-26 18:44:33 +00:00
#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
public Prescription() { }
public Prescription(string drug, string morning, string noon,
string evening, string night)
{
2015-11-26 18:44:33 +00:00
Drug = drug.Trim();
Morning = morning.Trim();
Noon = noon.Trim();
Evening = evening.Trim();
Night = night.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*";
2015-11-26 18:44:33 +00:00
// 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(
2015-11-26 18:44:33 +00:00
@"((?<drug>.+?)\s(?<dosing>" + DOSING +
@")(\s?[-\u2012\u2013\u2014]+\s?(?<dosing>" + DOSING + @")){1,3})");
#endregion
}
}