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