public class EnWiktionaryXmlParser {
+ private static final String TRANSLITERATION_FORMAT = " (tr. %s)";
+
static final Logger LOG = Logger.getLogger(EnWiktionaryXmlParser.class.getName());
// TODO: process {{ttbc}} lines
"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|" +
heading = heading.replaceAll("=", "").trim();
if (heading.equals("English")) {
doEnglishWord(title, text);
- } else if (langPattern.matcher(heading).matches()){
- doForeignWord(title, text);
+ } else if (langPattern.matcher(heading).find()){
+ doForeignWord(heading, title, text);
}
} // endPage()
if (partOfSpeechHeader.matcher(headerName).matches()) {
posDepth = wikiTokenizer.headingDepth();
pos = wikiTokenizer.headingWikiText();
+ // TODO: if we're inside the POS section, we should handle the first title line...
+
} else if (headerName.equals("Translations")) {
if (pos == null) {
LOG.warning("Translations without POS: " + title);
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) {
// Check whether we care about this line:
- //line = WikiLineReader.removeSquareBrackets(line);
-
if (wikiTokenizer.isFunction()) {
final String functionName = wikiTokenizer.functionName();
final List<String> positionArgs = wikiTokenizer.functionPositionArgs();
// 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
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(":");
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''")) {
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);
otherText.append(String.format(" {%s}", gender));
}
if (transliteration != null) {
- otherText.append(String.format(" (tr. %s)", transliteration));
+ otherText.append(String.format(TRANSLITERATION_FORMAT, transliteration));
otherIndexBuilder.addEntryWithString(indexedEntry, transliteration, EntryTypeName.WIKTIONARY_TRANSLITERATION);
}
//}
} else if (functionName.equals("qualifier")) {
- String qualifier = args.get(0);
- if (!namedArgs.isEmpty() || args.size() > 1) {
- LOG.warning("weird qualifier: " + line);
+ if (args.size() == 0) {
+ otherText.append(wikiTokenizer.token());
+ } else {
+ String qualifier = args.get(0);
+ if (!namedArgs.isEmpty() || args.size() > 1) {
+ LOG.warning("weird qualifier: " + line);
+ }
+ // Unindexed!
+ otherText.append("(").append(qualifier).append(")");
}
- // Unindexed!
- otherText.append("(").append(qualifier).append(")");
} else if (encodings.contains(functionName)) {
otherText.append("").append(args.get(0));
otherIndexBuilder.addEntryWithString(indexedEntry, args.get(0), EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
return;
}
+ if (lang != null) {
+ otherText.insert(0, String.format("(%s) ", lang));
+ }
+
StringBuilder englishText = new StringBuilder();
englishText.append(title);
// -------------------------------------------------------------------------
- private void doForeignWord(final String title, final String text) {
+ private void doForeignWord(final String lang, final String title, final String text) {
final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
while (wikiTokenizer.nextToken() != null) {
if (wikiTokenizer.isHeading()) {
} else if (headingName.equals("Pronunciation")) {
//doPronunciation(wikiLineReader);
} else if (partOfSpeechHeader.matcher(headingName).matches()) {
- doForeignPartOfSpeech(title, headingName, wikiTokenizer.headingDepth(), wikiTokenizer);
+ doForeignPartOfSpeech(lang, title, headingName, wikiTokenizer.headingDepth(), wikiTokenizer);
}
} else {
}
int foreignCount = 0;
- private void doForeignPartOfSpeech(String title, final String posHeading, final int posDepth, WikiTokenizer wikiTokenizer) {
+ private void doForeignPartOfSpeech(final String lang, String title, final String posHeading, final int posDepth, WikiTokenizer wikiTokenizer) {
if (++foreignCount % 1000 == 0) {
- LOG.info("***" + title + ", pos=" + posHeading + ", foreignCount=" + foreignCount);
+ LOG.info("***" + lang + ", " + title + ", pos=" + posHeading + ", foreignCount=" + foreignCount);
}
if (title.equals("moro")) {
System.out.println();
} else {
//foreignBuilder.append(title);
}
+ } else if (name.equals("attention") || name.equals("zh-attention")) {
+ // See: http://en.wiktionary.org/wiki/Template:attention
+ // Ignore these.
+ } else if (name.equals("infl")) {
+ // See: http://en.wiktionary.org/wiki/Template:infl
+ final String langCode = get(args, 0);
+ namedArgs.remove("sc");
+ final String tr = namedArgs.remove("tr");
+ final String g = namedArgs.remove("g");
+ final String g2 = namedArgs.remove("g2");
+ final String g3 = namedArgs.remove("g3");
+ if (!namedArgs.isEmpty()) {
+ LOG.warning("Didn't parse infl: " + wikiTokenizer.token());
+ foreignBuilder.append(wikiTokenizer.token());
+ } else {
+ String head = namedArgs.get("head");
+ if (head == null) {
+ head = title;
+ } else {
+ head = WikiTokenizer.toPlainText(head);
+ }
+ foreignBuilder.append(head);
+
+ if (g != null) {
+ foreignBuilder.append(" {").append(g);
+ if (g2 != null) {
+ foreignBuilder.append("|").append(g2);
+ }
+ if (g3 != null) {
+ foreignBuilder.append("|").append(g3);
+ }
+ foreignBuilder.append("}");
+ }
+
+ if (tr != null) {
+ foreignBuilder.append(String.format(TRANSLITERATION_FORMAT, tr));
+ wordForms.add(tr);
+ }
+
+ final String pos = get(args, 1);
+ if (pos != null) {
+ foreignBuilder.append(" (").append(pos).append(")");
+ }
+ for (int i = 2; i < args.size(); i += 2) {
+ final String inflName = get(args, i);
+ final String inflValue = get(args, i + 1);
+ foreignBuilder.append(", ").append(WikiTokenizer.toPlainText(inflName));
+ if (inflValue != null && inflValue.length() > 0) {
+ foreignBuilder.append(": ").append(WikiTokenizer.toPlainText(inflValue));
+ wordForms.add(inflValue);
+ }
+ }
+ }
} else if (name.equals("it-noun")) {
final String base = get(args, 0);
final String gender = get(args, 1);
// Should we make an entry even if there are no foreign list items?
String foreign = foreignBuilder.toString().trim();
if (!foreign.toLowerCase().startsWith(title.toLowerCase())) {
- foreign = title + " " + foreign;
+ foreign = String.format("%s %s", title, foreign);
+ }
+ if (!langPattern.matcher(lang).matches()) {
+ foreign = String.format("(%s) %s", lang, foreign);
}
for (final ListSection listSection : listSections) {
doForeignListItem(foreign, title, wordForms, listSection);
final String prefix = listSection.firstPrefix;
if (prefix.length() > 1) {
+ // Could just get looser and say that any prefix longer than first is a sublist.
LOG.warning("Prefix too long: " + listSection);
return;
}
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 != "--") {
+ if (pair.lang1 != "--" || pair.lang2 != "--") {
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);
+ final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
+ if (pair.lang1 != "--" || pair.lang2 != "--") {
+ pairEntry.pairs.add(pair);
+ }
}
} else if (nextPrefix.equals("#*")) {
// Can't really index these.
final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
lastForeign = nextLine;
- if (pair.lang1 != "--" && pair.lang1 != "--") {
+ if (pair.lang1 != "--" || pair.lang2 != "--") {
pairEntry.pairs.add(pair);
}
} else if (nextPrefix.equals("#::*") || nextPrefix.equals("##") || nextPrefix.equals("#*:") || nextPrefix.equals("#:*") || true) {
final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
- if (pair.lang1 != "--" && pair.lang1 != "--") {
+ if (pair.lang1 != "--" || pair.lang2 != "--") {
pairEntry.pairs.add(pair);
}
// } else {