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