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