Make AutoDetector aware of clinic mode.

This commit is contained in:
daniel 2017-09-18 06:06:10 +02:00
parent ba485ac560
commit 08bd137e94
5 changed files with 82 additions and 22 deletions

View File

@ -203,6 +203,7 @@ namespace zaaReloaded2
Word.Window activeWindow = word.ActiveWindow; Word.Window activeWindow = word.ActiveWindow;
Word.Selection selection = activeWindow.Selection; Word.Selection selection = activeWindow.Selection;
Word.Paragraphs paragraphs = selection.Paragraphs; Word.Paragraphs paragraphs = selection.Paragraphs;
Importer.IImporter importer = null;
if (!(paragraphs.Count > 1 if (!(paragraphs.Count > 1
|| (selection.Text.Length > 1 && selection.Text.EndsWith("\r")))) || (selection.Text.Length > 1 && selection.Text.EndsWith("\r"))))
{ {
@ -221,10 +222,10 @@ namespace zaaReloaded2
} }
// Don't release the COM object here // Don't release the COM object here
// Bovender.ComHelpers.ReleaseComObject(doc); // Bovender.ComHelpers.ReleaseComObject(doc);
importer = autoDetector.CreateImporter();
} }
Logger.Info("DoFormat: Importing"); Logger.Info("DoFormat: Importing");
ZaaImporter importer = new ZaaImporter();
importer.Import(selection.Text); importer.Import(selection.Text);
Formatter.Formatter formatter = new Formatter.Formatter(activeDocument); Formatter.Formatter formatter = new Formatter.Formatter(activeDocument);

Binary file not shown.

View File

@ -27,8 +27,14 @@ namespace zaaReloaded2.Importer
{ {
class AutoDetector class AutoDetector
{ {
#region Properties
public ImportMode ImportMode { get; private set; }
#endregion
#region Public methods #region Public methods
/// <summary> /// <summary>
/// Attempts to automatically detect laboratory data in the Word /// Attempts to automatically detect laboratory data in the Word
/// document. /// document.
@ -53,6 +59,7 @@ namespace zaaReloaded2.Importer
if (document.Bookmarks.Exists("Labor")) if (document.Bookmarks.Exists("Labor"))
{ {
Logger.Info("Detect: Found lab bookmark");
i = GetParagraphIndex( i = GetParagraphIndex(
document, document,
document.Bookmarks["Labor"].Range.Paragraphs[1]); document.Bookmarks["Labor"].Range.Paragraphs[1]);
@ -65,10 +72,10 @@ namespace zaaReloaded2.Importer
// lines such as "Tel. (09 31) 201-39432; -39126", which // lines such as "Tel. (09 31) 201-39432; -39126", which
// happen to structurally resemble a paragraph with // happen to structurally resemble a paragraph with
// laboratory items. // laboratory items.
if (LaurisTimePoint.IsTimeStampLine( if (IsTimeStampParagraph(document.Paragraphs[i]))
document.Paragraphs[i].Range.Text))
{ {
start = document.Paragraphs[i]; start = document.Paragraphs[i];
Logger.Info("Detect: Found time stamp line in paragraph #{0}", i);
break; break;
} }
i++; i++;
@ -76,11 +83,14 @@ namespace zaaReloaded2.Importer
if (start != null) if (start != null)
{ {
Logger.Info("Detect: Determining lab block");
end = start; end = start;
while (i <= document.Paragraphs.Count - 1) while (i <= document.Paragraphs.Count - 1)
{ {
if (!IsLabParagraph(document.Paragraphs[i+1])) Paragraph p = document.Paragraphs[i+1];
if (!IsLabParagraph(p) && !IsEmptyParagraph(p))
{ {
Logger.Info("Detect: Last lab paragraph is #{0}", i);
end = document.Paragraphs[i]; end = document.Paragraphs[i];
break; break;
} }
@ -90,9 +100,23 @@ namespace zaaReloaded2.Importer
document.Range(start.Range.Start, end.Range.End).Select(); document.Range(start.Range.Start, end.Range.End).Select();
return true; return true;
} }
Logger.Warn("Detect: Did not find lab block!");
return false; return false;
} }
public IImporter CreateImporter()
{
switch (ImportMode)
{
case ImportMode.Zaa:
return new ZaaImporter.ZaaImporter();
case ImportMode.Clinic:
return new ClinicImporter.ClinicImporter();
default:
throw new InvalidOperationException("Cannot create Importer for undefined import mode!");
}
}
#endregion #endregion
#region Private methods #region Private methods
@ -109,9 +133,10 @@ namespace zaaReloaded2.Importer
// the lab mode *must* be ZAA, because it will be a line in the form // the lab mode *must* be ZAA, because it will be a line in the form
// "(17.09.2015-201710:44:00) Cyclosporin-A vor Gabe: 130 µg/l;" which does not // "(17.09.2015-201710:44:00) Cyclosporin-A vor Gabe: 130 µg/l;" which does not
// occur in the clinic format. // occur in the clinic format.
if ((_mode == Mode.Undefined) && isZaaTimePoint && !isCinicTimePoint) if ((ImportMode == ImportMode.Undefined) && isZaaTimePoint && !isCinicTimePoint)
{ {
_mode = Mode.Zaa; Logger.Info("IsTimeStampParagraph: Found ZAA time stamp, setting mode to ZAA");
ImportMode = ImportMode.Zaa;
} }
return isCinicTimePoint || isZaaTimePoint; return isCinicTimePoint || isZaaTimePoint;
} }
@ -131,24 +156,26 @@ namespace zaaReloaded2.Importer
{ {
string text = paragraph.Range.Text; string text = paragraph.Range.Text;
bool isLabParagraph = false; bool isLabParagraph = false;
switch (_mode) switch (ImportMode)
{ {
case Mode.Undefined: case ImportMode.Undefined:
if (LaurisParagraph.ResemblesLaurisParagraph(text) || LaurisTimePoint.IsTimeStampLine(text)) if (LaurisParagraph.ResemblesLaurisParagraph(text) || LaurisTimePoint.IsTimeStampLine(text))
{ {
_mode = Mode.Zaa; ImportMode = ImportMode.Zaa;
Logger.Info("IsLabParagraph: Setting mode to ZAA");
isLabParagraph = true; isLabParagraph = true;
} }
else if (ClinicLine.ResemblesClinicLine(text) || ClinicTimePoint.IsTimeStampLine(text)) else if (ClinicLine.ResemblesClinicLine(text) || ClinicTimePoint.IsTimeStampLine(text))
{ {
_mode = Mode.Clinic; ImportMode = ImportMode.Clinic;
Logger.Info("IsLabParagraph: Setting mode to Clinic");
isLabParagraph = true; isLabParagraph = true;
} }
break; break;
case Mode.Zaa: case ImportMode.Zaa:
isLabParagraph = LaurisParagraph.ResemblesLaurisParagraph(text) || LaurisTimePoint.IsTimeStampLine(text); isLabParagraph = LaurisParagraph.ResemblesLaurisParagraph(text) || LaurisTimePoint.IsTimeStampLine(text);
break; break;
case Mode.Clinic: case ImportMode.Clinic:
isLabParagraph = ClinicLine.ResemblesClinicLine(text) || ClinicTimePoint.IsTimeStampLine(text); isLabParagraph = ClinicLine.ResemblesClinicLine(text) || ClinicTimePoint.IsTimeStampLine(text);
break; break;
default: default:
@ -169,19 +196,20 @@ namespace zaaReloaded2.Importer
{ {
return document.Range(0, paragraph.Range.Start).Paragraphs.Count; return document.Range(0, paragraph.Range.Start).Paragraphs.Count;
} }
private bool IsEmptyParagraph(Paragraph paragraph)
{
string text = paragraph.Range.Text;
return String.IsNullOrWhiteSpace(text);
}
#endregion #endregion
#region Fields #region Class logger
private enum Mode private static NLog.Logger Logger { get { return _logger.Value; } }
{
Undefined,
Zaa,
Clinic
}
private Mode _mode; private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
#endregion #endregion
} }

View File

@ -121,7 +121,7 @@ namespace zaaReloaded2.Importer.ClinicImporter
#region Fields #region Fields
static readonly Regex _expectedFormat = new Regex(@"\t(?<item>[^:]+:(\t[^\t]+){3})"); static readonly Regex _expectedFormat = new Regex(@"\t(?<item>[^:]+:(\t([^\t]+)?){3})");
Thesaurus.Parameters _parameterDictionary; Thesaurus.Parameters _parameterDictionary;
Thesaurus.Units _unitDictionary; Thesaurus.Units _unitDictionary;

View File

@ -0,0 +1,31 @@
/* ImportMode.cs
* part of zaaReloaded2
*
* Copyright 2017 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.Importer
{
public enum ImportMode
{
Undefined,
Zaa,
Clinic
}
}