]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/EnWiktionaryXmlParser.java
Reworking handling of foreign section.
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / EnWiktionaryXmlParser.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.parser;
16
17 import java.io.BufferedInputStream;
18 import java.io.DataInputStream;
19 import java.io.EOFException;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.LinkedHashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.logging.Logger;
32 import java.util.regex.Pattern;
33
34 import com.hughes.android.dictionary.engine.EntryTypeName;
35 import com.hughes.android.dictionary.engine.IndexBuilder;
36 import com.hughes.android.dictionary.engine.IndexedEntry;
37 import com.hughes.android.dictionary.engine.PairEntry;
38 import com.hughes.android.dictionary.engine.PairEntry.Pair;
39
40 public class EnWiktionaryXmlParser {
41   
42   static final Logger LOG = Logger.getLogger(EnWiktionaryXmlParser.class.getName());
43   
44   // TODO: look for {{ and [[ and <adf> <!-- in output.
45   // TODO: process {{ttbc}} lines
46   
47   static final Pattern partOfSpeechHeader = Pattern.compile(
48       "Noun|Verb|Adjective|Adverb|Pronoun|Conjunction|Interjection|" +
49       "Preposition|Proper noun|Article|Prepositional phrase|Acronym|" +
50       "Abbreviation|Initialism|Contraction|Prefix|Suffix|Symbol|Letter|" +
51       "Ligature|Idiom|Phrase|" +
52       // These are @deprecated:
53       "Noun form|Verb form|Adjective form|Nominal phrase|Noun phrase|" +
54       "Verb phrase|Transitive verb|Intransitive verb|Reflexive verb|" +
55       // These are extras I found:
56       "Determiner|Numeral|Number|Cardinal number|Ordinal number|Proverb|" +
57       "Particle|Interjection|Pronominal adverb" +
58       "Han character|Hanzi|Hanja|Kanji|Katakana character|Syllable");
59   
60   final IndexBuilder enIndexBuilder;
61   final IndexBuilder otherIndexBuilder;
62   final Pattern langPattern;
63   final Pattern langCodePattern;
64   final boolean swap;
65
66   public EnWiktionaryXmlParser(final IndexBuilder enIndexBuilder, final IndexBuilder otherIndexBuilder, final Pattern langPattern, final Pattern langCodePattern, final boolean swap) {
67     this.enIndexBuilder = enIndexBuilder;
68     this.otherIndexBuilder = otherIndexBuilder;
69     this.langPattern = langPattern;
70     this.langCodePattern = langCodePattern;
71     this.swap = swap;
72   }
73
74   
75   public void parse(final File file, final int pageLimit) throws IOException {
76     int pageCount = 0;
77     final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
78     while (true) {
79       if (pageLimit >= 0 && pageCount >= pageLimit) {
80         return;
81       }
82       
83       final String title;
84       try {
85         title = dis.readUTF();
86       } catch (EOFException e) {
87         dis.close();
88         return;
89       }
90       final String heading = dis.readUTF();
91       final int bytesLength = dis.readInt();
92       final byte[] bytes = new byte[bytesLength];
93       dis.readFully(bytes);
94       final String text = new String(bytes, "UTF8");
95       
96       parseSection(title, heading, text);
97
98       ++pageCount;
99       if (pageCount % 1000 == 0) {
100         LOG.info("pageCount=" + pageCount);
101       }
102     }
103   }
104   
105   private void parseSection(final String title, String heading, final String text) {
106     if (title.startsWith("Wiktionary:") ||
107         title.startsWith("Template:") ||
108         title.startsWith("Appendix:") ||
109         title.startsWith("Category:") ||
110         title.startsWith("Index:") ||
111         title.startsWith("MediaWiki:") ||
112         title.startsWith("TransWiki:") ||
113         title.startsWith("Citations:") ||
114         title.startsWith("Concordance:") ||
115         title.startsWith("Help:")) {
116       return;
117     }
118     
119     heading = heading.replaceAll("=", "").trim(); 
120     if (heading.equals("English")) {
121       doEnglishWord(title, text);
122     } else if (langPattern.matcher(heading).matches()){
123       doForeignWord(title, text);
124     }
125         
126   }  // endPage()
127   
128   // -------------------------------------------------------------------------
129   
130   String pos = null;
131   int posDepth = -1;
132
133   private void doEnglishWord(String title, String text) {
134     final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
135     while (wikiTokenizer.nextToken() != null) {
136       
137       if (wikiTokenizer.isHeading()) {
138         final String headerName = wikiTokenizer.headingWikiText();
139         
140         if (wikiTokenizer.headingDepth() <= posDepth) {
141           pos = null;
142           posDepth = -1;
143         }
144         
145         if (partOfSpeechHeader.matcher(headerName).matches()) {
146           posDepth = wikiTokenizer.headingDepth();
147           pos = wikiTokenizer.headingWikiText();
148         } else if (headerName.equals("Translations")) {
149           doTranslations(title, wikiTokenizer);
150         } else if (headerName.equals("Pronunciation")) {
151           //doPronunciation(wikiLineReader);
152         }
153       }
154     }
155   }
156
157
158   private static Set<String> encodings = new LinkedHashSet<String>(Arrays.asList("zh-ts",
159       "sd-Arab", "ku-Arab", "Arab", "unicode", "Laoo", "ur-Arab", "Thai", 
160       "fa-Arab", "Khmr", "zh-tsp", "Cyrl", "IPAchar", "ug-Arab", "ko-inline", 
161       "Jpan", "Kore", "Hebr", "rfscript", "Beng", "Mong", "Knda", "Cyrs",
162       "yue-tsj", "Mlym", "Tfng", "Grek", "yue-yue-j"));
163   
164   private void doTranslations(final String title, final WikiTokenizer wikiTokenizer) {
165     if (title.equals("absolutely")) {
166       System.out.println();
167     }
168     
169     String sense = null;
170     boolean done = false;
171     while (wikiTokenizer.nextToken() != null) {
172       if (wikiTokenizer.isHeading()) {
173         wikiTokenizer.returnToLineStart();
174         return;
175       }
176       if (done) {
177         continue;
178       }
179       
180       // Check whether we care about this line:
181       
182       //line = WikiLineReader.removeSquareBrackets(line);
183       
184       if (wikiTokenizer.isFunction()) {
185         final String functionName = wikiTokenizer.functionName();
186         final List<String> positionArgs = wikiTokenizer.functionPositionArgs();
187         
188         if (functionName.equals("trans-top")) {
189           sense = null;
190           if (wikiTokenizer.functionPositionArgs().size() >= 1) {
191             sense = positionArgs.get(0);
192             // TODO: could emphasize words in [[brackets]] inside sense.
193             sense = WikiTokenizer.toPlainText(sense);
194             //LOG.info("Sense: " + sense);
195           }
196         } else if (functionName.equals("trans-bottom")) {
197           sense = null;
198         } else if (functionName.equals("trans-mid")) {
199         } else if (functionName.equals("trans-see")) {
200           // TODO
201         } else if (functionName.startsWith("picdic")) {
202         } else if (functionName.startsWith("checktrans")) {
203         } else if (functionName.startsWith("ttbc")) {
204           wikiTokenizer.nextLine();
205           // TODO: would be great to handle
206           //TODO: Check this: done = true;
207         } else {
208           LOG.warning("Unexpected translation wikifunction: " + wikiTokenizer.token() + ", title=" + title);
209         }
210       } else if (wikiTokenizer.isListItem() && wikiTokenizer.listItemPrefix().startsWith("*")) {
211         final String line = wikiTokenizer.listItemWikiText();
212         // This line could produce an output...
213         
214         // First strip the language and check whether it matches.
215         // And hold onto it for sub-lines.
216         final int colonIndex = line.indexOf(":");
217         if (colonIndex == -1) {
218           continue;
219         }
220         
221         final String lang = line.substring(0, colonIndex);
222         if (!this.langPattern.matcher(lang).find()) {
223           continue;
224         }
225         
226         String rest = line.substring(colonIndex + 1).trim();
227         if (rest.length() > 0) {
228           doTranslationLine(line, title, sense, rest);
229         } else {
230           // TODO: do lines that are like Greek:
231         }
232         
233       } else if (wikiTokenizer.remainderStartsWith("''See''")) {
234         wikiTokenizer.nextLine();
235         LOG.fine("Skipping line: " + wikiTokenizer.token());
236       } else if (wikiTokenizer.isWikiLink()) {
237         final String wikiLink = wikiTokenizer.wikiLinkText();
238         if (wikiLink.contains(":") && wikiLink.contains(title)) {
239         } else if (wikiLink.contains("Category:")) {
240         } else  {
241           LOG.warning("Unexpected wikiLink: " + wikiTokenizer.token() + ", title=" + title);
242         }
243       } else if (wikiTokenizer.isNewline() || wikiTokenizer.isMarkup() || wikiTokenizer.isComment()) {
244       } else {
245         final String token = wikiTokenizer.token();
246         if (token.equals("----")) { 
247         } else {
248           LOG.warning("Unexpected translation token: " + wikiTokenizer.token() + ", title=" + title);
249         }
250       }
251       
252     }
253   }
254   
255   private static <T> T get(final List<T> list, final int index) {
256     return index < list.size() ? list.get(index) : null;
257   }
258   
259   private void doTranslationLine(final String line, final String title, final String sense, final String rest) {
260     // Good chance we'll actually file this one...
261     final PairEntry pairEntry = new PairEntry();
262     final IndexedEntry indexedEntry = new IndexedEntry(pairEntry);
263     
264     final StringBuilder otherText = new StringBuilder();
265     final WikiTokenizer wikiTokenizer = new WikiTokenizer(rest, false);
266     while (wikiTokenizer.nextToken() != null) {
267       
268       if (wikiTokenizer.isPlainText()) {
269         final String plainText = wikiTokenizer.token(); 
270         otherText.append("").append(plainText);
271         otherIndexBuilder.addEntryWithString(indexedEntry, plainText, EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
272         
273       } else if (wikiTokenizer.isWikiLink()) {
274         final String plainText = wikiTokenizer.wikiLinkText(); 
275         otherText.append("").append(plainText);
276         otherIndexBuilder.addEntryWithString(indexedEntry, plainText, EntryTypeName.WIKTIONARY_TRANSLATION_WIKI_TEXT);
277         
278       } else if (wikiTokenizer.isFunction()) {
279         final String functionName = wikiTokenizer.functionName();
280         final List<String> args = wikiTokenizer.functionPositionArgs();
281         final Map<String,String> namedArgs = wikiTokenizer.functionNamedArgs();
282         
283         if (functionName.equals("t") || functionName.equals("t+") || functionName.equals("t-") || functionName.equals("tø") || functionName.equals("apdx-t")) {
284           if (args.size() < 2) {
285             LOG.warning("{{t}} with too few args: " + line + ", title=" + title);
286             continue;
287           }
288           final String langCode = get(args, 0);
289           //if (this.langCodePattern.matcher(langCode).matches()) {
290             final String word = get(args, 1);
291             final String gender = get(args, 2);
292             final String transliteration = namedArgs.get("tr");
293             if (otherText.length() > 0) {
294               otherText.append("");
295             }
296             otherText.append(word);
297             otherIndexBuilder.addEntryWithString(indexedEntry, word, EntryTypeName.WIKTIONARY_TITLE_SINGLE, EntryTypeName.WIKTIONARY_TITLE_MULTI);
298             if (gender != null) {
299               otherText.append(String.format(" {%s}", gender));
300             }
301             if (transliteration != null) {
302               otherText.append(String.format(" (tr. %s)", transliteration));
303               otherIndexBuilder.addEntryWithString(indexedEntry, transliteration, EntryTypeName.WIKTIONARY_TRANSLITERATION);
304             }
305           //}
306         } else if (functionName.equals("qualifier")) {
307           String qualifier = args.get(0);
308           if (!namedArgs.isEmpty() || args.size() > 1) {
309             LOG.warning("weird qualifier: " + line);
310           }
311           // Unindexed!
312           otherText.append("(").append(qualifier).append(")");
313         } else if (encodings.contains(functionName)) {
314           otherText.append("").append(args.get(0));
315           otherIndexBuilder.addEntryWithString(indexedEntry, args.get(0), EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
316         } else if (isGender(functionName)) {
317           appendGender(otherText, functionName, args);
318         } else if (functionName.equals("g")) {
319           otherText.append("{g}");
320         } else if (functionName.equals("l")) {
321           // encodes text in various langs.
322           // lang is arg 0.
323           otherText.append("").append(args.get(1));
324           otherIndexBuilder.addEntryWithString(indexedEntry, args.get(1), EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
325           // TODO: transliteration
326         } else if (functionName.equals("term")) {
327           // cross-reference to another dictionary
328           otherText.append("").append(args.get(0));
329           otherIndexBuilder.addEntryWithString(indexedEntry, args.get(0), EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
330           // TODO: transliteration
331         } else if (functionName.equals("italbrac") || functionName.equals("gloss")) {
332           // TODO: put this text aside to use it.
333           otherText.append("[").append(args.get(0)).append("]");
334           otherIndexBuilder.addEntryWithString(indexedEntry, args.get(0), EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
335         } else if (functionName.equals("ttbc")) {
336           LOG.warning("Unexpected {{ttbc}}");
337         } else if (functionName.equals("trreq")) {
338         } else if (functionName.equals("not used")) {
339           otherText.append("(not used)");
340         } else if (functionName.equals("t-image")) {
341           // American sign language
342         } else {
343           // Unindexed!
344           otherText.append(wikiTokenizer.token());
345         }
346         
347       } else if (wikiTokenizer.isNewline()) {
348         assert false;
349       } else if (wikiTokenizer.isComment()) {
350       } else if (wikiTokenizer.isMarkup()) {
351       } else {
352         LOG.warning("Bad translation token: " + wikiTokenizer.token());
353       }
354     }
355     if (otherText.length() == 0) {
356       LOG.warning("Empty otherText: " + line);
357       return;
358     }
359     
360     StringBuilder englishText = new StringBuilder();
361     
362     englishText.append(title);
363     if (sense != null) {
364       englishText.append(" (").append(sense).append(")");
365       enIndexBuilder.addEntryWithString(indexedEntry, sense, EntryTypeName.WIKTIONARY_TRANSLATION_SENSE, EntryTypeName.WIKTIONARY_TRANSLATION_SENSE);
366     }
367     if (pos != null) {
368       englishText.append(" (").append(pos.toLowerCase()).append(")");
369     }
370     enIndexBuilder.addEntryWithString(indexedEntry, title, EntryTypeName.WIKTIONARY_TITLE_SINGLE, EntryTypeName.WIKTIONARY_TITLE_MULTI);
371     
372     final Pair pair = new Pair(trim(englishText.toString()), trim(otherText.toString()), swap);
373     pairEntry.pairs.add(pair);
374     if (!pairsAdded.add(pair.toString())) {
375       LOG.warning("Duplicate pair: " + pair.toString());
376     }
377     if (pair.toString().equals("libero {m} :: free (adjective)")) {
378       System.out.println();
379     }
380
381   }
382
383
384   private void appendGender(final StringBuilder otherText,
385       final String functionName, final List<String> args) {
386     otherText.append("{");
387     otherText.append(functionName);
388     for (int i = 0; i < args.size(); ++i) {
389       otherText.append("|").append(args.get(i));
390     }
391     otherText.append("}");
392   }
393
394
395   private boolean isGender(final String functionName) {
396     return functionName.equals("m") || functionName.equals("f") || functionName.equals("n") || functionName.equals("p");
397   }
398   
399   Set<String> pairsAdded = new LinkedHashSet<String>();
400   
401   // -------------------------------------------------------------------------
402   
403   private void doForeignWord(final String title, final String text) {
404     final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
405     while (wikiTokenizer.nextToken() != null) {
406       if (wikiTokenizer.isHeading()) {
407         final String headingName = wikiTokenizer.headingWikiText();
408         if (headingName.equals("Translations")) {
409           LOG.warning("Translations not in English section: " + title);
410         } else if (headingName.equals("Pronunciation")) {
411           //doPronunciation(wikiLineReader);
412         } else if (partOfSpeechHeader.matcher(headingName).matches()) {
413           doForeignPartOfSpeech(title, headingName, wikiTokenizer.headingDepth(), wikiTokenizer);
414         }
415       } else {
416       }
417     }
418   }
419   
420   static final class ListSection {
421     final String firstPrefix;
422     final String firstLine;
423     final List<String> nextPrefixes = new ArrayList<String>();
424     final List<String> nextLines = new ArrayList<String>();
425     
426     public ListSection(String firstPrefix, String firstLine) {
427       this.firstPrefix = firstPrefix;
428       this.firstLine = firstLine;
429     }
430
431     @Override
432     public String toString() {
433       return firstPrefix + firstLine + "{ " + nextPrefixes + "}";
434     }
435   }
436
437
438   private void doForeignPartOfSpeech(String title, final String posHeading, final int posDepth, WikiTokenizer wikiTokenizer) {
439     LOG.info("***" + title + ", pos=" + posHeading);
440     if (title.equals("moro")) {
441       System.out.println();
442     }
443     
444     final StringBuilder foreignBuilder = new StringBuilder();
445     Collection<String> wordForms = Collections.emptyList();
446     final List<ListSection> listSections = new ArrayList<ListSection>();
447     
448     try {
449     
450     ListSection lastListSection = null;
451     
452     int currentHeadingDepth = posDepth;
453     while (wikiTokenizer.nextToken() != null) {
454       if (wikiTokenizer.isHeading()) {
455         currentHeadingDepth = wikiTokenizer.headingDepth();
456         
457         if (currentHeadingDepth <= posDepth) {
458           wikiTokenizer.returnToLineStart();
459           return;
460         }
461       }
462       
463       if (currentHeadingDepth > posDepth) {
464         // TODO
465         continue;
466       }
467       
468       if (wikiTokenizer.isFunction()) {
469         final String name = wikiTokenizer.functionName();
470         final List<String> args = wikiTokenizer.functionPositionArgs();
471         final Map<String,String> namedArgs = wikiTokenizer.functionNamedArgs();
472         // First line is generally a repeat of the title with some extra information.
473         // We need to build up the left side (foreign text, tokens) separately from the
474         // right side (English).  The left-side may get paired with multiple right sides.
475         // The left side should get filed under every form of the word in question (singular, plural).
476         
477         // For verbs, the conjugation comes later on in a deeper section.
478         // Ideally, we'd want to file every English entry with the verb
479         // under every verb form coming from the conjugation.
480         // Ie. under "fa": see: "make :: fare" and "do :: fare"
481         // But then where should we put the conjugation table?
482         // 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!)
483         // for the conjugation table from "fa".
484         // Would like to be able to link to a lang#token.
485       if (isGender(name)) {
486         appendGender(foreignBuilder, name, args);
487       } else if (name.equals("wikipedia")) {
488         namedArgs.remove("lang");
489         if (args.size() > 1 || !namedArgs.isEmpty()) {
490           // Unindexed!
491           foreignBuilder.append(wikiTokenizer.token());
492         } else if (args.size() == 1) {
493           foreignBuilder.append(wikiTokenizer.token());
494         } else {
495           //foreignBuilder.append(title);
496         }
497       } else if (name.equals("it-noun")) {
498           assert wordForms.isEmpty();
499           final String base = get(args, 0);
500           final String gender = get(args, 1);
501           final String singular = base + get(args, 2);
502           final String plural = base + get(args, 3);
503           foreignBuilder.append(String.format("%s {%s}, %s {pl}", singular, gender, plural, plural));
504           wordForms = Arrays.asList(singular, plural);
505         } else if (name.equals("it-proper noun")) {
506           foreignBuilder.append(wikiTokenizer.token());
507         } else if (name.equals("it-adj")) {
508           foreignBuilder.append(wikiTokenizer.token());
509         } else if (name.startsWith("it-conj")) {
510           if (name.equals("it-conj-are")) {
511             itConjAre(args, namedArgs);
512           } else if (name.equals("it-conj-ere")) {
513           } else if (name.equals("it-conj-ire")) {
514           } else {
515             LOG.warning("Unknown conjugation: " + wikiTokenizer.token());
516           }
517         } else {
518           // Unindexed!
519           foreignBuilder.append(wikiTokenizer.token());
520           // LOG.warning("Unknown function: " + wikiTokenizer.token());
521         }
522         
523       } else if (wikiTokenizer.isListItem()) {
524         final String prefix = wikiTokenizer.listItemPrefix();
525         if (lastListSection != null && 
526             prefix.startsWith(lastListSection.firstPrefix) && 
527             prefix.length() > lastListSection.firstPrefix.length()) {
528           lastListSection.nextPrefixes.add(prefix);
529           lastListSection.nextLines.add(wikiTokenizer.listItemWikiText());
530         } else {
531           lastListSection = new ListSection(prefix, wikiTokenizer.listItemWikiText());
532           listSections.add(lastListSection);
533         }
534       } else if (lastListSection != null) {
535         // Don't append anything after the lists, because there's crap.
536       } else if (wikiTokenizer.isWikiLink()) {
537         // Unindexed!
538         foreignBuilder.append(wikiTokenizer.wikiLinkText());
539         
540       } else if (wikiTokenizer.isPlainText()) {
541         // Unindexed!
542         foreignBuilder.append(wikiTokenizer.token());
543         
544       } else if (wikiTokenizer.isMarkup() || wikiTokenizer.isNewline() || wikiTokenizer.isComment()) {
545         // Do nothing.
546       } else {
547         LOG.warning("Unexpected token: " + wikiTokenizer.token());
548       }
549     }
550     
551     } finally {
552       // Here's where we exit.
553       // TODO: Should we make an entry even if there are no foreign list items?
554       if (foreignBuilder.indexOf(title) == -1) {
555         foreignBuilder.insert(0, title + " ");
556       }
557       for (final ListSection listSection : listSections) {
558         doForeignListItem(foreignBuilder.toString(), title, wordForms, listSection);
559       }
560     }
561   }
562   
563   
564   static final Pattern UNINDEXED_WIKI_TEXT = Pattern.compile(
565       "(first|second|third)-person (singular|plural)|" +
566       "present tense|" +
567       "imperative"
568       );
569
570
571   private void doForeignListItem(final String foreignText, String title, final Collection<String> forms, final ListSection listSection) {
572     
573     final String prefix = listSection.firstPrefix;
574     if (prefix.length() > 1) {
575       LOG.warning("Prefix too long: " + listSection);
576       return;
577     }
578     
579     final PairEntry pairEntry = new PairEntry();
580     final IndexedEntry indexedEntry = new IndexedEntry(pairEntry);
581     
582     final StringBuilder englishBuilder = new StringBuilder();
583
584     final String mainLine = listSection.firstLine;
585     
586     final WikiTokenizer englishTokenizer = new WikiTokenizer(mainLine, false);
587     while (englishTokenizer.nextToken() != null) {
588       // TODO handle form of....
589       if (englishTokenizer.isPlainText()) {
590         englishBuilder.append(englishTokenizer.token());
591         enIndexBuilder.addEntryWithString(indexedEntry, englishTokenizer.token(), EntryTypeName.WIKTIONARY_ENGLISH_DEF);
592       } else if (englishTokenizer.isWikiLink()) {
593         final String text = englishTokenizer.wikiLinkText();
594         final String link = englishTokenizer.wikiLinkDest();
595         if (link != null) {
596           if (link.contains("#English")) {
597             englishBuilder.append(text);
598             enIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
599           } else if (link.contains("#") && this.langPattern.matcher(link).find()) {
600             englishBuilder.append(text);
601             otherIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_OTHER_LANG);
602           } else {
603             LOG.warning("Special link: " + englishTokenizer.token());
604             // TODO: something here...
605           }
606         } else {
607           // link == null
608           englishBuilder.append(text);
609           if (!UNINDEXED_WIKI_TEXT.matcher(text).find()) {
610             enIndexBuilder.addEntryWithString(indexedEntry, text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
611           }
612         }
613       } else if (englishTokenizer.isFunction()) {
614         final String name = englishTokenizer.functionName();
615         if (name.contains(" conjugation of ") || 
616             name.contains(" form of ") || 
617             name.contains(" feminine of ") || 
618             name.contains(" plural of ")) {
619           // Ignore these in the index, they're really annoying....
620           englishBuilder.append(englishTokenizer.token());
621         } else {
622           LOG.warning("Unexpected function: " + englishTokenizer.token());
623         }
624       } else {
625         if (englishTokenizer.isComment() || englishTokenizer.isMarkup()) {
626         } else {
627           LOG.warning("Unexpected definition text: " + englishTokenizer.token());
628         }
629       }
630     }
631     final String english = trim(englishBuilder.toString());
632     if (english.length() > 0) {
633       final Pair pair = new Pair(english, trim(foreignText), this.swap);
634       pairEntry.pairs.add(pair);
635       otherIndexBuilder.addEntryWithString(indexedEntry, title, EntryTypeName.WIKTIONARY_TITLE_SINGLE, EntryTypeName.WIKTIONARY_TITLE_MULTI);
636       for (final String form : forms) {
637         otherIndexBuilder.addEntryWithString(indexedEntry, form, EntryTypeName.WIKTIONARY_FORM_SINGLE, EntryTypeName.WIKTIONARY_FORM_MULTI);
638       }
639     }
640   }
641
642
643   private void itConjAre(List<String> args, Map<String, String> namedArgs) {
644     final String base = args.get(0);
645     final String aux = args.get(1);
646     
647     putIfMissing(namedArgs, "inf", base + "are");
648     putIfMissing(namedArgs, "aux", aux);
649     putIfMissing(namedArgs, "ger", base + "ando");
650     putIfMissing(namedArgs, "presp", base + "ante");
651     putIfMissing(namedArgs, "pastp", base + "ato");
652     // Present
653     putIfMissing(namedArgs, "pres1s", base + "o");
654     putIfMissing(namedArgs, "pres2s", base + "i");
655     putIfMissing(namedArgs, "pres3s", base + "a");
656     putIfMissing(namedArgs, "pres1p", base + "iamo");
657     putIfMissing(namedArgs, "pres2p", base + "ate");
658     putIfMissing(namedArgs, "pres3p", base + "ano");
659     // Imperfect
660     putIfMissing(namedArgs, "imperf1s", base + "avo");
661     putIfMissing(namedArgs, "imperf2s", base + "avi");
662     putIfMissing(namedArgs, "imperf3s", base + "ava");
663     putIfMissing(namedArgs, "imperf1p", base + "avamo");
664     putIfMissing(namedArgs, "imperf2p", base + "avate");
665     putIfMissing(namedArgs, "imperf3p", base + "avano");
666     // Passato remoto
667     putIfMissing(namedArgs, "prem1s", base + "ai");
668     putIfMissing(namedArgs, "prem2s", base + "asti");
669     putIfMissing(namedArgs, "prem3s", base + "ò");
670     putIfMissing(namedArgs, "prem1p", base + "ammo");
671     putIfMissing(namedArgs, "prem2p", base + "aste");
672     putIfMissing(namedArgs, "prem3p", base + "arono");
673     // Future
674     putIfMissing(namedArgs, "fut1s", base + "erò");
675     putIfMissing(namedArgs, "fut2s", base + "erai");
676     putIfMissing(namedArgs, "fut3s", base + "erà");
677     putIfMissing(namedArgs, "fut1p", base + "eremo");
678     putIfMissing(namedArgs, "fut2p", base + "erete");
679     putIfMissing(namedArgs, "fut3p", base + "eranno");
680     // Conditional
681     putIfMissing(namedArgs, "cond1s", base + "erei");
682     putIfMissing(namedArgs, "cond2s", base + "eresti");
683     putIfMissing(namedArgs, "cond3s", base + "erebbe");
684     putIfMissing(namedArgs, "cond1p", base + "eremmo");
685     putIfMissing(namedArgs, "cond2p", base + "ereste");
686     putIfMissing(namedArgs, "cond3p", base + "erebbero");
687     // Subjunctive / congiuntivo
688     putIfMissing(namedArgs, "sub123s", base + "i");
689     putIfMissing(namedArgs, "sub1p", base + "iamo");
690     putIfMissing(namedArgs, "sub2p", base + "iate");
691     putIfMissing(namedArgs, "sub3p", base + "ino");
692     // Imperfect subjunctive
693     putIfMissing(namedArgs, "impsub12s", base + "assi");
694     putIfMissing(namedArgs, "impsub3s", base + "asse");
695     putIfMissing(namedArgs, "impsub1p", base + "assimo");
696     putIfMissing(namedArgs, "impsub2p", base + "aste");
697     putIfMissing(namedArgs, "impsub3p", base + "assero");
698     // Imperative
699     putIfMissing(namedArgs, "imp2s", base + "a");
700     putIfMissing(namedArgs, "imp3s", base + "i");
701     putIfMissing(namedArgs, "imp1p", base + "iamo");
702     putIfMissing(namedArgs, "imp2p", base + "ate");
703     putIfMissing(namedArgs, "imp3p", base + "ino");
704
705
706     itConj(args, namedArgs);
707   }
708
709
710   private void itConj(List<String> args, Map<String, String> namedArgs) {
711     // TODO Auto-generated method stub
712     
713   }
714
715
716   private static void putIfMissing(final Map<String, String> namedArgs, final String key,
717       final String value) {
718     final String oldValue = namedArgs.get(key);
719     if (oldValue == null || oldValue.length() == 0) {
720       namedArgs.put(key, value);
721     }
722   }
723   
724   // TODO: check how ='' and =| are manifested....
725   // TODO: get this right in -are
726   private static void putOrNullify(final Map<String, String> namedArgs, final String key,
727       final String value) {
728     final String oldValue = namedArgs.get(key);
729     if (oldValue == null/* || oldValue.length() == 0*/) {
730       namedArgs.put(key, value);
731     } else {
732       if (oldValue.equals("''")) {
733         namedArgs.put(key, "");
734       }
735     }
736   }
737
738   static final Pattern whitespace = Pattern.compile("\\s+");
739   static String trim(final String s) {
740     return whitespace.matcher(s).replaceAll(" ").trim();
741   }
742
743   
744 }