Skip to content

Commit

Permalink
Fix terms logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Dec 5, 2023
1 parent 0364eee commit 887d04a
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/SIL.Machine/Corpora/ParatextBackupTermsCorpus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public ParatextBackupTermsCorpus(string fileName, IEnumerable<string> termCatego
ZipArchiveEntry biblicalTermsFileEntry = archive.GetEntry(textId.Split(':').Last());

XDocument biblicalTermsDoc;
IDictionary<string, string> termIdToCategoryDictionary;
if (PREDEFINED_TERMS_LIST_TYPES.Contains(textId.Split(':').First()))
{
using (
Expand All @@ -57,36 +58,49 @@ public ParatextBackupTermsCorpus(string fileName, IEnumerable<string> termCatego
)
{
biblicalTermsDoc = XDocument.Load(keyTermsFile);
termIdToCategoryDictionary = biblicalTermsDoc
.Descendants()
.Where(n => n.Name.LocalName == "Term")
.ToDictionary(e => e.Attribute("Id").Value, e => e.Element("Category")?.Value ?? "");
}
}
else
{
using (var keyTermsFile = biblicalTermsFileEntry.Open())
if (
textId.Split(':').First() == "Project"
&& textId.Split(':')[1] == settingsDoc.Root.Element("Name").Value
)
{
biblicalTermsDoc = XDocument.Load(keyTermsFile);
using (var keyTermsFile = biblicalTermsFileEntry.Open())
{
biblicalTermsDoc = XDocument.Load(keyTermsFile);
termIdToCategoryDictionary = biblicalTermsDoc
.Descendants()
.Where(n => n.Name.LocalName == "Term")
.ToDictionary(e => e.Attribute("Id").Value, e => e.Element("Category")?.Value ?? "");
}
}
else
{
termIdToCategoryDictionary = new Dictionary<string, string>();
}
}

IEnumerable<XElement> termsElements = termRenderingsDoc
.Descendants()
.Where(n => n.Name.LocalName == "TermRendering");

IDictionary<string, string> termIdToCategoryDictionary = biblicalTermsDoc
.Descendants()
.Where(n => n.Name.LocalName == "Term")
.ToDictionary(e => e.Attribute("Id").Value, e => e.Element("Category")?.Value ?? "");

foreach (XElement element in termsElements)
{
string id = element.Attribute("Id").Value;
string category = "";
if (
(
termCategories.Count() > 0
&& termIdToCategoryDictionary.ContainsKey(id)
&& !termCategories.Contains(termIdToCategoryDictionary[id])
)
(termCategories.Count() > 0 && !termIdToCategoryDictionary.TryGetValue(id, out category))
|| (termCategories.Count() > 0 && !termCategories.Contains(category))
)
{
continue;
}
id = id.Replace("\n", "&#xA");
string rendering = element.Element("Renderings").Value;
IReadOnlyList<string> renderings = GetRenderings(rendering);
Expand Down

0 comments on commit 887d04a

Please sign in to comment.