|
|
|
|
|
|
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.IO; |
|
|
using System.Linq; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
|
|
|
namespace SlobViewer.Tei |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class TeiReader |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
string _fileName; |
|
|
|
|
|
|
|
|
|
|
|
public TeiReader(string fileName) |
|
|
{ |
|
|
_fileName = fileName; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
xr.ReadToNextSibling("text"); |
|
|
|
|
|
xr.ReadStartElement(); |
|
|
|
|
|
xr.ReadStartElement(); |
|
|
|
|
|
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(); |
|
|
|
|
|
xr.ReadToNextSibling("text"); |
|
|
|
|
|
xr.ReadStartElement(); |
|
|
|
|
|
xr.ReadStartElement(); |
|
|
|
|
|
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; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
} |
|
|
} |
|
|
|