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.JAJP; 18 19 import jp.co.omronsoft.openwnn.LetterConverter; 20 import jp.co.omronsoft.openwnn.ComposingText; 21 import jp.co.omronsoft.openwnn.StrSegment; 22 import java.util.HashMap; 23 import android.content.SharedPreferences; 24 25 /** 26 * The Romaji to full-width Katakana converter class for Japanese IME. 27 * 28 * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD. All Rights Reserved. 29 */ 30 public class RomkanFullKatakana implements LetterConverter { 31 /** HashMap for Romaji-to-Kana conversion (Japanese mode) */ 32 private static final HashMap<String, String> mRomkanTable = new HashMap<String, String>() {{ 33 put("la", "\u30a1"); put("xa", "\u30a1"); put("a", "\u30a2"); 34 put("li", "\u30a3"); put("lyi", "\u30a3"); put("xi", "\u30a3"); 35 put("xyi", "\u30a3"); put("i", "\u30a4"); put("yi", "\u30a4"); 36 put("ye", "\u30a4\u30a7"); put("lu", "\u30a5"); put("xu", "\u30a5"); 37 put("u", "\u30a6"); put("whu", "\u30a6"); put("wu", "\u30a6"); 38 put("wha", "\u30a6\u30a1"); put("whi", "\u30a6\u30a3"); put("wi", "\u30a6\u30a3"); 39 put("we", "\u30a6\u30a7"); put("whe", "\u30a6\u30a7"); put("who", "\u30a6\u30a9"); 40 put("le", "\u30a7"); put("lye", "\u30a7"); put("xe", "\u30a7"); 41 put("xye", "\u30a7"); put("e", "\u30a8"); put("lo", "\u30a9"); 42 put("xo", "\u30a9"); put("o", "\u30aa"); put("ca", "\u30ab"); 43 put("lka", "\u30f5"); put("xka", "\u30f5"); 44 put("ka", "\u30ab"); put("ga", "\u30ac"); put("ki", "\u30ad"); 45 put("kyi", "\u30ad\u30a3"); put("kye", "\u30ad\u30a7"); put("kya", "\u30ad\u30e3"); 46 put("kyu", "\u30ad\u30e5"); put("kyo", "\u30ad\u30e7"); put("gi", "\u30ae"); 47 put("gyi", "\u30ae\u30a3"); put("gye", "\u30ae\u30a7"); put("gya", "\u30ae\u30e3"); 48 put("gyu", "\u30ae\u30e5"); put("gyo", "\u30ae\u30e7"); put("cu", "\u30af"); 49 put("ku", "\u30af"); put("qu", "\u30af"); put("kwa", "\u30af\u30a1"); 50 put("qa", "\u30af\u30a1"); put("qwa", "\u30af\u30a1"); put("qi", "\u30af\u30a3"); 51 put("qwi", "\u30af\u30a3"); put("qyi", "\u30af\u30a3"); put("qwu", "\u30af\u30a5"); 52 put("qe", "\u30af\u30a7"); put("qwe", "\u30af\u30a7"); put("qye", "\u30af\u30a7"); 53 put("qo", "\u30af\u30a9"); put("qwo", "\u30af\u30a9"); put("qya", "\u30af\u30e3"); 54 put("qyu", "\u30af\u30e5"); put("qyo", "\u30af\u30e7"); put("gu", "\u30b0"); 55 put("gwa", "\u30b0\u30a1"); put("gwi", "\u30b0\u30a3"); put("gwu", "\u30b0\u30a5"); 56 put("gwe", "\u30b0\u30a7"); put("gwo", "\u30b0\u30a9"); 57 put("lke", "\u30f6"); put("xke", "\u30f6"); put("ke", "\u30b1"); 58 put("ge", "\u30b2"); put("co", "\u30b3"); put("ko", "\u30b3"); 59 put("go", "\u30b4"); put("sa", "\u30b5"); put("za", "\u30b6"); 60 put("ci", "\u30b7"); put("shi", "\u30b7"); put("si", "\u30b7"); 61 put("syi", "\u30b7\u30a3"); put("she", "\u30b7\u30a7"); put("sye", "\u30b7\u30a7"); 62 put("sha", "\u30b7\u30e3"); put("sya", "\u30b7\u30e3"); put("shu", "\u30b7\u30e5"); 63 put("syu", "\u30b7\u30e5"); put("sho", "\u30b7\u30e7"); put("syo", "\u30b7\u30e7"); 64 put("ji", "\u30b8"); put("zi", "\u30b8"); put("jyi", "\u30b8\u30a3"); 65 put("zyi", "\u30b8\u30a3"); put("je", "\u30b8\u30a7"); put("jye", "\u30b8\u30a7"); 66 put("zye", "\u30b8\u30a7"); put("ja", "\u30b8\u30e3"); put("jya", "\u30b8\u30e3"); 67 put("zya", "\u30b8\u30e3"); put("ju", "\u30b8\u30e5"); put("jyu", "\u30b8\u30e5"); 68 put("zyu", "\u30b8\u30e5"); put("jo", "\u30b8\u30e7"); put("jyo", "\u30b8\u30e7"); 69 put("zyo", "\u30b8\u30e7"); put("su", "\u30b9"); put("swa", "\u30b9\u30a1"); 70 put("swi", "\u30b9\u30a3"); put("swu", "\u30b9\u30a5"); put("swe", "\u30b9\u30a7"); 71 put("swo", "\u30b9\u30a9"); put("zu", "\u30ba"); put("ce", "\u30bb"); 72 put("se", "\u30bb"); put("ze", "\u30bc"); put("so", "\u30bd"); 73 put("zo", "\u30be"); put("ta", "\u30bf"); put("da", "\u30c0"); 74 put("chi", "\u30c1"); put("ti", "\u30c1"); put("cyi", "\u30c1\u30a3"); 75 put("tyi", "\u30c1\u30a3"); put("che", "\u30c1\u30a7"); put("cye", "\u30c1\u30a7"); 76 put("tye", "\u30c1\u30a7"); put("cha", "\u30c1\u30e3"); put("cya", "\u30c1\u30e3"); 77 put("tya", "\u30c1\u30e3"); put("chu", "\u30c1\u30e5"); put("cyu", "\u30c1\u30e5"); 78 put("tyu", "\u30c1\u30e5"); put("cho", "\u30c1\u30e7"); put("cyo", "\u30c1\u30e7"); 79 put("tyo", "\u30c1\u30e7"); put("di", "\u30c2"); put("dyi", "\u30c2\u30a3"); 80 put("dye", "\u30c2\u30a7"); put("dya", "\u30c2\u30e3"); put("dyu", "\u30c2\u30e5"); 81 put("dyo", "\u30c2\u30e7"); put("ltsu", "\u30c3"); put("ltu", "\u30c3"); 82 put("xtu", "\u30c3"); put("", "\u30c3"); put("tsu", "\u30c4"); 83 put("tu", "\u30c4"); put("tsa", "\u30c4\u30a1"); put("tsi", "\u30c4\u30a3"); 84 put("tse", "\u30c4\u30a7"); put("tso", "\u30c4\u30a9"); put("du", "\u30c5"); 85 put("te", "\u30c6"); put("thi", "\u30c6\u30a3"); put("the", "\u30c6\u30a7"); 86 put("tha", "\u30c6\u30e3"); put("thu", "\u30c6\u30e5"); put("tho", "\u30c6\u30e7"); 87 put("de", "\u30c7"); put("dhi", "\u30c7\u30a3"); put("dhe", "\u30c7\u30a7"); 88 put("dha", "\u30c7\u30e3"); put("dhu", "\u30c7\u30e5"); put("dho", "\u30c7\u30e7"); 89 put("to", "\u30c8"); put("twa", "\u30c8\u30a1"); put("twi", "\u30c8\u30a3"); 90 put("twu", "\u30c8\u30a5"); put("twe", "\u30c8\u30a7"); put("two", "\u30c8\u30a9"); 91 put("do", "\u30c9"); put("dwa", "\u30c9\u30a1"); put("dwi", "\u30c9\u30a3"); 92 put("dwu", "\u30c9\u30a5"); put("dwe", "\u30c9\u30a7"); put("dwo", "\u30c9\u30a9"); 93 put("na", "\u30ca"); put("ni", "\u30cb"); put("nyi", "\u30cb\u30a3"); 94 put("nye", "\u30cb\u30a7"); put("nya", "\u30cb\u30e3"); put("nyu", "\u30cb\u30e5"); 95 put("nyo", "\u30cb\u30e7"); put("nu", "\u30cc"); put("ne", "\u30cd"); 96 put("no", "\u30ce"); put("ha", "\u30cf"); put("ba", "\u30d0"); 97 put("pa", "\u30d1"); put("hi", "\u30d2"); put("hyi", "\u30d2\u30a3"); 98 put("hye", "\u30d2\u30a7"); put("hya", "\u30d2\u30e3"); put("hyu", "\u30d2\u30e5"); 99 put("hyo", "\u30d2\u30e7"); put("bi", "\u30d3"); put("byi", "\u30d3\u30a3"); 100 put("bye", "\u30d3\u30a7"); put("bya", "\u30d3\u30e3"); put("byu", "\u30d3\u30e5"); 101 put("byo", "\u30d3\u30e7"); put("pi", "\u30d4"); put("pyi", "\u30d4\u30a3"); 102 put("pye", "\u30d4\u30a7"); put("pya", "\u30d4\u30e3"); put("pyu", "\u30d4\u30e5"); 103 put("pyo", "\u30d4\u30e7"); put("fu", "\u30d5"); put("hu", "\u30d5"); 104 put("fa", "\u30d5\u30a1"); put("fwa", "\u30d5\u30a1"); put("fi", "\u30d5\u30a3"); 105 put("fwi", "\u30d5\u30a3"); put("fyi", "\u30d5\u30a3"); put("fwu", "\u30d5\u30a5"); 106 put("fe", "\u30d5\u30a7"); put("fwe", "\u30d5\u30a7"); put("fye", "\u30d5\u30a7"); 107 put("fo", "\u30d5\u30a9"); put("fwo", "\u30d5\u30a9"); put("fya", "\u30d5\u30e3"); 108 put("fyu", "\u30d5\u30e5"); put("fyo", "\u30d5\u30e7"); put("bu", "\u30d6"); 109 put("pu", "\u30d7"); put("he", "\u30d8"); put("be", "\u30d9"); 110 put("pe", "\u30da"); put("ho", "\u30db"); put("bo", "\u30dc"); 111 put("po", "\u30dd"); put("ma", "\u30de"); put("mi", "\u30df"); 112 put("myi", "\u30df\u30a3"); put("mye", "\u30df\u30a7"); put("mya", "\u30df\u30e3"); 113 put("myu", "\u30df\u30e5"); put("myo", "\u30df\u30e7"); put("mu", "\u30e0"); 114 put("me", "\u30e1"); put("mo", "\u30e2"); put("lya", "\u30e3"); 115 put("xya", "\u30e3"); put("ya", "\u30e4"); put("lyu", "\u30e5"); 116 put("xyu", "\u30e5"); put("yu", "\u30e6"); put("lyo", "\u30e7"); 117 put("xyo", "\u30e7"); put("yo", "\u30e8"); put("ra", "\u30e9"); 118 put("ri", "\u30ea"); put("ryi", "\u30ea\u30a3"); put("rye", "\u30ea\u30a7"); 119 put("rya", "\u30ea\u30e3"); put("ryu", "\u30ea\u30e5"); put("ryo", "\u30ea\u30e7"); 120 put("ru", "\u30eb"); put("re", "\u30ec"); put("ro", "\u30ed"); 121 put("lwa", "\u30ee"); put("xwa", "\u30ee"); put("wa", "\u30ef"); 122 put("wo", "\u30f2"); put("nn", "\u30f3"); put("xn", "\u30f3"); 123 put("vu", "\u30f4"); put("va", "\u30f4\u30a1"); put("vi", "\u30f4\u30a3"); 124 put("vyi", "\u30f4\u30a3"); put("ve", "\u30f4\u30a7"); put("vye", "\u30f4\u30a7"); 125 put("vo", "\u30f4\u30a9"); put("vya", "\u30f4\u30e3"); put("vyu", "\u30f4\u30e5"); 126 put("vyo", "\u30f4\u30e7"); 127 put("bb", "\u30c3b"); put("cc", "\u30c3c"); put("dd", "\u30c3d"); 128 put("ff", "\u30c3f"); put("gg", "\u30c3g"); put("hh", "\u30c3h"); 129 put("jj", "\u30c3j"); put("kk", "\u30c3k"); put("ll", "\u30c3l"); 130 put("mm", "\u30c3m"); put("pp", "\u30c3p"); put("qq", "\u30c3q"); 131 put("rr", "\u30c3r"); put("ss", "\u30c3s"); put("tt", "\u30c3t"); 132 put("vv", "\u30c3v"); put("ww", "\u30c3w"); put("xx", "\u30c3x"); 133 put("yy", "\u30c3y"); put("zz", "\u30c3z"); put("nb", "\u30f3b"); 134 put("nc", "\u30f3c"); put("nd", "\u30f3d"); put("nf", "\u30f3f"); 135 put("ng", "\u30f3g"); put("nh", "\u30f3h"); put("nj", "\u30f3j"); 136 put("nk", "\u30f3k"); put("nm", "\u30f3m"); put("np", "\u30f3p"); 137 put("nq", "\u30f3q"); put("nr", "\u30f3r"); put("ns", "\u30f3s"); 138 put("nt", "\u30f3t"); put("nv", "\u30f3v"); put("nw", "\u30f3w"); 139 put("nx", "\u30f3x"); put("nz", "\u30f3z"); put("nl", "\u30f3l"); 140 put("-", "\u30fc"); put(".", "\u3002"); put(",", "\u3001"); put("?", "\uff1f"); put("/", "\u30fb"); 141 }}; 142 143 /** Max length of the target text */ 144 private static final int MAX_LENGTH = 4; 145 146 147 /** 148 * Default constructor 149 */ 150 public RomkanFullKatakana() { 151 super(); 152 } 153 154 /** @see LetterConverter#convert */ 155 public boolean convert(ComposingText text) { 156 return convert(text, mRomkanTable); 157 } 158 159 /** 160 * convert Romaji to Full Katakana 161 * 162 * @param text The input/output text 163 * @param table HashMap for Romaji-to-Kana conversion 164 * @return {@code true} if conversion is compleated; {@code false} if not 165 */ 166 public static boolean convert(ComposingText text, HashMap<String, String> table) { 167 int cursor = text.getCursor(1); 168 169 if (cursor <= 0) { 170 return false; 171 } 172 173 StrSegment[] str = new StrSegment[MAX_LENGTH]; 174 int start = MAX_LENGTH; 175 int checkLength = Math.min(cursor, MAX_LENGTH); 176 for (int i = 1; i <= checkLength; i++) { 177 str[MAX_LENGTH - i] = text.getStrSegment(1, cursor - i); 178 start--; 179 } 180 181 StringBuffer key = new StringBuffer(); 182 while (start < MAX_LENGTH) { 183 for (int i = start; i < MAX_LENGTH; i++) { 184 key.append(str[i].string); 185 } 186 boolean upper = Character.isUpperCase(key.charAt(key.length() - 1)); 187 String match = table.get(key.toString().toLowerCase()); 188 if (match != null) { 189 if (upper) { 190 match = match.toUpperCase(); 191 } 192 StrSegment[] out; 193 if (match.length() == 1) { 194 out = new StrSegment[1]; 195 out[0] = new StrSegment(match, str[start].from, str[MAX_LENGTH - 1].to); 196 text.replaceStrSegment(ComposingText.LAYER1, out, MAX_LENGTH - start); 197 } else { 198 out = new StrSegment[2]; 199 out[0] = new StrSegment(match.substring(0, match.length() - 1), 200 str[start].from, str[MAX_LENGTH - 1].to - 1); 201 out[1] = new StrSegment(match.substring(match.length() - 1), 202 str[MAX_LENGTH - 1].to, str[MAX_LENGTH - 1].to); 203 text.replaceStrSegment(1, out, MAX_LENGTH - start); 204 } 205 return true; 206 } 207 start++; 208 key.delete(0, key.length()); 209 } 210 211 return false; 212 } 213 214 /** @see LetterConverter#setPreferences */ 215 public void setPreferences(SharedPreferences pref) {} 216 } 217