131 lines
3.8 KiB
C#
Executable File
131 lines
3.8 KiB
C#
Executable File
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 static 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))
|
|
{
|
|
end = document.Paragraphs[i];
|
|
break;
|
|
}
|
|
i--;
|
|
}
|
|
|
|
if (end != null)
|
|
{
|
|
start = end;
|
|
while (i > 2)
|
|
{
|
|
if (!Prescription.IsPrescriptionLine(document.Paragraphs[i - 1].Range.Text))
|
|
{
|
|
start = 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)
|
|
{
|
|
List<Prescription> list = new List<Prescription>();
|
|
IList<Prescription> addition;
|
|
int columns = 1;
|
|
string[] lines = Helpers.SplitParagraphs(text);
|
|
foreach (string line in lines)
|
|
{
|
|
if (Prescription.IsPrescriptionLine(line))
|
|
{
|
|
addition = Prescription.ManyFromLine(line);
|
|
columns = System.Math.Max(columns, addition.Count);
|
|
list.AddRange(addition);
|
|
}
|
|
}
|
|
|
|
// If the input had several columns, sort the prescriptions by
|
|
// column.
|
|
// TODO: Make this more generic so it works with 3 or 4 columns as well.
|
|
if (columns == 2)
|
|
{
|
|
var firstCol = list.Where((item, index) => index % 2 == 0);
|
|
var secondCol = list.Where((item, index) => index % 2 != 0);
|
|
Prescriptions = firstCol.Concat(secondCol).ToList();
|
|
}
|
|
else
|
|
{
|
|
Prescriptions = list;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|