private static void WriteWord(StreamWriter writer, string headword, List <Schema.A> cards) { if (cards.Count == 0) { return; } if (cards.Count > 1) { writer.WriteLine(headword); for (int i = 0; i < cards.Count; ++i) { Schema.A card = cards[i]; // Writing word number writer.Write("\t[b]"); writer.Write(i + 1); writer.Write(".[/b]"); WritePartOfSpeech(writer, card, " "); string cardHeadword = GetCardHeadword(card); if (!String.IsNullOrEmpty(cardHeadword)) { writer.Write(' '); WriteEscaped(writer, FixHeadword(cardHeadword)); } // Need new line even if WritePartOfSpeech haven't write anything writer.WriteLine(); WriteCard(writer, card); } } else { Schema.A card = cards[0]; string cardHeadword = GetCardHeadword(card); if (String.IsNullOrEmpty(cardHeadword)) { Console.WriteLine($"Can't get headword for card {headword}"); } else { writer.WriteLine(EscapeCardHeadword(FixHeadword(cardHeadword))); } WritePartOfSpeech(writer, card, "\t", writer.NewLine); WriteCard(writer, card); } writer.WriteLine(); }
private static string GetCardHeadword(Schema.A card) { if (card.P == null || card.P.mg == null || card.P.mg.m == null || String.IsNullOrEmpty(card.P.mg.m.Value)) { return(null); } return(card.P.mg.m.Value); }
private static Dictionary <string, List <Sortable <Schema.A> > > GetWordsDictionary(string xmlPath) { Dictionary <string, List <Sortable <Schema.A> > > wordsMap = new Dictionary <string, List <Sortable <Schema.A> > >(); using (FileStream inputFileStream = new FileStream(xmlPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (XmlReader reader = CreateXmlReader(inputFileStream)) { XmlSerializer serializer = new XmlSerializer(typeof(Schema.A)); Schema.A word = null; while ((word = GetNextWord(reader, serializer)) != null) { if (String.IsNullOrEmpty(word.P.mg.m.O)) { Console.WriteLine($"Empty sortvalue for {GetCardHeadword(word)}"); continue; } string headword = GetCardHeadword(word); if (String.IsNullOrEmpty(headword)) { Console.WriteLine("Empty headword!"); continue; } headword = ClearHeadword(headword); if (String.IsNullOrEmpty(headword)) { Console.WriteLine("Empty headword after cleaning!"); continue; } List <Sortable <Schema.A> > words = null; if (!wordsMap.TryGetValue(headword, out words)) { words = new List <Sortable <Schema.A> >(); wordsMap.Add(headword, words); } if (word.P.mg.m.i != null) { int homonymNumber = 0; if (!Int32.TryParse(word.P.mg.m.i, out homonymNumber)) { Console.WriteLine($"Failed to parse homonym number for headword \"{headword}\" for word \"{word.P.mg.m.Value})\""); continue; } words.Add(new Sortable <Schema.A> { Object = word, SortOrder = homonymNumber }); } else { words.Add(new Sortable <Schema.A> { Object = word, SortOrder = 0 }); } } } return(wordsMap); }
private static void WriteCard(StreamWriter writer, Schema.A card) { // Adding forms if (card.P.grg != null) { foreach (Schema.grg grg in card.P.grg) { if (String.IsNullOrEmpty(grg.mv)) { continue; } writer.Write('\t'); WriteForms(writer, grg.mv); writer.WriteLine(); } } // Adding translations for (int i = 0; i < card.S.Length; ++i) { Schema.tp tp = card.S[i]; // Adding link to another translation if (tp.tvt != null) { bool writeOpenTag = true; bool writeCloseTag = false; bool appendComma = false; foreach (Schema.tvt tvt in tp.tvt) { if (String.IsNullOrEmpty(tvt.Value)) { continue; } if (writeOpenTag) { writer.Write("\t[m1]"); writeOpenTag = false; writeCloseTag = true; } if (appendComma) { writer.Write(", "); } else { appendComma = true; } // Sometimes there are two words delimited by comma string[] headwords = tvt.Value.Split(','); bool appendCommaHeadwords = false; foreach (string headword in headwords) { if (appendCommaHeadwords) { writer.Write(", "); } else { appendCommaHeadwords = true; } writer.Write("<<"); writer.Write(ClearHeadword(headword)); writer.Write(">>"); } } if (writeCloseTag) { writer.WriteLine("[/m]"); } } // Adding translation if (tp.tg != null) { bool appendComma = false; bool writeOpenTag = true; bool writeCloseTag = false; foreach (Schema.tg tg in tp.tg) { if (tg.xp == null || tg.xp.xg == null) { continue; } foreach (Schema.xg xg in tg.xp.xg) { foreach (string text in xg.x.Text) { if (String.IsNullOrEmpty(text)) { continue; } if (writeOpenTag) { // Adding translation number writer.Write("\t[m1]"); writer.Write(i + 1); writer.Write(") "); // Adding definitions if (tg.dg != null) { if (WriteDefinition(writer, tg.dg)) { writer.Write(" "); } } // Adding labels if (tg.dg != null) { if (WriteDefinitionGroup(writer, tg.dg)) { writer.Write(' '); } } writer.Write("[trn]"); writeOpenTag = false; writeCloseTag = true; } if (appendComma) { writer.Write(", "); } else { appendComma = true; } WriteTranslation(writer, text.Trim()); if (!String.IsNullOrEmpty(xg.aspvst)) { writer.Write('/'); WriteTranslation(writer, xg.aspvst); } if (xg.vstiil != null) { writer.Write(" ("); appendComma = false; foreach (Schema.vstiil vstiil in xg.vstiil) { WriteLabel(writer, vstiil.Value.ToString(), appendComma, ", "); appendComma = true; } writer.Write(')'); } } } } if (writeCloseTag) { writer.WriteLine("[/trn][/m]"); } } // Adding examples if (tp.np != null) { bool writeExamleTagOpen = true; bool writeExamleTagClose = false; foreach (Schema.ng ng in tp.np) { // Write estonian if (ng.n != null) { bool writeLangTagOpen = true; bool writeLangTagClose = false; bool appendComma = false; foreach (Schema.n n in ng.n) { foreach (string text in n.Text) { if (String.IsNullOrEmpty(text)) { continue; } if (writeExamleTagOpen) { writer.WriteLine("\t[*][ex][m2]"); writeExamleTagOpen = false; writeExamleTagClose = true; } if (appendComma) { writer.Write(", "); } else { appendComma = true; } if (writeLangTagOpen) { writer.Write("\t\t[lang name=\"Estonian\"]"); writeLangTagOpen = false; writeLangTagClose = true; } WriteExample(writer, text); } } if (writeLangTagClose) { writer.Write("[/lang]"); } } // Write translation if (ng.qnp != null) { bool addSeparator = true; bool appendComma = false; foreach (Schema.qnp qnp in ng.qnp) { foreach (Schema.qng qng in qnp.qng) { string fullText = ""; foreach (string text in qng.qn.Text) { if (String.IsNullOrEmpty(text)) { continue; } fullText += text; } if (addSeparator) { writer.Write(" — "); addSeparator = false; } if (appendComma) { writer.Write(", "); } else { appendComma = true; } WriteTranslation(writer, fullText.Trim()); } } } writer.WriteLine(); } if (writeExamleTagClose) { writer.WriteLine("\t[/m][/ex][/*]"); } } } // Adding phraselogical expressions if (card.F != null) { bool writeOpenTag = true; bool writeCloseTag = false; foreach (Schema.fg fg in card.F) { if (fg.f == null) { continue; } bool writeStartSubcard = true; bool writeCloseSubcard = false; bool appendComma = false; bool headerAdded = false; // Writing subcard headword foreach (Schema.f f in fg.f) { if (f.Text == null) { continue; } string headword = ""; foreach (string text in f.Text) { if (String.IsNullOrEmpty(text)) { continue; } headword += text; } if (writeOpenTag) { // Square character writer.WriteLine("\t[*]\u25a0"); writeOpenTag = false; writeCloseTag = true; } if (writeStartSubcard) { writer.Write("\t@"); writeStartSubcard = false; writeCloseSubcard = true; } if (appendComma) { writer.Write(", "); } else { appendComma = true; } WriteSubCardHeadword(writer, headword.Trim()); headerAdded = true; } if (!headerAdded) { break; } writer.WriteLine(); // Writing subcard translation if (fg.fqnp == null) { break; } appendComma = false; writer.Write("\t[m1]"); foreach (Schema.fqnp fqnp in fg.fqnp) { if (fqnp.fqng == null) { continue; } foreach (Schema.fqng fqng in fqnp.fqng) { if (fqng.qf == null || fqng.qf.Text == null) { continue; } foreach (string text in fqng.qf.Text) { if (appendComma) { writer.Write(", "); } else { appendComma = true; } WriteTranslation(writer, text.Trim()); } if (fqng.s != null) { writer.Write(" ("); bool appendDelimiter = false; foreach (Schema.s s in fqng.s) { WriteLabel(writer, s.Value.ToString(), appendDelimiter); appendDelimiter = true; } writer.Write(")"); } } } writer.WriteLine("[/m]"); if (writeCloseSubcard) { writer.WriteLine("\t@"); } } if (writeCloseTag) { writer.WriteLine("\t[/*]"); } } }
private static bool WritePartOfSpeech(StreamWriter writer, Schema.A card, string prepend = null, string append = null) { if (card.P == null || card.P.grg == null) { return(false); } const string comma = ", "; bool added = false; bool appendComma = false; bool addPrepend = prepend != null; bool addAppend = false; foreach (Schema.grg grg in card.P.grg) { if (String.IsNullOrEmpty(grg.sly)) { continue; } if (addPrepend) { if (prepend != null) { writer.Write(prepend); } addPrepend = false; addAppend = true; } // ex: Adv_&_Postp if (grg.sly.Contains('&')) { string[] substrings = grg.sly.Split('&'); foreach (string str in substrings) { WriteLabel(writer, str.Trim('_'), appendComma, comma); appendComma = true; } } else { WriteLabel(writer, grg.sly, appendComma, comma); appendComma = true; } added = true; } if (addAppend) { if (append != null) { writer.Write(append); } } return(added); }