]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/enwiktionary/EnWiktionaryXmlParser.java
Changed ordering of FormOf, handling of FormOf, handing of Encoding.
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / enwiktionary / 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.enwiktionary;
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.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.logging.Logger;
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 import com.hughes.android.dictionary.parser.WikiTokenizer;
39
40 public class EnWiktionaryXmlParser {
41   
42   static final Logger LOG = Logger.getLogger(EnWiktionaryXmlParser.class.getName());
43   
44   // TODO: process {{ttbc}} lines
45   
46   static final Pattern partOfSpeechHeader = Pattern.compile(
47       "Noun|Verb|Adjective|Adverb|Pronoun|Conjunction|Interjection|" +
48       "Preposition|Proper noun|Article|Prepositional phrase|Acronym|" +
49       "Abbreviation|Initialism|Contraction|Prefix|Suffix|Symbol|Letter|" +
50       "Ligature|Idiom|Phrase|\\{\\{acronym\\}\\}|\\{\\{initialism\\}\\}|" +
51       "\\{\\{abbreviation\\}\\}|" +
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 foreignIndexBuilder;
62   final Pattern langPattern;
63   final Pattern langCodePattern;
64   final boolean swap;
65   
66   // State used while parsing.
67   enum State {
68     TRANSLATION_LINE,
69     ENGLISH_DEF_OF_FOREIGN,
70     ENGLISH_EXAMPLE,
71     FOREIGN_EXAMPLE,
72   }
73   State state = null;
74   String title;
75
76   public EnWiktionaryXmlParser(final IndexBuilder enIndexBuilder, final IndexBuilder otherIndexBuilder, final Pattern langPattern, final Pattern langCodePattern, final boolean swap) {
77     this.enIndexBuilder = enIndexBuilder;
78     this.foreignIndexBuilder = otherIndexBuilder;
79     this.langPattern = langPattern;
80     this.langCodePattern = langCodePattern;
81     this.swap = swap;
82   }
83
84   
85   public void parse(final File file, final int pageLimit) throws IOException {
86     int pageCount = 0;
87     final DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
88     while (true) {
89       if (pageLimit >= 0 && pageCount >= pageLimit) {
90         return;
91       }
92       
93       try {
94         title = dis.readUTF();
95       } catch (EOFException e) {
96         LOG.warning("Error reading split!");
97         dis.close();
98         return;
99       }
100       final String heading = dis.readUTF();
101       final int bytesLength = dis.readInt();
102       final byte[] bytes = new byte[bytesLength];
103       dis.readFully(bytes);
104       final String text = new String(bytes, "UTF8");
105       
106       parseSection(heading, text);
107
108       ++pageCount;
109       if (pageCount % 1000 == 0) {
110         LOG.info("pageCount=" + pageCount);
111       }
112     }
113   }
114   
115   private void parseSection(String heading, final String text) {
116     if (title.startsWith("Wiktionary:") ||
117         title.startsWith("Template:") ||
118         title.startsWith("Appendix:") ||
119         title.startsWith("Category:") ||
120         title.startsWith("Index:") ||
121         title.startsWith("MediaWiki:") ||
122         title.startsWith("TransWiki:") ||
123         title.startsWith("Citations:") ||
124         title.startsWith("Concordance:") ||
125         title.startsWith("Help:")) {
126       return;
127     }
128     
129     heading = heading.replaceAll("=", "").trim(); 
130     if (heading.equals("English")) {
131       doEnglishWord(text);
132     } else if (langPattern.matcher(heading).find()){
133       doForeignWord(heading, text);
134     }
135         
136   }  // endPage()
137   
138   // -------------------------------------------------------------------------
139   
140   private void doEnglishWord(String text) {
141     
142     String pos = null;
143     int posDepth = -1;
144
145     final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
146     while (wikiTokenizer.nextToken() != null) {
147       
148       if (wikiTokenizer.isHeading()) {
149         final String headerName = wikiTokenizer.headingWikiText();
150         
151         if (wikiTokenizer.headingDepth() <= posDepth) {
152           pos = null;
153           posDepth = -1;
154         }
155         
156         if (partOfSpeechHeader.matcher(headerName).matches()) {
157           posDepth = wikiTokenizer.headingDepth();
158           pos = wikiTokenizer.headingWikiText();
159           // TODO: if we're inside the POS section, we should handle the first title line...
160           
161         } else if (headerName.equals("Translations")) {
162           if (pos == null) {
163             LOG.info("Translations without POS (but using anyway): " + title);
164           }
165           doTranslations(wikiTokenizer, pos);
166         } else if (headerName.equals("Pronunciation")) {
167           //doPronunciation(wikiLineReader);
168         }
169       } else if (wikiTokenizer.isFunction()) {
170         final String name = wikiTokenizer.functionName();
171         if (name.equals("head") && pos == null) {
172           LOG.warning("{{head}} without POS: " + title);
173         }
174       }
175     }
176   }
177   
178   final AppendAndIndexWikiCallback appendAndIndexWikiCallback = new AppendAndIndexWikiCallback(this);
179   {
180     appendAndIndexWikiCallback.functionCallbacks.putAll(FunctionCallbacksDefault.DEFAULT);
181   }
182   
183   private void doTranslations(final WikiTokenizer wikiTokenizer, final String pos) {
184     if (title.equals("absolutely")) {
185       //System.out.println();
186     }
187     
188     String topLevelLang = null;
189     String sense = null;
190     boolean done = false;
191     while (wikiTokenizer.nextToken() != null) {
192       if (wikiTokenizer.isHeading()) {
193         wikiTokenizer.returnToLineStart();
194         return;
195       }
196       if (done) {
197         continue;
198       }
199       
200       // Check whether we care about this line:
201       
202       if (wikiTokenizer.isFunction()) {
203         final String functionName = wikiTokenizer.functionName();
204         final List<String> positionArgs = wikiTokenizer.functionPositionArgs();
205         
206         if (functionName.equals("trans-top")) {
207           sense = null;
208           if (wikiTokenizer.functionPositionArgs().size() >= 1) {
209             sense = positionArgs.get(0);
210             // TODO: could emphasize words in [[brackets]] inside sense.
211             sense = WikiTokenizer.toPlainText(sense);
212             //LOG.info("Sense: " + sense);
213           }
214         } else if (functionName.equals("trans-bottom")) {
215           sense = null;
216         } else if (functionName.equals("trans-mid")) {
217         } else if (functionName.equals("trans-see")) {
218           // TODO: would also be nice...
219         } else if (functionName.startsWith("picdic")) {
220         } else if (functionName.startsWith("checktrans")) {
221           done = true;
222         } else if (functionName.startsWith("ttbc")) {
223           wikiTokenizer.nextLine();
224           // TODO: would be great to handle ttbc
225           // TODO: Check this: done = true;
226         } else {
227           LOG.warning("Unexpected translation wikifunction: " + wikiTokenizer.token() + ", title=" + title);
228         }
229       } else if (wikiTokenizer.isListItem()) {
230         final String line = wikiTokenizer.listItemWikiText();
231         // This line could produce an output...
232         
233         if (line.contains("ich hoan dich gear")) {
234           //System.out.println();
235         }
236         
237         // First strip the language and check whether it matches.
238         // And hold onto it for sub-lines.
239         final int colonIndex = line.indexOf(":");
240         if (colonIndex == -1) {
241           continue;
242         }
243         
244         final String lang = trim(WikiTokenizer.toPlainText(line.substring(0, colonIndex)));
245         final boolean appendLang;
246         if (wikiTokenizer.listItemPrefix().length() == 1) {
247           topLevelLang = lang;
248           final boolean thisFind = langPattern.matcher(lang).find();
249           if (!thisFind) {
250             continue;
251           }
252           appendLang = !langPattern.matcher(lang).matches();
253         } else if (topLevelLang == null) {
254           continue;
255         } else {
256           // Two-level -- the only way we won't append is if this second level matches exactly.
257           if (!langPattern.matcher(lang).matches() && !langPattern.matcher(topLevelLang).find()) {
258             continue;
259           }
260           appendLang = !langPattern.matcher(lang).matches();
261         }
262         
263         String rest = line.substring(colonIndex + 1).trim();
264         if (rest.length() > 0) {
265           doTranslationLine(line, appendLang ? lang : null, pos, sense, rest);
266         }
267         
268       } else if (wikiTokenizer.remainderStartsWith("''See''")) {
269         wikiTokenizer.nextLine();
270         LOG.fine("Skipping See line: " + wikiTokenizer.token());
271       } else if (wikiTokenizer.isWikiLink()) {
272         final String wikiLink = wikiTokenizer.wikiLinkText();
273         if (wikiLink.contains(":") && wikiLink.contains(title)) {
274         } else if (wikiLink.contains("Category:")) {
275         } else  {
276           LOG.warning("Unexpected wikiLink: " + wikiTokenizer.token() + ", title=" + title);
277         }
278       } else if (wikiTokenizer.isNewline() || wikiTokenizer.isMarkup() || wikiTokenizer.isComment()) {
279       } else {
280         final String token = wikiTokenizer.token();
281         if (token.equals("----")) { 
282         } else {
283           LOG.warning("Unexpected translation token: " + wikiTokenizer.token() + ", title=" + title);
284         }
285       }
286       
287     }
288   }
289   
290   private void doTranslationLine(final String line, final String lang, final String pos, final String sense, final String rest) {
291     state = State.TRANSLATION_LINE;
292     // Good chance we'll actually file this one...
293     final PairEntry pairEntry = new PairEntry();
294     final IndexedEntry indexedEntry = new IndexedEntry(pairEntry);
295     
296     final StringBuilder foreignText = new StringBuilder();
297     appendAndIndexWikiCallback.reset(foreignText, indexedEntry);
298     appendAndIndexWikiCallback.dispatch(rest, foreignIndexBuilder, EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
299     
300     if (foreignText.length() == 0) {
301       LOG.warning("Empty foreignText: " + line);
302       return;
303     }
304     
305     if (lang != null) {
306       foreignText.insert(0, String.format("(%s) ", lang));
307     }
308     
309     StringBuilder englishText = new StringBuilder();
310     
311     englishText.append(title);
312     if (sense != null) {
313       englishText.append(" (").append(sense).append(")");
314       enIndexBuilder.addEntryWithString(indexedEntry, sense, EntryTypeName.WIKTIONARY_TRANSLATION_SENSE);
315     }
316     if (pos != null) {
317       englishText.append(" (").append(pos.toLowerCase()).append(")");
318     }
319     enIndexBuilder.addEntryWithString(indexedEntry, title, EntryTypeName.WIKTIONARY_TITLE_MULTI);
320     
321     final Pair pair = new Pair(trim(englishText.toString()), trim(foreignText.toString()), swap);
322     pairEntry.pairs.add(pair);
323     if (!pairsAdded.add(pair.toString())) {
324       LOG.warning("Duplicate pair: " + pair.toString());
325     }
326   }
327
328
329   Set<String> pairsAdded = new LinkedHashSet<String>();
330   
331   // -------------------------------------------------------------------------
332   
333   private void doForeignWord(final String lang, final String text) {
334     final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
335     while (wikiTokenizer.nextToken() != null) {
336       if (wikiTokenizer.isHeading()) {
337         final String headingName = wikiTokenizer.headingWikiText();
338         if (headingName.equals("Translations")) {
339           LOG.warning("Translations not in English section: " + title);
340         } else if (headingName.equals("Pronunciation")) {
341           //doPronunciation(wikiLineReader);
342         } else if (partOfSpeechHeader.matcher(headingName).matches()) {
343           doForeignPartOfSpeech(lang, headingName, wikiTokenizer.headingDepth(), wikiTokenizer);
344         }
345       } else {
346       }
347     }
348   }
349   
350   static final class ListSection {
351     final String firstPrefix;
352     final String firstLine;
353     final List<String> nextPrefixes = new ArrayList<String>();
354     final List<String> nextLines = new ArrayList<String>();
355     
356     public ListSection(String firstPrefix, String firstLine) {
357       this.firstPrefix = firstPrefix;
358       this.firstLine = firstLine;
359     }
360
361     @Override
362     public String toString() {
363       return firstPrefix + firstLine + "{ " + nextPrefixes + "}";
364     }
365   }
366
367
368   int foreignCount = 0;
369   final Collection<String> wordForms = new ArrayList<String>();
370   boolean titleAppended = false;
371
372   private void doForeignPartOfSpeech(final String lang, String posHeading, final int posDepth, WikiTokenizer wikiTokenizer) {
373     if (++foreignCount % 1000 == 0) {
374       LOG.info("***" + lang + ", " + title + ", pos=" + posHeading + ", foreignCount=" + foreignCount);
375     }
376     if (title.equals("6")) {
377       System.out.println();
378     }
379     
380     final StringBuilder foreignBuilder = new StringBuilder();
381     final List<ListSection> listSections = new ArrayList<ListSection>();
382     
383     appendAndIndexWikiCallback.reset(foreignBuilder, null);
384     this.state = State.ENGLISH_DEF_OF_FOREIGN;  // TODO: this is wrong, need new category....
385     titleAppended = false;
386     wordForms.clear();
387     
388     try {
389     
390     ListSection lastListSection = null;
391     
392     int currentHeadingDepth = posDepth;
393     while (wikiTokenizer.nextToken() != null) {
394       if (wikiTokenizer.isHeading()) {
395         currentHeadingDepth = wikiTokenizer.headingDepth();
396         
397         if (currentHeadingDepth <= posDepth) {
398           wikiTokenizer.returnToLineStart();
399           return;
400         }
401       }
402       
403       if (currentHeadingDepth > posDepth) {
404         // TODO: deal with other neat info sections
405         continue;
406       }
407       
408       if (wikiTokenizer.isFunction()) {
409         final String name = wikiTokenizer.functionName();
410         final List<String> args = wikiTokenizer.functionPositionArgs();
411         final Map<String,String> namedArgs = wikiTokenizer.functionNamedArgs();
412         // First line is generally a repeat of the title with some extra information.
413         // We need to build up the left side (foreign text, tokens) separately from the
414         // right side (English).  The left-side may get paired with multiple right sides.
415         // The left side should get filed under every form of the word in question (singular, plural).
416         
417         // For verbs, the conjugation comes later on in a deeper section.
418         // Ideally, we'd want to file every English entry with the verb
419         // under every verb form coming from the conjugation.
420         // Ie. under "fa": see: "make :: fare" and "do :: fare"
421         // But then where should we put the conjugation table?
422         // 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!)
423         // for the conjugation table from "fa".
424         // Would like to be able to link to a lang#token.
425         
426         appendAndIndexWikiCallback.onFunction(wikiTokenizer, name, args, namedArgs);
427         
428       } else if (wikiTokenizer.isListItem()) {
429         final String prefix = wikiTokenizer.listItemPrefix();
430         if (lastListSection != null && 
431             prefix.startsWith(lastListSection.firstPrefix) && 
432             prefix.length() > lastListSection.firstPrefix.length()) {
433           lastListSection.nextPrefixes.add(prefix);
434           lastListSection.nextLines.add(wikiTokenizer.listItemWikiText());
435         } else {
436           lastListSection = new ListSection(prefix, wikiTokenizer.listItemWikiText());
437           listSections.add(lastListSection);
438         }
439       } else if (lastListSection != null) {
440         // Don't append anything after the lists, because there's crap.
441       } else if (wikiTokenizer.isWikiLink()) {
442         // Unindexed!
443         foreignBuilder.append(wikiTokenizer.wikiLinkText());
444         
445       } else if (wikiTokenizer.isPlainText()) {
446         // Unindexed!
447         foreignBuilder.append(wikiTokenizer.token());
448         
449       } else if (wikiTokenizer.isMarkup() || wikiTokenizer.isNewline() || wikiTokenizer.isComment()) {
450         // Do nothing.
451       } else {
452         LOG.warning("Unexpected token: " + wikiTokenizer.token());
453       }
454     }
455     
456     } finally {
457       // Here's where we exit.
458       // Should we make an entry even if there are no foreign list items?
459       String foreign = foreignBuilder.toString().trim();
460       if (!titleAppended && !foreign.toLowerCase().startsWith(title.toLowerCase())) {
461         foreign = String.format("%s %s", title, foreign);
462       }
463       if (!langPattern.matcher(lang).matches()) {
464         foreign = String.format("(%s) %s", lang, foreign);
465       }
466       for (final ListSection listSection : listSections) {
467         doForeignListSection(foreign, title, wordForms, listSection);
468       }
469     }
470   }
471   
472   
473   // Might only want to remove "lang" if it's equal to "zh", for example.
474   static final Set<String> USELESS_WIKI_ARGS = new LinkedHashSet<String>(
475       Arrays.asList(
476           "lang",
477           "sc",
478           "sort",
479           "cat",
480           "xs",
481           "nodot"));
482
483   public boolean entryIsFormOfSomething = false;
484
485   private void doForeignListSection(final String foreignText, String title, final Collection<String> forms, final ListSection listSection) {
486     state = State.ENGLISH_DEF_OF_FOREIGN;
487     final String prefix = listSection.firstPrefix;
488     if (prefix.length() > 1) {
489       // Could just get looser and say that any prefix longer than first is a sublist.
490       LOG.warning("Prefix too long: " + listSection);
491       return;
492     }
493     
494     final PairEntry pairEntry = new PairEntry();
495     final IndexedEntry indexedEntry = new IndexedEntry(pairEntry);
496
497     entryIsFormOfSomething = false;
498     final StringBuilder englishBuilder = new StringBuilder();
499     final String mainLine = listSection.firstLine;
500     appendAndIndexWikiCallback.reset(englishBuilder, indexedEntry);
501     appendAndIndexWikiCallback.dispatch(mainLine, enIndexBuilder, EntryTypeName.WIKTIONARY_ENGLISH_DEF);
502
503     final String english = trim(englishBuilder.toString());
504     if (english.length() > 0) {
505       final Pair pair = new Pair(english, trim(foreignText), this.swap);
506       pairEntry.pairs.add(pair);
507       foreignIndexBuilder.addEntryWithString(indexedEntry, title, entryIsFormOfSomething ? EntryTypeName.WIKTIONARY_IS_FORM_OF_SOMETHING_ELSE : EntryTypeName.WIKTIONARY_TITLE_MULTI);
508       for (final String form : forms) {
509         foreignIndexBuilder.addEntryWithString(indexedEntry, form, EntryTypeName.WIKTIONARY_INFLECTED_FORM_MULTI);
510       }
511     }
512     
513     // Do examples.
514     String lastForeign = null;
515     for (int i = 0; i < listSection.nextPrefixes.size(); ++i) {
516       final String nextPrefix = listSection.nextPrefixes.get(i);
517       final String nextLine = listSection.nextLines.get(i);
518
519       // TODO: This splitting is not sensitive to wiki code.
520       int dash = nextLine.indexOf("&mdash;");
521       int mdashLen = 7;
522       if (dash == -1) {
523         dash = nextLine.indexOf("—");
524         mdashLen = 1;
525       }
526       if (dash == -1) {
527         dash = nextLine.indexOf(" - ");
528         mdashLen = 3;
529       }
530       
531       if ((nextPrefix.equals("#:") || nextPrefix.equals("##:")) && dash != -1) {
532         final String foreignEx = nextLine.substring(0, dash);
533         final String englishEx = nextLine.substring(dash + mdashLen);
534         final Pair pair = new Pair(formatAndIndexExampleString(englishEx, enIndexBuilder, indexedEntry), formatAndIndexExampleString(foreignEx, foreignIndexBuilder, indexedEntry), swap);
535         if (pair.lang1 != "--" && pair.lang1 != "--") {
536           pairEntry.pairs.add(pair);
537         }
538         lastForeign = null;
539       } else if (nextPrefix.equals("#:") || nextPrefix.equals("##:")){
540         final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
541         lastForeign = nextLine;
542         if (pair.lang1 != "--" && pair.lang1 != "--") {
543           pairEntry.pairs.add(pair);
544         }
545       } else if (nextPrefix.equals("#::") || nextPrefix.equals("#**")) {
546         if (lastForeign != null && pairEntry.pairs.size() > 0) {
547           pairEntry.pairs.remove(pairEntry.pairs.size() - 1);
548           final Pair pair = new Pair(formatAndIndexExampleString(nextLine, enIndexBuilder, indexedEntry), formatAndIndexExampleString(lastForeign, foreignIndexBuilder, indexedEntry), swap);
549           if (pair.lang1 != "--" || pair.lang2 != "--") {
550             pairEntry.pairs.add(pair);
551           }
552           lastForeign = null;
553         } else {
554           LOG.warning("TODO: English example with no foreign: " + title + ", " + nextLine);
555           final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
556           if (pair.lang1 != "--" || pair.lang2 != "--") {
557             pairEntry.pairs.add(pair);
558           }
559         }
560       } else if (nextPrefix.equals("#*")) {
561         // Can't really index these.
562         final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
563         lastForeign = nextLine;
564         if (pair.lang1 != "--" || pair.lang2 != "--") {
565           pairEntry.pairs.add(pair);
566         }
567       } else if (nextPrefix.equals("#::*") || nextPrefix.equals("##") || nextPrefix.equals("#*:") || nextPrefix.equals("#:*") || true) {
568         final Pair pair = new Pair("--", formatAndIndexExampleString(nextLine, null, indexedEntry), swap);
569         if (pair.lang1 != "--" || pair.lang2 != "--") {
570           pairEntry.pairs.add(pair);
571         }
572 //      } else {
573 //        assert false;
574       }
575     }
576   }
577   
578   private String formatAndIndexExampleString(final String example, final IndexBuilder indexBuilder, final IndexedEntry indexedEntry) {
579     // TODO:
580 //    if (wikiTokenizer.token().equals("'''")) {
581 //      insideTripleQuotes = !insideTripleQuotes;
582 //    }
583     final StringBuilder builder = new StringBuilder();
584     appendAndIndexWikiCallback.reset(builder, indexedEntry);
585     appendAndIndexWikiCallback.entryTypeName = EntryTypeName.WIKTIONARY_EXAMPLE;
586     appendAndIndexWikiCallback.entryTypeNameSticks = true;
587     try {
588       // TODO: this is a hack needed because we don't safely split on the dash.
589       appendAndIndexWikiCallback.dispatch(example, indexBuilder, EntryTypeName.WIKTIONARY_EXAMPLE);
590     } catch (AssertionError e) {
591       return "--";
592     }
593     final String result = trim(builder.toString());
594     return result.length() > 0 ? result : "--";
595   }
596
597
598   private void itConjAre(List<String> args, Map<String, String> namedArgs) {
599     final String base = args.get(0);
600     final String aux = args.get(1);
601     
602     putIfMissing(namedArgs, "inf", base + "are");
603     putIfMissing(namedArgs, "aux", aux);
604     putIfMissing(namedArgs, "ger", base + "ando");
605     putIfMissing(namedArgs, "presp", base + "ante");
606     putIfMissing(namedArgs, "pastp", base + "ato");
607     // Present
608     putIfMissing(namedArgs, "pres1s", base + "o");
609     putIfMissing(namedArgs, "pres2s", base + "i");
610     putIfMissing(namedArgs, "pres3s", base + "a");
611     putIfMissing(namedArgs, "pres1p", base + "iamo");
612     putIfMissing(namedArgs, "pres2p", base + "ate");
613     putIfMissing(namedArgs, "pres3p", base + "ano");
614     // Imperfect
615     putIfMissing(namedArgs, "imperf1s", base + "avo");
616     putIfMissing(namedArgs, "imperf2s", base + "avi");
617     putIfMissing(namedArgs, "imperf3s", base + "ava");
618     putIfMissing(namedArgs, "imperf1p", base + "avamo");
619     putIfMissing(namedArgs, "imperf2p", base + "avate");
620     putIfMissing(namedArgs, "imperf3p", base + "avano");
621     // Passato remoto
622     putIfMissing(namedArgs, "prem1s", base + "ai");
623     putIfMissing(namedArgs, "prem2s", base + "asti");
624     putIfMissing(namedArgs, "prem3s", base + "ò");
625     putIfMissing(namedArgs, "prem1p", base + "ammo");
626     putIfMissing(namedArgs, "prem2p", base + "aste");
627     putIfMissing(namedArgs, "prem3p", base + "arono");
628     // Future
629     putIfMissing(namedArgs, "fut1s", base + "erò");
630     putIfMissing(namedArgs, "fut2s", base + "erai");
631     putIfMissing(namedArgs, "fut3s", base + "erà");
632     putIfMissing(namedArgs, "fut1p", base + "eremo");
633     putIfMissing(namedArgs, "fut2p", base + "erete");
634     putIfMissing(namedArgs, "fut3p", base + "eranno");
635     // Conditional
636     putIfMissing(namedArgs, "cond1s", base + "erei");
637     putIfMissing(namedArgs, "cond2s", base + "eresti");
638     putIfMissing(namedArgs, "cond3s", base + "erebbe");
639     putIfMissing(namedArgs, "cond1p", base + "eremmo");
640     putIfMissing(namedArgs, "cond2p", base + "ereste");
641     putIfMissing(namedArgs, "cond3p", base + "erebbero");
642     // Subjunctive / congiuntivo
643     putIfMissing(namedArgs, "sub123s", base + "i");
644     putIfMissing(namedArgs, "sub1p", base + "iamo");
645     putIfMissing(namedArgs, "sub2p", base + "iate");
646     putIfMissing(namedArgs, "sub3p", base + "ino");
647     // Imperfect subjunctive
648     putIfMissing(namedArgs, "impsub12s", base + "assi");
649     putIfMissing(namedArgs, "impsub3s", base + "asse");
650     putIfMissing(namedArgs, "impsub1p", base + "assimo");
651     putIfMissing(namedArgs, "impsub2p", base + "aste");
652     putIfMissing(namedArgs, "impsub3p", base + "assero");
653     // Imperative
654     putIfMissing(namedArgs, "imp2s", base + "a");
655     putIfMissing(namedArgs, "imp3s", base + "i");
656     putIfMissing(namedArgs, "imp1p", base + "iamo");
657     putIfMissing(namedArgs, "imp2p", base + "ate");
658     putIfMissing(namedArgs, "imp3p", base + "ino");
659
660
661     itConj(args, namedArgs);
662   }
663
664
665   private void itConj(List<String> args, Map<String, String> namedArgs) {
666     // TODO Auto-generated method stub
667     
668   }
669
670
671   private static void putIfMissing(final Map<String, String> namedArgs, final String key,
672       final String value) {
673     final String oldValue = namedArgs.get(key);
674     if (oldValue == null || oldValue.length() == 0) {
675       namedArgs.put(key, value);
676     }
677   }
678   
679   // TODO: check how ='' and =| are manifested....
680   // TODO: get this right in -are
681   private static void putOrNullify(final Map<String, String> namedArgs, final String key,
682       final String value) {
683     final String oldValue = namedArgs.get(key);
684     if (oldValue == null/* || oldValue.length() == 0*/) {
685       namedArgs.put(key, value);
686     } else {
687       if (oldValue.equals("''")) {
688         namedArgs.put(key, "");
689       }
690     }
691   }
692
693   static final Pattern whitespace = Pattern.compile("\\s+");
694   static String trim(final String s) {
695     return whitespace.matcher(s).replaceAll(" ").trim();
696   }
697
698   
699 }