The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 1 | /* |
Junichi Monma | 59aefa2 | 2012-04-24 16:13:54 +0900 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 OMRON SOFTWARE Co., Ltd. |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package jp.co.omronsoft.openwnn; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Iterator; |
| 23 | import org.xmlpull.v1.XmlPullParserException; |
| 24 | import android.content.SharedPreferences; |
| 25 | import android.content.res.XmlResourceParser; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | /** |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 29 | * The generator class of symbol list. |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 30 | * <br> |
| 31 | * This class is used for generating lists of symbols. |
| 32 | * |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 33 | * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD. All Rights Reserved. |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 34 | */ |
| 35 | public class SymbolList implements WnnEngine { |
| 36 | /* |
| 37 | * DEFINITION OF CONSTANTS |
| 38 | */ |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 39 | /** Language definition (English) */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 40 | public static final int LANG_EN = 0; |
| 41 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 42 | /** Language definition (Japanese) */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 43 | public static final int LANG_JA = 1; |
| 44 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 45 | /** Language definition (Chinese) */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 46 | public static final int LANG_ZHCN = 2; |
| 47 | |
| 48 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 49 | /** Key string to get normal symbol list for Japanese */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 50 | public static final String SYMBOL_JAPANESE = "j"; |
| 51 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 52 | /** Key string to get normal symbol list for English */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 53 | public static final String SYMBOL_ENGLISH = "e"; |
| 54 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 55 | /** Key string to get normal symbol list for Chinese */ |
| 56 | public static final String SYMBOL_CHINESE = "c1"; |
Daisuke Miyakawa | 7b3b414 | 2009-03-24 19:47:47 -0700 | [diff] [blame] | 57 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 58 | /** Key string to get face mark list for Japanese */ |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 59 | public static final String SYMBOL_JAPANESE_FACE = "j_face"; |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 60 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 61 | /** The name of XML tag key */ |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 62 | private static final String XMLTAG_KEY = "string"; |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * DEFINITION OF VARIABLES |
| 66 | */ |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 67 | /** Symbols data */ |
Daisuke Miyakawa | 7b3b414 | 2009-03-24 19:47:47 -0700 | [diff] [blame] | 68 | protected HashMap<String,ArrayList<String>> mSymbols; |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 69 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 70 | /** OpenWnn which has this instance */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 71 | private OpenWnn mWnn; |
| 72 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 73 | /** current list of symbols */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 74 | private ArrayList<String> mCurrentList; |
| 75 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 76 | /** Iterator for getting symbols from the list */ |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 77 | private Iterator<String> mCurrentListIterator; |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * DEFINITION OF METHODS |
| 81 | */ |
| 82 | /** |
| 83 | * Constructor |
| 84 | * |
Daisuke Miyakawa | 7b3b414 | 2009-03-24 19:47:47 -0700 | [diff] [blame] | 85 | * @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 Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 87 | */ |
| 88 | public SymbolList(OpenWnn parent, int lang) { |
| 89 | mWnn = parent; |
Daisuke Miyakawa | 7b3b414 | 2009-03-24 19:47:47 -0700 | [diff] [blame] | 90 | mSymbols = new HashMap<String, ArrayList<String>>(); |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 91 | |
| 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 Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 101 | mSymbols.put(SYMBOL_JAPANESE, getXmlfile(R.xml.symbols_japan_list)); |
| 102 | mSymbols.put(SYMBOL_JAPANESE_FACE, getXmlfile(R.xml.symbols_japan_face_list)); |
Junichi Monma | 59aefa2 | 2012-04-24 16:13:54 +0900 | [diff] [blame] | 103 | mSymbols.put(SYMBOL_ENGLISH, getXmlfile(R.xml.symbols_latin1_list)); |
| 104 | mCurrentList = mSymbols.get(SYMBOL_JAPANESE); |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 105 | break; |
Daisuke Miyakawa | e83971e | 2009-06-16 16:31:48 +0900 | [diff] [blame] | 106 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 107 | case LANG_ZHCN: |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 108 | /* symbols for Chinese IME */ |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 109 | 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 Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 112 | break; |
| 113 | } |
| 114 | |
| 115 | mCurrentList = null; |
| 116 | } |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 117 | |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Get a attribute value from a XML resource. |
| 120 | * |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 121 | * @param xrp XML resource |
| 122 | * @param name The attribute name |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 123 | * |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 124 | * @return The value of the attribute |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 125 | */ |
| 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 Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 138 | * @param id XML resource ID |
| 139 | * @return The symbols list |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 140 | */ |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 141 | private ArrayList<String> getXmlfile(int id) { |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 142 | 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 Miyakawa | 7b3b414 | 2009-03-24 19:47:47 -0700 | [diff] [blame] | 172 | * @return {@code true} if valid type is specified; {@code false} if not; |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 173 | */ |
| 174 | public boolean setDictionary(String listType) { |
| 175 | mCurrentList = mSymbols.get(listType); |
| 176 | return (mCurrentList != null); |
| 177 | } |
| 178 | |
| 179 | /*********************************************************************** |
| 180 | * WnnEngine's interface |
| 181 | **********************************************************************/ |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 182 | /** @see jp.co.omronsoft.openwnn.WnnEngine#init */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 183 | public void init() {} |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 184 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 185 | /** @see jp.co.omronsoft.openwnn.WnnEngine#close */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 186 | public void close() {} |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 187 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 188 | /** @see jp.co.omronsoft.openwnn.WnnEngine#predict */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 189 | 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 Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 200 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 201 | /** @see jp.co.omronsoft.openwnn.WnnEngine#convert */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 202 | public int convert(ComposingText text) { |
| 203 | return 0; |
| 204 | } |
| 205 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 206 | /** @see jp.co.omronsoft.openwnn.WnnEngine#searchWords */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 207 | public int searchWords(String key) {return 0;} |
| 208 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 209 | /** @see jp.co.omronsoft.openwnn.WnnEngine#searchWords */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 210 | public int searchWords(WnnWord word) {return 0;} |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 211 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 212 | /** @see jp.co.omronsoft.openwnn.WnnEngine#getNextCandidate */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 213 | 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 Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 221 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 222 | /** @see jp.co.omronsoft.openwnn.WnnEngine#learn */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 223 | public boolean learn(WnnWord word) {return false;} |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 224 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 225 | /** @see jp.co.omronsoft.openwnn.WnnEngine#addWord */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 226 | public int addWord(WnnWord word) {return 0;} |
| 227 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 228 | /** @see jp.co.omronsoft.openwnn.WnnEngine#deleteWord */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 229 | public boolean deleteWord(WnnWord word) {return false;} |
Daisuke Miyakawa | 77ffa9b | 2009-04-05 19:19:47 -0700 | [diff] [blame] | 230 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 231 | /** @see jp.co.omronsoft.openwnn.WnnEngine#setPreferences */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 232 | public void setPreferences(SharedPreferences pref) {} |
| 233 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 234 | /** @see jp.co.omronsoft.openwnn.WnnEngine#breakSequence */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 235 | public void breakSequence() {} |
| 236 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 237 | /** @see jp.co.omronsoft.openwnn.WnnEngine#makeCandidateListOf */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 238 | public int makeCandidateListOf(int clausePosition) {return 0;} |
| 239 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 240 | /** @see jp.co.omronsoft.openwnn.WnnEngine#initializeDictionary */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 241 | public boolean initializeDictionary(int dictionary) {return true;} |
| 242 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 243 | /** @see jp.co.omronsoft.openwnn.WnnEngine#initializeDictionary */ |
Daisuke Miyakawa | 7b3b414 | 2009-03-24 19:47:47 -0700 | [diff] [blame] | 244 | public boolean initializeDictionary(int dictionary, int type) {return true;} |
| 245 | |
Daisuke Miyakawa | 3feb3d2 | 2009-03-27 19:24:41 -0700 | [diff] [blame] | 246 | /** @see jp.co.omronsoft.openwnn.WnnEngine#getUserDictionaryWords */ |
The Android Open Source Project | 053d509 | 2009-03-15 21:40:26 -0700 | [diff] [blame] | 247 | public WnnWord[] getUserDictionaryWords() {return null;} |
| 248 | } |