1 // Copyright 2012 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.parser.wiktionary;
17 import java.util.List;
18 import java.util.regex.Pattern;
20 import com.hughes.android.dictionary.engine.EntryTypeName;
21 import com.hughes.android.dictionary.engine.IndexBuilder;
22 import com.hughes.android.dictionary.engine.IndexedEntry;
23 import com.hughes.android.dictionary.engine.PairEntry;
24 import com.hughes.android.dictionary.engine.PairEntry.Pair;
25 import com.hughes.android.dictionary.parser.WikiTokenizer;
27 public final class EnToTranslationParser extends EnParser {
29 public EnToTranslationParser(final IndexBuilder enIndexBuilder,
30 final IndexBuilder otherIndexBuilder, final Pattern langPattern,
31 final Pattern langCodePattern, final boolean swap) {
32 super(enIndexBuilder, otherIndexBuilder, langPattern, langCodePattern, swap);
36 void parseSection(String heading, String text) {
37 if (isIgnorableTitle(title)) {
40 heading = heading.replace("=", "").trim();
41 if (!heading.contains("English")) {
48 final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
49 while (wikiTokenizer.nextToken() != null) {
51 if (wikiTokenizer.isHeading()) {
52 final String headerName = wikiTokenizer.headingWikiText();
54 if (wikiTokenizer.headingDepth() <= posDepth) {
59 if (partOfSpeechHeader.matcher(headerName).matches()) {
60 posDepth = wikiTokenizer.headingDepth();
61 pos = wikiTokenizer.headingWikiText();
62 // TODO: if we're inside the POS section, we should handle the first title line...
64 } else if (headerName.equals("Translations")) {
66 LOG.info("Translations without POS (but using anyway): " + title);
68 doTranslations(wikiTokenizer, pos);
69 } else if (headerName.equals("Pronunciation")) {
70 //doPronunciation(wikiLineReader);
72 } else if (wikiTokenizer.isFunction()) {
73 final String name = wikiTokenizer.functionName();
74 if (name.equals("head") && pos == null) {
75 LOG.warning("{{head}} without POS: " + title);
81 private void doTranslations(final WikiTokenizer wikiTokenizer, final String pos) {
82 if (title.equals("absolutely")) {
83 //System.out.println();
86 String topLevelLang = null;
89 while (wikiTokenizer.nextToken() != null) {
90 if (wikiTokenizer.isHeading()) {
91 wikiTokenizer.returnToLineStart();
98 // Check whether we care about this line:
100 if (wikiTokenizer.isFunction()) {
101 final String functionName = wikiTokenizer.functionName();
102 final List<String> positionArgs = wikiTokenizer.functionPositionArgs();
104 if (functionName.equals("trans-top")) {
106 if (wikiTokenizer.functionPositionArgs().size() >= 1) {
107 sense = positionArgs.get(0);
108 sense = WikiTokenizer.toPlainText(sense);
109 //LOG.info("Sense: " + sense);
111 } else if (functionName.equals("trans-bottom")) {
113 } else if (functionName.equals("trans-mid")) {
114 } else if (functionName.equals("trans-see")) {
115 incrementCount("WARNING:trans-see");
116 } else if (functionName.startsWith("picdic")) {
117 } else if (functionName.startsWith("checktrans")) {
119 } else if (functionName.startsWith("ttbc")) {
120 wikiTokenizer.nextLine();
121 // TODO: would be great to handle ttbc
122 // TODO: Check this: done = true;
124 LOG.warning("Unexpected translation wikifunction: " + wikiTokenizer.token() + ", title=" + title);
126 } else if (wikiTokenizer.isListItem()) {
127 final String line = wikiTokenizer.listItemWikiText();
128 // This line could produce an output...
130 // if (line.contains("ich hoan dich gear")) {
131 // //System.out.println();
134 // First strip the language and check whether it matches.
135 // And hold onto it for sub-lines.
136 final int colonIndex = line.indexOf(":");
137 if (colonIndex == -1) {
141 final String lang = trim(WikiTokenizer.toPlainText(line.substring(0, colonIndex)));
142 incrementCount("tCount:" + lang);
143 final boolean appendLang;
144 if (wikiTokenizer.listItemPrefix().length() == 1) {
146 final boolean thisFind = langPattern.matcher(lang).find();
150 appendLang = !langPattern.matcher(lang).matches();
151 } else if (topLevelLang == null) {
154 // Two-level -- the only way we won't append is if this second level matches exactly.
155 if (!langPattern.matcher(lang).matches() && !langPattern.matcher(topLevelLang).find()) {
158 appendLang = !langPattern.matcher(lang).matches();
161 String rest = line.substring(colonIndex + 1).trim();
162 if (rest.length() > 0) {
163 doTranslationLine(line, appendLang ? lang : null, pos, sense, rest);
166 } else if (wikiTokenizer.remainderStartsWith("''See''")) {
167 wikiTokenizer.nextLine();
168 incrementCount("WARNING: ''See''" );
169 LOG.fine("Skipping See line: " + wikiTokenizer.token());
170 } else if (wikiTokenizer.isWikiLink()) {
171 final String wikiLink = wikiTokenizer.wikiLinkText();
172 if (wikiLink.contains(":") && wikiLink.contains(title)) {
173 } else if (wikiLink.contains("Category:")) {
175 incrementCount("WARNING: Unexpected wikiLink" );
176 LOG.warning("Unexpected wikiLink: " + wikiTokenizer.token() + ", title=" + title);
178 } else if (wikiTokenizer.isNewline() || wikiTokenizer.isMarkup() || wikiTokenizer.isComment()) {
180 final String token = wikiTokenizer.token();
181 if (token.equals("----")) {
183 LOG.warning("Unexpected translation token: " + wikiTokenizer.token() + ", title=" + title);
184 incrementCount("WARNING: Unexpected translation token" );
191 private void doTranslationLine(final String line, final String lang, final String pos, final String sense, final String rest) {
192 state = State.TRANSLATION_LINE;
193 // Good chance we'll actually file this one...
194 final PairEntry pairEntry = new PairEntry(entrySource);
195 final IndexedEntry indexedEntry = new IndexedEntry(pairEntry);
196 indexedEntry.isValid = true;
198 final StringBuilder foreignText = new StringBuilder();
199 appendAndIndexWikiCallback.reset(foreignText, indexedEntry);
200 appendAndIndexWikiCallback.dispatch(rest, foreignIndexBuilder, EntryTypeName.WIKTIONARY_TRANSLATION_OTHER_TEXT);
202 if (foreignText.length() == 0) {
203 LOG.warning("Empty foreignText: " + line);
204 incrementCount("WARNING: Empty foreignText" );
209 foreignText.insert(0, String.format("(%s) ", lang));
212 StringBuilder englishText = new StringBuilder();
214 englishText.append(title);
216 englishText.append(" (").append(sense).append(")");
217 enIndexBuilder.addEntryWithString(indexedEntry, sense, EntryTypeName.WIKTIONARY_TRANSLATION_SENSE);
220 englishText.append(" (").append(pos.toLowerCase()).append(")");
222 enIndexBuilder.addEntryWithString(indexedEntry, title, EntryTypeName.WIKTIONARY_TITLE_MULTI);
224 final Pair pair = new Pair(trim(englishText.toString()), trim(foreignText.toString()), swap);
225 pairEntry.pairs.add(pair);
226 if (!pairsAdded.add(pair.toString())) {
227 LOG.warning("Duplicate pair: " + pair.toString());
228 incrementCount("WARNING: Duplicate pair" );
231 } // EnToTranslationParser