File size: 4,842 Bytes
fab29d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | // Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SlobViewer.Tei
{
/// <summary>
/// Reads a TEI file as can be found in the FreeDict github source directory
/// of the FREEDICT project (see <see href="https://freedict.org/"/>
/// and <see href="https://github.com/freedict/fd-dictionaries"/>).
/// </summary>
public class TeiReader
{
/// <summary>
/// The full file name of the TEI file.
/// </summary>
string _fileName;
/// <summary>Initializes a new instance of the <see cref="TeiReader"/> class.</summary>
/// <param name="fileName">Full file name of the TEI file.</param>
public TeiReader(string fileName)
{
_fileName = fileName;
}
/// <summary>
/// Reads from the TEI file given in the constructor of this class and generates a dictionary.
/// </summary>
/// <returns>A dictionary of key-value pairs.</returns>
public Dictionary<string, string> Read()
{
var dictionary = new Dictionary<string, string>();
var keyList = new List<string>();
using (var stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
var settings = new System.Xml.XmlReaderSettings()
{
CloseInput = false,
IgnoreWhitespace = true,
};
using (var xr = System.Xml.XmlReader.Create(stream, settings))
{
xr.MoveToContent();
xr.ReadStartElement(); // TEI
xr.ReadToNextSibling("text"); // Skip TEI and teiheader
xr.ReadStartElement(); // text
xr.ReadStartElement(); // body
while (xr.Name == "entry")
{
xr.ReadStartElement("entry");
xr.ReadStartElement("form");
var key = xr.ReadElementContentAsString("orth", xr.NamespaceURI);
xr.ReadToFollowing("entry");
keyList.Add(key);
}
}
stream.Seek(0, SeekOrigin.Begin);
using (var xr = System.Xml.XmlReader.Create(stream, settings))
{
xr.MoveToContent();
xr.ReadStartElement(); // TEI
xr.ReadToNextSibling("text"); // Skip TEI and teiheader
xr.ReadStartElement(); // text
xr.ReadStartElement(); // body
for (int i = 0; xr.Name == "entry"; ++i)
{
string content = xr.ReadOuterXml();
if (!dictionary.ContainsKey(keyList[i]))
{
dictionary.Add(keyList[i], content);
}
else
{
bool resolved = false;
var key = keyList[i];
var presentContent = dictionary[key];
var altKey = ReevaluateKey(content);
if (altKey != key && !dictionary.ContainsKey(altKey))
{
dictionary.Add(altKey, content);
resolved = true;
}
else
{
altKey = ReevaluateKey(presentContent);
if (altKey != key && !dictionary.ContainsKey(altKey))
{
dictionary[key] = content;
dictionary[altKey] = presentContent;
resolved = true;
}
}
if (!resolved)
{
if (content.Length > presentContent.Length)
dictionary[key] = content;
}
}
}
}
}
return dictionary;
}
/// <summary>
/// Reevaluates the key of a TEI entry. This is sometimes neccessary as some TEI files do not have unique keys. The call
/// evaluates the TEI entry and tries to generate a key from the content.
/// </summary>
/// <param name="content">The content of the TEI entry.</param>
/// <returns>An alternative key derived from the TEI content.</returns>
private string ReevaluateKey(string content)
{
string key;
string hint = null;
var settings = new System.Xml.XmlReaderSettings()
{
CloseInput = false,
IgnoreWhitespace = true,
};
using (var xr = System.Xml.XmlReader.Create(new StringReader(content)))
{
xr.MoveToContent();
xr.ReadStartElement("entry");
xr.ReadStartElement("form");
key = xr.ReadElementContentAsString("orth", xr.NamespaceURI);
xr.ReadToFollowing("usg");
if (xr.Name == "usg")
{
hint = xr.ReadElementContentAsString("usg", xr.NamespaceURI);
}
}
return hint == null ? key : key + ", " + hint;
}
}
}
|