+ if (currentHeadingDepth > posDepth) {
+ // TODO: deal with other neat info sections
+ continue;
+ }
+
+ if (wikiTokenizer.isFunction()) {
+ final String name = wikiTokenizer.functionName();
+ final List<String> args = wikiTokenizer.functionPositionArgs();
+ final Map<String,String> namedArgs = wikiTokenizer.functionNamedArgs();
+ // First line is generally a repeat of the title with some extra information.
+ // We need to build up the left side (foreign text, tokens) separately from the
+ // right side (English). The left-side may get paired with multiple right sides.
+ // The left side should get filed under every form of the word in question (singular, plural).
+
+ // For verbs, the conjugation comes later on in a deeper section.
+ // Ideally, we'd want to file every English entry with the verb
+ // under every verb form coming from the conjugation.
+ // Ie. under "fa": see: "make :: fare" and "do :: fare"
+ // But then where should we put the conjugation table?
+ // I think just under fare. But then we need a way to link to the entry (actually the row, since entries doesn't show up!)
+ // for the conjugation table from "fa".
+ // Would like to be able to link to a lang#token.
+ if (isGender(name)) {
+ appendGender(foreignBuilder, name, args);
+ } else if (name.equals("wikipedia")) {
+ namedArgs.remove("lang");
+ if (args.size() > 1 || !namedArgs.isEmpty()) {
+ // Unindexed!
+ foreignBuilder.append(wikiTokenizer.token());
+ } else if (args.size() == 1) {
+ foreignBuilder.append(wikiTokenizer.token());
+ } else {
+ //foreignBuilder.append(title);
+ }
+ } else if (name.equals("attention") || name.equals("zh-attention")) {
+ // See: http://en.wiktionary.org/wiki/Template:attention
+ // Ignore these.
+ // TODO: head } else if (name.equals("head")) {
+ } else if (name.equals("infl")) {
+ // See: http://en.wiktionary.org/wiki/Template:infl
+ final String langCode = get(args, 0);
+ String head = namedArgs.remove("head");
+ if (head == null) {
+ head = namedArgs.remove("title"); // Bug
+ }
+ if (head == null) {
+ head = title;
+ } else {
+ head = WikiTokenizer.toPlainText(head);
+ }
+ titleAppended = true;
+
+ namedArgs.keySet().removeAll(USELESS_WIKI_ARGS);
+
+ final String tr = namedArgs.remove("tr");
+ String g = namedArgs.remove("g");
+ if (g == null) {
+ g = namedArgs.remove("gender");
+ }
+ final String g2 = namedArgs.remove("g2");
+ final String g3 = namedArgs.remove("g3");
+
+ 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);
+ }
+ }
+ for (final String key : namedArgs.keySet()) {
+ final String value = WikiTokenizer.toPlainText(namedArgs.get(key));
+ foreignBuilder.append(" ").append(key).append("=").append(value);
+ wordForms.add(value);
+ }
+ } else if (name.equals("it-noun")) {
+ titleAppended = true;
+ final String base = get(args, 0);
+ final String gender = get(args, 1);
+ final String singular = base + get(args, 2);
+ final String plural = base + get(args, 3);
+ foreignBuilder.append(String.format(" %s {%s}, %s {pl}", singular, gender, plural, plural));
+ wordForms.add(singular);
+ wordForms.add(plural);
+ if (!namedArgs.isEmpty() || args.size() > 4) {
+ LOG.warning("Invalid it-noun: " + wikiTokenizer.token());
+ }
+ } else if (name.equals("it-proper noun")) {
+ foreignBuilder.append(wikiTokenizer.token());
+ } else if (name.equals("it-adj")) {
+ foreignBuilder.append(wikiTokenizer.token());
+ } else if (name.startsWith("it-conj")) {
+ if (name.equals("it-conj-are")) {
+ itConjAre(args, namedArgs);
+ } else if (name.equals("it-conj-ere")) {
+ } else if (name.equals("it-conj-ire")) {
+ } else {
+ LOG.warning("Unknown conjugation: " + wikiTokenizer.token());
+ }
+ } else {
+ // Unindexed!
+ foreignBuilder.append(wikiTokenizer.token());
+ // LOG.warning("Unknown function: " + wikiTokenizer.token());
+ }
+ } else if (wikiTokenizer.isListItem()) {
+ final String prefix = wikiTokenizer.listItemPrefix();
+ if (lastListSection != null &&
+ prefix.startsWith(lastListSection.firstPrefix) &&
+ prefix.length() > lastListSection.firstPrefix.length()) {
+ lastListSection.nextPrefixes.add(prefix);
+ lastListSection.nextLines.add(wikiTokenizer.listItemWikiText());
+ } else {
+ lastListSection = new ListSection(prefix, wikiTokenizer.listItemWikiText());
+ listSections.add(lastListSection);
+ }
+ } else if (lastListSection != null) {
+ // Don't append anything after the lists, because there's crap.
+ } else if (wikiTokenizer.isWikiLink()) {
+ // Unindexed!
+ foreignBuilder.append(wikiTokenizer.wikiLinkText());
+
+ } else if (wikiTokenizer.isPlainText()) {
+ // Unindexed!
+ foreignBuilder.append(wikiTokenizer.token());
+
+ } else if (wikiTokenizer.isMarkup() || wikiTokenizer.isNewline() || wikiTokenizer.isComment()) {
+ // Do nothing.
+ } else {
+ LOG.warning("Unexpected token: " + wikiTokenizer.token());
+ }
+ }
+
+ } finally {
+ // Here's where we exit.
+ // Should we make an entry even if there are no foreign list items?
+ String foreign = foreignBuilder.toString().trim();
+ if (!titleAppended && !foreign.toLowerCase().startsWith(title.toLowerCase())) {
+ 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);
+ }
+ }
+ }
+
+
+ static final Pattern UNINDEXED_WIKI_TEXT = Pattern.compile(
+ "(first|second|third)-person (singular|plural)|" +
+ "present tense|" +
+ "imperative"
+ );
+
+ // Might only want to remove "lang" if it's equal to "zh", for example.
+ static final Set<String> USELESS_WIKI_ARGS = new LinkedHashSet<String>(Arrays.asList("lang", "sc", "sort", "cat"));
+
+ private void doForeignListItem(final String foreignText, String title, final Collection<String> forms, final ListSection 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;
+ }
+
+ final PairEntry pairEntry = new PairEntry();
+ final IndexedEntry indexedEntry = new IndexedEntry(pairEntry);
+
+ final StringBuilder englishBuilder = new StringBuilder();
+
+ final String mainLine = listSection.firstLine;
+ final WikiTokenizer englishTokenizer = new WikiTokenizer(mainLine, false);
+ while (englishTokenizer.nextToken() != null) {
+ // TODO handle form of....
+ if (englishTokenizer.isPlainText()) {
+ englishBuilder.append(englishTokenizer.token());
+ enIndexBuilder.addEntryWithString(indexedEntry, englishTokenizer.token(), EntryTypeName.WIKTIONARY_ENGLISH_DEF);
+ } else if (englishTokenizer.isWikiLink()) {
+ final String text = englishTokenizer.wikiLinkText();
+ final String link = englishTokenizer.wikiLinkDest();
+ if (link != null) {
+ if (link.contains("#English")) {
+ englishBuilder.append(text);
+ enIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
+ } else if (link.contains("#") && this.langPattern.matcher(link).find()) {
+ englishBuilder.append(text);
+ foreignIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_OTHER_LANG);
+ } else if (link.equals("plural")) {
+ englishBuilder.append(text);
+ } else {
+ //LOG.warning("Special link: " + englishTokenizer.token());
+ enIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
+ englishBuilder.append(text);
+ }
+ } else {
+ // link == null
+ englishBuilder.append(text);
+ if (!UNINDEXED_WIKI_TEXT.matcher(text).find()) {
+ enIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
+ }
+ }
+ } else if (englishTokenizer.isFunction()) {
+ final String name = englishTokenizer.functionName();
+ final List<String> args = englishTokenizer.functionPositionArgs();
+ final Map<String,String> namedArgs = englishTokenizer.functionNamedArgs();
+
+ if (
+ name.equals("form of") ||
+ name.contains("conjugation of") ||
+ name.contains("participle of") ||
+ name.contains("gerund of") ||
+ name.contains("feminine of") ||
+ name.contains("plural of")) {
+ String formName = name;
+ if (name.equals("form of")) {
+ formName = remove(args, 0, null);
+ }
+ if (formName == null) {
+ LOG.warning("Missing form name: " + title);
+ formName = "form of";
+ }
+ String baseForm = get(args, 1, "");
+ if ("".equals(baseForm)) {
+ baseForm = get(args, 0, null);
+ remove(args, 1, "");
+ } else {
+ remove(args, 0, null);
+ }
+ namedArgs.keySet().removeAll(USELESS_WIKI_ARGS);
+ WikiTokenizer.appendFunction(englishBuilder.append("{"), formName, args, namedArgs).append("}");
+ if (baseForm != null) {
+ foreignIndexBuilder.addEntryWithString(indexedEntry, baseForm, EntryTypeName.WIKTIONARY_BASE_FORM_SINGLE, EntryTypeName.WIKTIONARY_BASE_FORM_MULTI);
+ } else {
+ // null baseForm happens in Danish.
+ LOG.warning("Null baseform: " + title);
+ }
+// } else if (name.equals("defn")) {
+ // TODO: test me!
+ // Do nothing.
+ // http://en.wiktionary.org/wiki/Wiktionary:Requests_for_deletion/Others#Template:defn
+ // Redundant, used for the same purpose as {{rfdef}}, but this
+ // doesn't produce the "This word needs a definition" text.
+ // Delete or redirect.
+ } else {
+ namedArgs.keySet().removeAll(USELESS_WIKI_ARGS);
+ if (args.size() == 0 && namedArgs.isEmpty()) {
+ englishBuilder.append("{").append(name).append("}");
+ } else {
+ WikiTokenizer.appendFunction(englishBuilder.append("{{"), name, args, namedArgs).append("}}");
+ }
+// LOG.warning("Unexpected function: " + englishTokenizer.token());
+ }
+ } else {
+ if (englishTokenizer.isComment() || englishTokenizer.isMarkup()) {
+ } else {
+ LOG.warning("Unexpected definition type: " + englishTokenizer.token());
+ }
+ }
+ }
+
+ final String english = trim(englishBuilder.toString());
+ if (english.length() > 0) {
+ final Pair pair = new Pair(english, trim(foreignText), this.swap);
+ pairEntry.pairs.add(pair);
+ foreignIndexBuilder.addEntryWithString(indexedEntry, title, EntryTypeName.WIKTIONARY_TITLE_SINGLE, EntryTypeName.WIKTIONARY_TITLE_MULTI);
+ for (final String form : forms) {
+ foreignIndexBuilder.addEntryWithString(indexedEntry, form, EntryTypeName.WIKTIONARY_INFLECTD_FORM_SINGLE, EntryTypeName.WIKTIONARY_INFLECTED_FORM_MULTI);
+ }
+ }
+
+ // Do examples.
+ String lastForeign = null;
+ for (int i = 0; i < listSection.nextPrefixes.size(); ++i) {
+ final String nextPrefix = listSection.nextPrefixes.get(i);
+ final String nextLine = listSection.nextLines.get(i);
+ int dash = nextLine.indexOf("—");
+ int mdashLen = 7;
+ if (dash == -1) {
+ dash = nextLine.indexOf("—");
+ mdashLen = 1;
+ }
+ if (dash == -1) {
+ dash = nextLine.indexOf(" - ");
+ mdashLen = 3;
+ }