blob: 80005255a73aec68199beaf5da721617982b2ac8 [file] [log] [blame]
The Android Open Source Project053d5092009-03-15 21:40:26 -07001/*
Junichi Monma59aefa22012-04-24 16:13:54 +09002 * Copyright (C) 2008-2012 OMRON SOFTWARE Co., Ltd.
The Android Open Source Project053d5092009-03-15 21:40:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package jp.co.omronsoft.openwnn;
18
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.HashMap;
22import java.util.Iterator;
23import org.xmlpull.v1.XmlPullParserException;
24import android.content.SharedPreferences;
25import android.content.res.XmlResourceParser;
26import android.util.Log;
27
28/**
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070029 * The generator class of symbol list.
The Android Open Source Project053d5092009-03-15 21:40:26 -070030 * <br>
31 * This class is used for generating lists of symbols.
32 *
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070033 * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD. All Rights Reserved.
The Android Open Source Project053d5092009-03-15 21:40:26 -070034 */
35public class SymbolList implements WnnEngine {
36 /*
37 * DEFINITION OF CONSTANTS
38 */
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070039 /** Language definition (English) */
The Android Open Source Project053d5092009-03-15 21:40:26 -070040 public static final int LANG_EN = 0;
41
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070042 /** Language definition (Japanese) */
The Android Open Source Project053d5092009-03-15 21:40:26 -070043 public static final int LANG_JA = 1;
44
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070045 /** Language definition (Chinese) */
The Android Open Source Project053d5092009-03-15 21:40:26 -070046 public static final int LANG_ZHCN = 2;
47
48
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070049 /** Key string to get normal symbol list for Japanese */
The Android Open Source Project053d5092009-03-15 21:40:26 -070050 public static final String SYMBOL_JAPANESE = "j";
51
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070052 /** Key string to get normal symbol list for English */
The Android Open Source Project053d5092009-03-15 21:40:26 -070053 public static final String SYMBOL_ENGLISH = "e";
54
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070055 /** Key string to get normal symbol list for Chinese */
56 public static final String SYMBOL_CHINESE = "c1";
Daisuke Miyakawa7b3b4142009-03-24 19:47:47 -070057
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070058 /** Key string to get face mark list for Japanese */
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -070059 public static final String SYMBOL_JAPANESE_FACE = "j_face";
The Android Open Source Project053d5092009-03-15 21:40:26 -070060
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070061 /** The name of XML tag key */
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -070062 private static final String XMLTAG_KEY = "string";
The Android Open Source Project053d5092009-03-15 21:40:26 -070063
64 /*
65 * DEFINITION OF VARIABLES
66 */
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070067 /** Symbols data */
Daisuke Miyakawa7b3b4142009-03-24 19:47:47 -070068 protected HashMap<String,ArrayList<String>> mSymbols;
The Android Open Source Project053d5092009-03-15 21:40:26 -070069
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070070 /** OpenWnn which has this instance */
The Android Open Source Project053d5092009-03-15 21:40:26 -070071 private OpenWnn mWnn;
72
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070073 /** current list of symbols */
The Android Open Source Project053d5092009-03-15 21:40:26 -070074 private ArrayList<String> mCurrentList;
75
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -070076 /** Iterator for getting symbols from the list */
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -070077 private Iterator<String> mCurrentListIterator;
The Android Open Source Project053d5092009-03-15 21:40:26 -070078
79 /*
80 * DEFINITION OF METHODS
81 */
82 /**
83 * Constructor
84 *
Daisuke Miyakawa7b3b4142009-03-24 19:47:47 -070085 * @param parent OpenWnn instance which uses this.
86 * @param lang Language ({@code LANG_EN}, {@code LANG_JA} or {@code LANG_ZHCN})
The Android Open Source Project053d5092009-03-15 21:40:26 -070087 */
88 public SymbolList(OpenWnn parent, int lang) {
89 mWnn = parent;
Daisuke Miyakawa7b3b4142009-03-24 19:47:47 -070090 mSymbols = new HashMap<String, ArrayList<String>>();
The Android Open Source Project053d5092009-03-15 21:40:26 -070091
92 switch (lang) {
93 case LANG_EN:
94 /* symbols for English IME */
95 mSymbols.put(SYMBOL_ENGLISH, getXmlfile(R.xml.symbols_latin12_list));
96 mCurrentList = mSymbols.get(SYMBOL_ENGLISH);
97 break;
98
99 case LANG_JA:
100 /* symbols for Japanese IME */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700101 mSymbols.put(SYMBOL_JAPANESE, getXmlfile(R.xml.symbols_japan_list));
102 mSymbols.put(SYMBOL_JAPANESE_FACE, getXmlfile(R.xml.symbols_japan_face_list));
Junichi Monma59aefa22012-04-24 16:13:54 +0900103 mSymbols.put(SYMBOL_ENGLISH, getXmlfile(R.xml.symbols_latin1_list));
104 mCurrentList = mSymbols.get(SYMBOL_JAPANESE);
The Android Open Source Project053d5092009-03-15 21:40:26 -0700105 break;
Daisuke Miyakawae83971e2009-06-16 16:31:48 +0900106
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700107 case LANG_ZHCN:
The Android Open Source Project053d5092009-03-15 21:40:26 -0700108 /* symbols for Chinese IME */
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700109 mSymbols.put(SYMBOL_CHINESE, getXmlfile(R.xml.symbols_china_list));
110 mSymbols.put(SYMBOL_ENGLISH, getXmlfile(R.xml.symbols_latin1_list));
111 mCurrentList = mSymbols.get(SYMBOL_CHINESE);
The Android Open Source Project053d5092009-03-15 21:40:26 -0700112 break;
113 }
114
115 mCurrentList = null;
116 }
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700117
The Android Open Source Project053d5092009-03-15 21:40:26 -0700118 /**
119 * Get a attribute value from a XML resource.
120 *
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700121 * @param xrp XML resource
122 * @param name The attribute name
The Android Open Source Project053d5092009-03-15 21:40:26 -0700123 *
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700124 * @return The value of the attribute
The Android Open Source Project053d5092009-03-15 21:40:26 -0700125 */
126 private String getXmlAttribute(XmlResourceParser xrp, String name) {
127 int resId = xrp.getAttributeResourceValue(null, name, 0);
128 if (resId == 0) {
129 return xrp.getAttributeValue(null, name);
130 } else {
131 return mWnn.getString(resId);
132 }
133 }
134
135 /**
136 * Load a symbols list from XML resource.
137 *
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700138 * @param id XML resource ID
139 * @return The symbols list
The Android Open Source Project053d5092009-03-15 21:40:26 -0700140 */
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700141 private ArrayList<String> getXmlfile(int id) {
The Android Open Source Project053d5092009-03-15 21:40:26 -0700142 ArrayList<String> list = new ArrayList<String>();
143
144 XmlResourceParser xrp = mWnn.getResources().getXml(id);
145 try {
146 int xmlEventType;
147 while ((xmlEventType = xrp.next()) != XmlResourceParser.END_DOCUMENT) {
148 if (xmlEventType == XmlResourceParser.START_TAG) {
149 String attribute = xrp.getName();
150 if (XMLTAG_KEY.equals(attribute)) {
151 String value = getXmlAttribute(xrp, "value");
152 if (value != null) {
153 list.add(value);
154 }
155 }
156 }
157 }
158 xrp.close();
159 } catch (XmlPullParserException e) {
160 Log.e("OpenWnn", "Ill-formatted keybaord resource file");
161 } catch (IOException e) {
162 Log.e("OpenWnn", "Unable to read keyboard resource file");
163 }
164
165 return list;
166 }
167
168 /**
169 * Set the dictionary
170 *
171 * @param listType The list of symbol
Daisuke Miyakawa7b3b4142009-03-24 19:47:47 -0700172 * @return {@code true} if valid type is specified; {@code false} if not;
The Android Open Source Project053d5092009-03-15 21:40:26 -0700173 */
174 public boolean setDictionary(String listType) {
175 mCurrentList = mSymbols.get(listType);
176 return (mCurrentList != null);
177 }
178
179 /***********************************************************************
180 * WnnEngine's interface
181 **********************************************************************/
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700182 /** @see jp.co.omronsoft.openwnn.WnnEngine#init */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700183 public void init() {}
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700184
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700185 /** @see jp.co.omronsoft.openwnn.WnnEngine#close */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700186 public void close() {}
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700187
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700188 /** @see jp.co.omronsoft.openwnn.WnnEngine#predict */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700189 public int predict(ComposingText text, int minLen, int maxLen) {
190 /* ignore if there is no list for the type */
191 if (mCurrentList == null) {
192 mCurrentListIterator = null;
193 return 0;
194 }
195
196 /* return the iterator of the list */
197 mCurrentListIterator = mCurrentList.iterator();
198 return 1;
199 }
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700200
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700201 /** @see jp.co.omronsoft.openwnn.WnnEngine#convert */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700202 public int convert(ComposingText text) {
203 return 0;
204 }
205
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700206 /** @see jp.co.omronsoft.openwnn.WnnEngine#searchWords */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700207 public int searchWords(String key) {return 0;}
208
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700209 /** @see jp.co.omronsoft.openwnn.WnnEngine#searchWords */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700210 public int searchWords(WnnWord word) {return 0;}
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700211
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700212 /** @see jp.co.omronsoft.openwnn.WnnEngine#getNextCandidate */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700213 public WnnWord getNextCandidate() {
214 if (mCurrentListIterator == null || !mCurrentListIterator.hasNext()) {
215 return null;
216 }
217 String str = mCurrentListIterator.next();
218 WnnWord word = new WnnWord(str, str);
219 return word;
220 }
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700221
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700222 /** @see jp.co.omronsoft.openwnn.WnnEngine#learn */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700223 public boolean learn(WnnWord word) {return false;}
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700224
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700225 /** @see jp.co.omronsoft.openwnn.WnnEngine#addWord */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700226 public int addWord(WnnWord word) {return 0;}
227
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700228 /** @see jp.co.omronsoft.openwnn.WnnEngine#deleteWord */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700229 public boolean deleteWord(WnnWord word) {return false;}
Daisuke Miyakawa77ffa9b2009-04-05 19:19:47 -0700230
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700231 /** @see jp.co.omronsoft.openwnn.WnnEngine#setPreferences */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700232 public void setPreferences(SharedPreferences pref) {}
233
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700234 /** @see jp.co.omronsoft.openwnn.WnnEngine#breakSequence */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700235 public void breakSequence() {}
236
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700237 /** @see jp.co.omronsoft.openwnn.WnnEngine#makeCandidateListOf */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700238 public int makeCandidateListOf(int clausePosition) {return 0;}
239
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700240 /** @see jp.co.omronsoft.openwnn.WnnEngine#initializeDictionary */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700241 public boolean initializeDictionary(int dictionary) {return true;}
242
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700243 /** @see jp.co.omronsoft.openwnn.WnnEngine#initializeDictionary */
Daisuke Miyakawa7b3b4142009-03-24 19:47:47 -0700244 public boolean initializeDictionary(int dictionary, int type) {return true;}
245
Daisuke Miyakawa3feb3d22009-03-27 19:24:41 -0700246 /** @see jp.co.omronsoft.openwnn.WnnEngine#getUserDictionaryWords */
The Android Open Source Project053d5092009-03-15 21:40:26 -0700247 public WnnWord[] getUserDictionaryWords() {return null;}
248}