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