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.Arrays;
18 import java.util.LinkedHashSet;
19 import java.util.List;
22 import java.util.regex.Pattern;
24 import com.hughes.android.dictionary.engine.IndexBuilder;
25 import com.hughes.android.dictionary.engine.IndexedEntry;
26 import com.hughes.android.dictionary.engine.PairEntry;
27 import com.hughes.android.dictionary.engine.PairEntry.Pair;
28 import com.hughes.android.dictionary.parser.WikiTokenizer;
29 import com.hughes.android.dictionary.parser.wiktionary.EnFunctionCallbacks.TranslationCallback;
30 import com.hughes.util.ListUtil;
32 public final class EnTranslationToTranslationParser extends AbstractWiktionaryParser {
34 final IndexBuilder[] indexBuilders;
35 final Pattern[] langCodePatterns;
37 PairEntry pairEntry = null;
38 IndexedEntry indexedEntry = null;
39 StringBuilder[] builders = null;
41 final Set<String> Ts = new LinkedHashSet<String>(Arrays.asList("t", "t+",
42 "t-", "tø", "apdx-t", "ttbc"));
44 public EnTranslationToTranslationParser(final IndexBuilder[] indexBuilders,
45 final Pattern[] langCodePatterns) {
46 this.indexBuilders = indexBuilders;
47 this.langCodePatterns = langCodePatterns;
51 void removeUselessArgs(Map<String, String> namedArgs) {
52 namedArgs.keySet().removeAll(EnParser.USELESS_WIKI_ARGS);
56 void parseSection(String heading, String text) {
57 if (EnParser.isIgnorableTitle(title)) {
60 final WikiTokenizer wikiTokenizer = new WikiTokenizer(text);
61 while (wikiTokenizer.nextToken() != null) {
62 if (wikiTokenizer.isFunction()) {
63 final String name = wikiTokenizer.functionName();
64 if (Ts.contains(name)) {
66 } else if (name.equals("trans-top")) {
67 startEntry(title, wikiTokenizer.token());
68 } else if (name.equals("trans-bottom")) {
75 final TranslationCallback<EnTranslationToTranslationParser> translationCallback = new TranslationCallback<EnTranslationToTranslationParser>();
77 final AppendAndIndexWikiCallback<EnTranslationToTranslationParser> appendAndIndexWikiCallback = new AppendAndIndexWikiCallback<EnTranslationToTranslationParser>(
80 for (final String t : Ts) {
81 appendAndIndexWikiCallback.functionCallbacks.put(t, translationCallback);
85 private void onT(WikiTokenizer wikiTokenizer) {
86 final List<String> args = wikiTokenizer.functionPositionArgs();
87 final String langCode = ListUtil.get(args, 0);
88 for (int p = 0; p < 2; ++p) {
89 if (langCodePatterns[p].matcher(langCode).matches()) {
90 appendAndIndexWikiCallback.builder = builders[p];
91 appendAndIndexWikiCallback.indexBuilder = indexBuilders[p];
92 appendAndIndexWikiCallback.onFunction(wikiTokenizer,
93 wikiTokenizer.functionName(), wikiTokenizer.functionPositionArgs(),
94 wikiTokenizer.functionNamedArgs());
99 void startEntry(final String title, final String func) {
100 if (pairEntry != null) {
101 LOG.warning("startEntry() twice" + func);
105 pairEntry = new PairEntry(entrySource);
106 indexedEntry = new IndexedEntry(pairEntry);
107 builders = new StringBuilder[] { new StringBuilder(), new StringBuilder() };
110 void finishEntry(final String title) {
111 if (pairEntry == null) {
112 LOG.warning("finalizeEntry() twice" + title);
115 final String lang1 = builders[0].toString();
116 final String lang2 = builders[1].toString();
117 if (lang1.length() > 0 && lang2.length() > 0) {
118 pairEntry.pairs.add(new Pair(lang1, lang2));
119 indexedEntry.isValid = true;