X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2Fparser%2FEnWiktionaryXmlParser.java;h=428e11d41489c16ba9804a46546f0f674175033a;hb=2ef2645d18d49dafc11e4b1c02e548849ac82ed2;hp=6a6f43856a2bb34aa3e720d9ae839bc80401f6b3;hpb=a7ae2524281869de5aa756ae35524b21bab3e08a;p=DictionaryPC.git diff --git a/src/com/hughes/android/dictionary/parser/EnWiktionaryXmlParser.java b/src/com/hughes/android/dictionary/parser/EnWiktionaryXmlParser.java index 6a6f438..428e11d 100644 --- a/src/com/hughes/android/dictionary/parser/EnWiktionaryXmlParser.java +++ b/src/com/hughes/android/dictionary/parser/EnWiktionaryXmlParser.java @@ -46,7 +46,7 @@ public class EnWiktionaryXmlParser { "Noun|Verb|Adjective|Adverb|Pronoun|Conjunction|Interjection|" + "Preposition|Proper noun|Article|Prepositional phrase|Acronym|" + "Abbreviation|Initialism|Contraction|Prefix|Suffix|Symbol|Letter|" + - "Ligature|Idiom|Phrase|" + + "Ligature|Idiom|Phrase|" + //|\\{\\{acronym\\}\\}|\\{\\{initialism\\}\\} // These are @deprecated: "Noun form|Verb form|Adjective form|Nominal phrase|Noun phrase|" + "Verb phrase|Transitive verb|Intransitive verb|Reflexive verb|" + @@ -165,9 +165,10 @@ public class EnWiktionaryXmlParser { private void doTranslations(final String title, final WikiTokenizer wikiTokenizer, final String pos) { if (title.equals("absolutely")) { - System.out.println(); + //System.out.println(); } + String topLevelLang = null; String sense = null; boolean done = false; while (wikiTokenizer.nextToken() != null) { @@ -181,8 +182,6 @@ public class EnWiktionaryXmlParser { // Check whether we care about this line: - //line = WikiLineReader.removeSquareBrackets(line); - if (wikiTokenizer.isFunction()) { final String functionName = wikiTokenizer.functionName(); final List positionArgs = wikiTokenizer.functionPositionArgs(); @@ -202,6 +201,7 @@ public class EnWiktionaryXmlParser { // TODO: would also be nice... } else if (functionName.startsWith("picdic")) { } else if (functionName.startsWith("checktrans")) { + done = true; } else if (functionName.startsWith("ttbc")) { wikiTokenizer.nextLine(); // TODO: would be great to handle ttbc @@ -213,6 +213,10 @@ public class EnWiktionaryXmlParser { final String line = wikiTokenizer.listItemWikiText(); // This line could produce an output... + if (line.contains("ich hoan dich gear")) { + //System.out.println(); + } + // First strip the language and check whether it matches. // And hold onto it for sub-lines. final int colonIndex = line.indexOf(":"); @@ -220,16 +224,28 @@ public class EnWiktionaryXmlParser { continue; } - final String lang = line.substring(0, colonIndex); - if (!this.langPattern.matcher(lang).find()) { + final String lang = trim(WikiTokenizer.toPlainText(line.substring(0, colonIndex))); + final boolean appendLang; + if (wikiTokenizer.listItemPrefix().length() == 1) { + topLevelLang = lang; + final boolean thisFind = langPattern.matcher(lang).find(); + if (!thisFind) { + continue; + } + appendLang = !langPattern.matcher(lang).matches(); + } else if (topLevelLang == null) { continue; + } else { + // Two-level -- the only way we won't append is if this second level matches exactly. + if (!langPattern.matcher(lang).matches() && !langPattern.matcher(topLevelLang).find()) { + continue; + } + appendLang = !langPattern.matcher(lang).matches(); } String rest = line.substring(colonIndex + 1).trim(); if (rest.length() > 0) { - doTranslationLine(line, title, pos, sense, rest); - } else { - // TODO: do lines that are like "Greek:" + doTranslationLine(line, appendLang ? lang : null, title, pos, sense, rest); } } else if (wikiTokenizer.remainderStartsWith("''See''")) { @@ -258,7 +274,7 @@ public class EnWiktionaryXmlParser { return index < list.size() ? list.get(index) : null; } - private void doTranslationLine(final String line, final String title, final String pos, final String sense, final String rest) { + private void doTranslationLine(final String line, final String lang, final String title, final String pos, final String sense, final String rest) { // Good chance we'll actually file this one... final PairEntry pairEntry = new PairEntry(); final IndexedEntry indexedEntry = new IndexedEntry(pairEntry); @@ -363,6 +379,10 @@ public class EnWiktionaryXmlParser { return; } + if (lang != null) { + otherText.insert(0, "(" + lang + ") "); + } + StringBuilder englishText = new StringBuilder(); englishText.append(title); @@ -685,14 +705,16 @@ public class EnWiktionaryXmlParser { pairEntry.pairs.add(pair); } } else if (nextPrefix.equals("#::") || nextPrefix.equals("#**")) { - if (lastForeign != null) { + if (lastForeign != null && pairEntry.pairs.size() > 0) { pairEntry.pairs.remove(pairEntry.pairs.size() - 1); final Pair pair = new Pair(formatAndIndexExampleString(nextLine, enIndexBuilder, indexedEntry), formatAndIndexExampleString(lastForeign, otherIndexBuilder, indexedEntry), swap); if (pair.lang1 != "--" && pair.lang1 != "--") { pairEntry.pairs.add(pair); } + lastForeign = null; } else { - LOG.warning("English example with no foreign: " + title + ", " + nextLine); + LOG.warning("TODO: English example with no foreign: " + title + ", " + nextLine); + // TODO: add something. } } else if (nextPrefix.equals("#*")) { // Can't really index these.