157 lines
5.2 KiB
C#
Executable File
157 lines
5.2 KiB
C#
Executable File
/* LaurisParagraph.cs
|
|
* part of zaaReloaded2
|
|
*
|
|
* Copyright 2015-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.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using zaaReloaded2.Thesaurus;
|
|
using zaaReloaded2.LabModel;
|
|
using Bovender.Extensions;
|
|
|
|
namespace zaaReloaded2.Importer.ZaaImporter
|
|
{
|
|
/// <summary>
|
|
/// Parses an entire Lauris paragraph (such as "Klinische Chemie: ...")
|
|
/// and creates a list of <see cref="LabItem"/>s.
|
|
/// </summary>
|
|
public class LaurisParagraph
|
|
{
|
|
#region Static methods
|
|
|
|
/// <summary>
|
|
/// Investigates a paragraph and determines whether it looks
|
|
/// like a Lauris laboratory items paragraph.
|
|
/// </summary>
|
|
public static bool ResemblesLaurisParagraph(string paragraph)
|
|
{
|
|
return _expectedFormat.IsMatch(paragraph);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public properties
|
|
|
|
/// <summary>
|
|
/// Gets a collection of <see cref="LabItem"/>s found in this paragraph.
|
|
/// </summary>
|
|
public IItemDictionary Items { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the caption that was extracted from the <see cref="OriginalParagraph"/>,
|
|
/// e.g. "Klin. Chemie" in "Klin. Chemie: Natrium ...".
|
|
/// </summary>
|
|
public string Caption { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the original paragraph that this object was constructed from.
|
|
/// </summary>
|
|
public string OriginalParagraph { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Is true if the <see cref="OriginalParagraph"/> matches the expected
|
|
/// format and contains <see cref="LabItem"/>s.
|
|
/// </summary>
|
|
public bool IsLaurisParagraph { get; private set; }
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public LaurisParagraph(string paragraph)
|
|
{
|
|
OriginalParagraph = paragraph;
|
|
Parse();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a <see cref="LaurisParagraph"/> object from a given
|
|
/// Lauris paragraph, using a <paramref name="parameterDictionary"/>
|
|
/// and a <paramref name="unitDictionary"/> to translate the individual
|
|
/// items' properties.
|
|
/// </summary>
|
|
/// <param name="paragraph">lauris paragraph to parse.</param>
|
|
/// <param name="parameterDictionary">ParameterDictionary that contains
|
|
/// canonical names and material types.</param>
|
|
/// <param name="unitDictionary">Unit dictionary that contains canonical
|
|
/// unit names.</param>
|
|
public LaurisParagraph(string paragraph,
|
|
Thesaurus.Parameters parameterDictionary,
|
|
Thesaurus.Units unitDictionary)
|
|
{
|
|
OriginalParagraph = paragraph;
|
|
_parameterDictionary = parameterDictionary;
|
|
_unitDictionary = unitDictionary;
|
|
Parse();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private methods
|
|
|
|
/// <summary>
|
|
/// Attempts to parse a Lauris paragraph.
|
|
/// </summary>
|
|
void Parse()
|
|
{
|
|
Logger.Info("Parse: \"{0}\"", OriginalParagraph.TruncateWithEllipsis(40));
|
|
Match m = _expectedFormat.Match(OriginalParagraph);
|
|
if (m.Success)
|
|
{
|
|
Logger.Info("Parse: Matches Lauris paragraph format");
|
|
Items = new ItemDictionary();
|
|
if (m.Groups["caption"].Success)
|
|
{
|
|
Caption = m.Groups["caption"].Value.Trim(new char[] {' ', ':'});
|
|
}
|
|
|
|
foreach (Capture itemCapture in m.Groups["items"].Captures)
|
|
{
|
|
LaurisItem i = new LaurisItem(itemCapture.Value, _parameterDictionary, _unitDictionary);
|
|
Items[i.QualifiedName] = i;
|
|
}
|
|
IsLaurisParagraph = Items.Count > 0;
|
|
}
|
|
else
|
|
{
|
|
Logger.Info("Parse: Does not match Lauris paragraph format");
|
|
IsLaurisParagraph = false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Fields
|
|
|
|
static readonly Regex _expectedFormat = new Regex(@"(?<caption>[^:]+:\s*)?(?<items>[^:]+:\s*[^;]+;)+");
|
|
Thesaurus.Parameters _parameterDictionary;
|
|
Thesaurus.Units _unitDictionary;
|
|
|
|
#endregion
|
|
|
|
#region Class logger
|
|
|
|
private static NLog.Logger Logger { get { return _logger.Value; } }
|
|
|
|
private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
|
|
|
|
#endregion
|
|
}
|
|
}
|