]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/parser/wiktionary/EnParser.java
Don't handle it-conj in EnParser.
[DictionaryPC.git] / src / com / hughes / android / dictionary / parser / wiktionary / EnParser.java
1 // Copyright 2012 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.wiktionary;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.LinkedHashSet;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.regex.Pattern;
24
25 import com.hughes.android.dictionary.engine.EntryTypeName;
26 import com.hughes.android.dictionary.engine.IndexBuilder;
27 import com.hughes.android.dictionary.parser.WikiTokenizer;
28
29 public abstract class EnParser extends AbstractWiktionaryParser {
30
31   // TODO: process {{ttbc}} lines
32   
33   static final Pattern partOfSpeechHeader = Pattern.compile(
34       "Noun|Verb|Adjective|Adverb|Pronoun|Conjunction|Interjection|" +
35       "Preposition|Proper noun|Article|Prepositional phrase|Acronym|" +
36       "Abbreviation|Initialism|Contraction|Prefix|Suffix|Symbol|Letter|" +
37       "Ligature|Idiom|Phrase|\\{\\{acronym\\}\\}|\\{\\{initialism\\}\\}|" +
38       "\\{\\{abbreviation\\}\\}|" +
39       // These are @deprecated:
40       "Noun form|Verb form|Adjective form|Nominal phrase|Noun phrase|" +
41       "Verb phrase|Transitive verb|Intransitive verb|Reflexive verb|" +
42       // These are extras I found:
43       "Determiner|Numeral|Number|Cardinal number|Ordinal number|Proverb|" +
44       "Particle|Interjection|Pronominal adverb" +
45       "Han character|Hanzi|Hanja|Kanji|Katakana character|Syllable");
46   
47   // Might only want to remove "lang" if it's equal to "zh", for example.
48   static final Set<String> USELESS_WIKI_ARGS = new LinkedHashSet<String>(
49       Arrays.asList(
50           "lang",
51           "sc",
52           "sort",
53           "cat",
54           "cat2",
55           "xs",
56           "nodot"));
57
58   static boolean isIgnorableTitle(final String title) {
59     return title.startsWith("Wiktionary:") ||
60         title.startsWith("Template:") ||
61         title.startsWith("Appendix:") ||
62         title.startsWith("Category:") ||
63         title.startsWith("Index:") ||
64         title.startsWith("MediaWiki:") ||
65         title.startsWith("TransWiki:") ||
66         title.startsWith("Citations:") ||
67         title.startsWith("Concordance:") ||
68         title.startsWith("Help:");
69   }
70   
71   final IndexBuilder enIndexBuilder;
72   final IndexBuilder foreignIndexBuilder;
73   final Pattern langPattern;
74   final Pattern langCodePattern;
75   final boolean swap;
76   
77   // State used while parsing.
78   enum State {
79     TRANSLATION_LINE,
80     ENGLISH_DEF_OF_FOREIGN,
81     ENGLISH_EXAMPLE,
82     FOREIGN_EXAMPLE,
83   }
84   State state = null;
85
86   public boolean entryIsFormOfSomething = false;
87   final Collection<String> wordForms = new ArrayList<String>();
88   boolean titleAppended = false;
89
90
91   final AppendAndIndexWikiCallback<EnParser> appendAndIndexWikiCallback = new AppendAndIndexCallback(this);
92   {
93     appendAndIndexWikiCallback.functionCallbacks.putAll(EnFunctionCallbacks.DEFAULT);
94     for (final String key : new ArrayList<String>(appendAndIndexWikiCallback.functionCallbacks.keySet())) {
95         // Don't handle the it-conj functions here.
96         if (key.startsWith("it-conj")) {
97             appendAndIndexWikiCallback.functionCallbacks.remove(key);
98         }
99     }
100   }
101   
102   EnParser(final IndexBuilder enIndexBuilder, final IndexBuilder otherIndexBuilder, final Pattern langPattern, final Pattern langCodePattern, final boolean swap) {
103     this.enIndexBuilder = enIndexBuilder;
104     this.foreignIndexBuilder = otherIndexBuilder;
105     this.langPattern = langPattern;
106     this.langCodePattern = langCodePattern;
107     this.swap = swap;
108   }
109
110   @Override
111   void removeUselessArgs(Map<String, String> namedArgs) {
112     namedArgs.keySet().removeAll(USELESS_WIKI_ARGS);
113   }
114   
115   static class AppendAndIndexCallback extends AppendAndIndexWikiCallback<EnParser> {
116
117     public AppendAndIndexCallback(EnParser parser) {
118       super(parser);
119     }
120
121     @Override
122     public void onWikiLink(WikiTokenizer wikiTokenizer) {
123       final String text = wikiTokenizer.wikiLinkText();
124       final String link = wikiTokenizer.wikiLinkDest();
125       if (link != null) {
126         if (link.contains("#English")) {
127           dispatch(text, parser.enIndexBuilder, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
128         } else if (link.contains("#") && parser.langPattern.matcher(link).find()) {
129           dispatch(text, parser.foreignIndexBuilder, EntryTypeName.WIKTIONARY_ENGLISH_DEF_OTHER_LANG);
130         } else if (link.equals("plural")) {
131           builder.append(text);
132         } else {
133           //LOG.warning("Special link: " + englishTokenizer.token());
134           dispatch(text, EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK);
135         }
136       } else {
137         // link == null
138         final EntryTypeName entryTypeName;
139         switch (parser.state) {
140         case TRANSLATION_LINE:
141           entryTypeName = EntryTypeName.WIKTIONARY_TRANSLATION_WIKI_TEXT;
142           break;
143         case ENGLISH_DEF_OF_FOREIGN:
144           entryTypeName = EntryTypeName.WIKTIONARY_ENGLISH_DEF_WIKI_LINK;
145           break;
146           default:
147             throw new IllegalStateException("Invalid enum value: " + parser.state);
148         }
149         dispatch(text, entryTypeName);
150       }
151     }
152     
153   }
154
155 }