Add prescriptions importer.

This commit is contained in:
Daniel Kraus 2015-11-26 20:02:07 +01:00
parent c35c73f56a
commit bcbed5bca3
5 changed files with 172 additions and 6 deletions

44
zaaReloaded2/Helpers.cs Executable file
View File

@ -0,0 +1,44 @@
/* Helpers.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;
namespace zaaReloaded2
{
/// <summary>
/// Common helper methods.
/// </summary>
public static class Helpers
{
/// <summary>
/// Splits a text into paragraphs.
/// </summary>
/// <param name="paragraph">Text to split.</param>
/// <returns>Array of paragraphs in the text.</returns>
/// <remarks>
/// This implementation relies on the fact that the order of
/// splitting strings in C#'s String.Split() method is
/// important; see http://stackoverflow.com/a/8664639/270712
/// </remarks>
public static string[] SplitParagraphs(string text)
{
return text.Split(
new string[] { "\r\n", "\n\r", "\r", "\n" },
StringSplitOptions.RemoveEmptyEntries);
}
}
}

View File

@ -67,12 +67,7 @@ namespace zaaReloaded2.Importer.ZaaImporter
/// <param name="text">ZAA-formatted Lauris output to import.</param>
public void Import(string text)
{
// Split the text into parargraphs. This implementation relies on the fact
// that the order or splitting strings in C#'s String.Split() method is
// important; see http://stackoverflow.com/a/8664639/270712
string[] paragraphs = text.Split(
new string[] { "\r\n", "\n\r", "\r", "\n" },
StringSplitOptions.RemoveEmptyEntries);
string[] paragraphs = Helpers.SplitParagraphs(text);
LaurisTimePoint timePoint = null;
foreach (string paragraph in paragraphs)

View File

@ -0,0 +1,112 @@
using Microsoft.Office.Interop.Word;
/* Importer.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;
namespace zaaReloaded2.Medication
{
/// <summary>
/// Imports prescriptions from a physician's letter.
/// </summary>
public class Importer
{
#region Static methods
/// <summary>
/// Attempts to automatically detect a block of prescriptions
/// in a document. The document is screened from end to start.
/// The detected block is selected.
/// </summary>
/// <returns>True if a block was detected, false if not.</returns>
public bool AutoDetect(Document document)
{
Paragraph start = null;
Paragraph end = null;
int i = document.Paragraphs.Count;
while (i > 1)
{
string line = document.Paragraphs[i].Range.Text;
if (Prescription.IsPrescriptionLine(line))
{
start = document.Paragraphs[i];
break;
}
i--;
}
if (start != null)
{
end = start;
while (i > 1)
{
if (!Prescription.IsPrescriptionLine(document.Paragraphs[i - 1].Range.Text))
{
end = document.Paragraphs[i];
break;
}
i--;
}
document.Range(start.Range.Start, end.Range.End).Select();
return true;
}
return false;
}
#endregion
#region Properties
public List<Prescription> Prescriptions { get; protected set; }
#endregion
#region Constructor
public Importer() { }
public Importer(string text)
: this()
{
Import(text);
}
#endregion
#region Private methods
protected virtual void Import(string text)
{
Prescriptions = new List<Prescription>();
string[] lines = Helpers.SplitParagraphs(text);
foreach (string line in lines)
{
if (Prescription.IsPrescriptionLine(line))
{
Prescriptions.Add(Prescription.FromLine(line));
}
}
}
#endregion
}
}

View File

@ -28,6 +28,19 @@ namespace zaaReloaded2.Medication
/// </summary>
public class Prescription
{
#region Static methods
/// <summary>
/// Determines whether a line contains prescriptions.
/// </summary>
/// <param name="line">Line to inspect.</param>
/// <returns>True if the line contains prescriptions.</returns>
public static bool IsPrescriptionLine(string line)
{
return lineRegex.IsMatch(line);
}
#endregion
#region Factory
/// <summary>

View File

@ -194,6 +194,7 @@
-->
<ItemGroup>
<Compile Include="Commands.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="Importer\ZaaImporter\AutoDetect.cs" />
<Compile Include="Controller\Comments\CommentPool.cs" />
<Compile Include="Controller\Elements\ControlElementBase.cs" />
@ -221,6 +222,7 @@
<Compile Include="Formatter\DanielsStyle.cs" />
<Compile Include="Formatter\DocumentWriter.cs" />
<Compile Include="Formatter\NoLaboratoryDataException.cs" />
<Compile Include="Medication\Importer.cs" />
<Compile Include="Medication\Prescription.cs" />
<Compile Include="Preferences.cs" />
<Compile Include="Ribbon.cs" />