public override bool Execute() { var latin = new LatinDictionary(); var definedWords = new Dictionary<string, Tuple<int, string, string>>(); _definedWords = 0; _undefinedWords = 0; foreach (var file in Files) { LoadFile(file); var content = new StringBuilder(); content.AppendLine($@"\newbook{{{_bookNumber}}}"); // define all words _currentLine = 1; var macro = "lline"; foreach (var element in _elements) { if (MaxLines > 0 && _currentLine > MaxLines) { break; } AssertCurrentLineNumber(element); switch (element.Name.LocalName) { case "milestone": macro = "pline"; break; case "l": content.Append(@"\" + macro + "{"); macro = "lline"; var wb = new StringBuilder(); foreach (var c in element.Value) { if (char.IsLetter(c)) { wb.Append(c); } else { if (wb.Length > 0) { var word = wb.ToString(); wb.Clear(); DefineAndPrintWord(latin, content, definedWords, word); } AppendCharacter(content, c); } } if (wb.Length > 0) { DefineAndPrintWord(latin, content, definedWords, wb.ToString()); wb.Clear(); } content.AppendLine("}"); _currentLine++; break; } } File.WriteAllText(Path.Combine(OutputPath, $"content{_bookNumber}.tex"), content.ToString()); } // sort common/uncommon words var ordered = definedWords.OrderByDescending(d => d.Value.Item1).ToList(); var commonWords = ordered.Take(CommonWordCount).Select(d => new KeyValuePair<string, Tuple<string, string>>(d.Key, Tuple.Create(d.Value.Item2, d.Value.Item3))); var uncommonWords = ordered.Skip(CommonWordCount).Select(d => new KeyValuePair<string, Tuple<string, string>>(d.Key, Tuple.Create(d.Value.Item2, d.Value.Item3))); File.WriteAllText(Path.Combine(OutputPath, "commonwords.tex"), string.Join("\r\n", commonWords.OrderBy(kvp => kvp.Key).Select(kvp => $@"\newcommonterm{{{EscapeString(kvp.Key)}}}{{{EscapeString(kvp.Value.Item1)}}}{{{EscapeString(kvp.Value.Item2)}}}"))); File.WriteAllText(Path.Combine(OutputPath, "uncommonwords.tex"), string.Join("\r\n", uncommonWords.OrderBy(kvp => kvp.Key).Select(kvp => $@"\newuncommonterm{{{EscapeString(kvp.Key)}}}{{{EscapeString(kvp.Value.Item1)}}}{{{EscapeString(kvp.Value.Item2)}}}"))); if (MaxLines > 0) { Log.LogWarning($"Warning because because {nameof(MaxLines)} was specified."); } Log.LogMessage($"Defined {_definedWords} words with {_undefinedWords} undefined. {_definedWords * 100.0 / (_definedWords + _undefinedWords)}% success rate."); return true; }
private void DefineAndPrintWord(LatinDictionary latin, StringBuilder content, Dictionary<string, Tuple<int, string, string>> definedWords, string word) { var definitionGroups = latin.GetDefinitions(word).ToArray(); bool defined = false; switch (definitionGroups.Length) { case 0: _undefinedWords++; Log.LogWarning($"Unable to define {word} on line {_currentLine}."); break; case 1: defined = true; foreach (var part in definitionGroups.Single().Parts) { _definedWords++; var entry = part.Stem.Entry; content.Append($@"\agls{{{EscapeString(entry.EntryKey)}}}{{{EscapeString(word.Substring(part.Span.Offset, part.Span.Length))}}}"); if (definedWords.ContainsKey(entry.EntryKey)) { var tuple = definedWords[entry.EntryKey]; definedWords[entry.EntryKey] = Tuple.Create(tuple.Item1 + 1, tuple.Item2, tuple.Item3); } else { definedWords[entry.EntryKey] = Tuple.Create(1, entry.Entry, entry.Definition); } Console.WriteLine($"Found definition on line {_currentLine}: {word} = {entry.Entry} - {entry.Definition}"); } break; default: _undefinedWords++; Log.LogWarning($"Ambiguous definition for {word} on line {_currentLine}: [{string.Join("; ", definitionGroups.SelectMany(m => m.Parts.Select(fs => fs.Stem.Entry.Entry)))}]."); break; } if (!defined) { content.Append(EscapeString(word)); } }