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.*; 20 import java.util.HashMap; 21 import android.content.SharedPreferences; 22 23 /** 24 * The Romaji to Hiragana converter class for Japanese IME. 25 * 26 * @author Copyright (C) 2009 OMRON SOFTWARE CO., LTD. All Rights Reserved. 27 */ 28 public class Romkan implements LetterConverter { 29 /** HashMap for Romaji-to-Kana conversion (Japanese mode) */ 30 private static final HashMap<String, String> romkanTable = new HashMap<String, String>() {{ 31 put("la", "\u3041"); put("xa", "\u3041"); put("a", "\u3042"); 32 put("li", "\u3043"); put("lyi", "\u3043"); put("xi", "\u3043"); 33 put("xyi", "\u3043"); put("i", "\u3044"); put("yi", "\u3044"); 34 put("ye", "\u3044\u3047"); put("lu", "\u3045"); put("xu", "\u3045"); 35 put("u", "\u3046"); put("whu", "\u3046"); put("wu", "\u3046"); 36 put("wha", "\u3046\u3041"); put("whi", "\u3046\u3043"); put("wi", "\u3046\u3043"); 37 put("we", "\u3046\u3047"); put("whe", "\u3046\u3047"); put("who", "\u3046\u3049"); 38 put("le", "\u3047"); put("lye", "\u3047"); put("xe", "\u3047"); 39 put("xye", "\u3047"); put("e", "\u3048"); put("lo", "\u3049"); 40 put("xo", "\u3049"); put("o", "\u304a"); put("ca", "\u304b"); 41 put("ka", "\u304b"); put("ga", "\u304c"); put("ki", "\u304d"); 42 put("kyi", "\u304d\u3043"); put("kye", "\u304d\u3047"); put("kya", "\u304d\u3083"); 43 put("kyu", "\u304d\u3085"); put("kyo", "\u304d\u3087"); put("gi", "\u304e"); 44 put("gyi", "\u304e\u3043"); put("gye", "\u304e\u3047"); put("gya", "\u304e\u3083"); 45 put("gyu", "\u304e\u3085"); put("gyo", "\u304e\u3087"); put("cu", "\u304f"); 46 put("ku", "\u304f"); put("qu", "\u304f"); put("kwa", "\u304f\u3041"); 47 put("qa", "\u304f\u3041"); put("qwa", "\u304f\u3041"); put("qi", "\u304f\u3043"); 48 put("qwi", "\u304f\u3043"); put("qyi", "\u304f\u3043"); put("qwu", "\u304f\u3045"); 49 put("qe", "\u304f\u3047"); put("qwe", "\u304f\u3047"); put("qye", "\u304f\u3047"); 50 put("qo", "\u304f\u3049"); put("qwo", "\u304f\u3049"); put("qya", "\u304f\u3083"); 51 put("qyu", "\u304f\u3085"); put("qyo", "\u304f\u3087"); put("gu", "\u3050"); 52 put("gwa", "\u3050\u3041"); put("gwi", "\u3050\u3043"); put("gwu", "\u3050\u3045"); 53 put("gwe", "\u3050\u3047"); put("gwo", "\u3050\u3049"); put("ke", "\u3051"); 54 put("ge", "\u3052"); put("co", "\u3053"); put("ko", "\u3053"); 55 put("go", "\u3054"); put("sa", "\u3055"); put("za", "\u3056"); 56 put("ci", "\u3057"); put("shi", "\u3057"); put("si", "\u3057"); 57 put("syi", "\u3057\u3043"); put("she", "\u3057\u3047"); put("sye", "\u3057\u3047"); 58 put("sha", "\u3057\u3083"); put("sya", "\u3057\u3083"); put("shu", "\u3057\u3085"); 59 put("syu", "\u3057\u3085"); put("sho", "\u3057\u3087"); put("syo", "\u3057\u3087"); 60 put("ji", "\u3058"); put("zi", "\u3058"); put("jyi", "\u3058\u3043"); 61 put("zyi", "\u3058\u3043"); put("je", "\u3058\u3047"); put("jye", "\u3058\u3047"); 62 put("zye", "\u3058\u3047"); put("ja", "\u3058\u3083"); put("jya", "\u3058\u3083"); 63 put("zya", "\u3058\u3083"); put("ju", "\u3058\u3085"); put("jyu", "\u3058\u3085"); 64 put("zyu", "\u3058\u3085"); put("jo", "\u3058\u3087"); put("jyo", "\u3058\u3087"); 65 put("zyo", "\u3058\u3087"); put("su", "\u3059"); put("swa", "\u3059\u3041"); 66 put("swi", "\u3059\u3043"); put("swu", "\u3059\u3045"); put("swe", "\u3059\u3047"); 67 put("swo", "\u3059\u3049"); put("zu", "\u305a"); put("ce", "\u305b"); 68 put("se", "\u305b"); put("ze", "\u305c"); put("so", "\u305d"); 69 put("zo", "\u305e"); put("ta", "\u305f"); put("da", "\u3060"); 70 put("chi", "\u3061"); put("ti", "\u3061"); put("cyi", "\u3061\u3043"); 71 put("tyi", "\u3061\u3043"); put("che", "\u3061\u3047"); put("cye", "\u3061\u3047"); 72 put("tye", "\u3061\u3047"); put("cha", "\u3061\u3083"); put("cya", "\u3061\u3083"); 73 put("tya", "\u3061\u3083"); put("chu", "\u3061\u3085"); put("cyu", "\u3061\u3085"); 74 put("tyu", "\u3061\u3085"); put("cho", "\u3061\u3087"); put("cyo", "\u3061\u3087"); 75 put("tyo", "\u3061\u3087"); put("di", "\u3062"); put("dyi", "\u3062\u3043"); 76 put("dye", "\u3062\u3047"); put("dya", "\u3062\u3083"); put("dyu", "\u3062\u3085"); 77 put("dyo", "\u3062\u3087"); put("ltsu", "\u3063"); put("ltu", "\u3063"); 78 put("xtu", "\u3063"); put("", "\u3063"); put("tsu", "\u3064"); 79 put("tu", "\u3064"); put("tsa", "\u3064\u3041"); put("tsi", "\u3064\u3043"); 80 put("tse", "\u3064\u3047"); put("tso", "\u3064\u3049"); put("du", "\u3065"); 81 put("te", "\u3066"); put("thi", "\u3066\u3043"); put("the", "\u3066\u3047"); 82 put("tha", "\u3066\u3083"); put("thu", "\u3066\u3085"); put("tho", "\u3066\u3087"); 83 put("de", "\u3067"); put("dhi", "\u3067\u3043"); put("dhe", "\u3067\u3047"); 84 put("dha", "\u3067\u3083"); put("dhu", "\u3067\u3085"); put("dho", "\u3067\u3087"); 85 put("to", "\u3068"); put("twa", "\u3068\u3041"); put("twi", "\u3068\u3043"); 86 put("twu", "\u3068\u3045"); put("twe", "\u3068\u3047"); put("two", "\u3068\u3049"); 87 put("do", "\u3069"); put("dwa", "\u3069\u3041"); put("dwi", "\u3069\u3043"); 88 put("dwu", "\u3069\u3045"); put("dwe", "\u3069\u3047"); put("dwo", "\u3069\u3049"); 89 put("na", "\u306a"); put("ni", "\u306b"); put("nyi", "\u306b\u3043"); 90 put("nye", "\u306b\u3047"); put("nya", "\u306b\u3083"); put("nyu", "\u306b\u3085"); 91 put("nyo", "\u306b\u3087"); put("nu", "\u306c"); put("ne", "\u306d"); 92 put("no", "\u306e"); put("ha", "\u306f"); put("ba", "\u3070"); 93 put("pa", "\u3071"); put("hi", "\u3072"); put("hyi", "\u3072\u3043"); 94 put("hye", "\u3072\u3047"); put("hya", "\u3072\u3083"); put("hyu", "\u3072\u3085"); 95 put("hyo", "\u3072\u3087"); put("bi", "\u3073"); put("byi", "\u3073\u3043"); 96 put("bye", "\u3073\u3047"); put("bya", "\u3073\u3083"); put("byu", "\u3073\u3085"); 97 put("byo", "\u3073\u3087"); put("pi", "\u3074"); put("pyi", "\u3074\u3043"); 98 put("pye", "\u3074\u3047"); put("pya", "\u3074\u3083"); put("pyu", "\u3074\u3085"); 99 put("pyo", "\u3074\u3087"); put("fu", "\u3075"); put("hu", "\u3075"); 100 put("fa", "\u3075\u3041"); put("fwa", "\u3075\u3041"); put("fi", "\u3075\u3043"); 101 put("fwi", "\u3075\u3043"); put("fyi", "\u3075\u3043"); put("fwu", "\u3075\u3045"); 102 put("fe", "\u3075\u3047"); put("fwe", "\u3075\u3047"); put("fye", "\u3075\u3047"); 103 put("fo", "\u3075\u3049"); put("fwo", "\u3075\u3049"); put("fya", "\u3075\u3083"); 104 put("fyu", "\u3075\u3085"); put("fyo", "\u3075\u3087"); put("bu", "\u3076"); 105 put("pu", "\u3077"); put("he", "\u3078"); put("be", "\u3079"); 106 put("pe", "\u307a"); put("ho", "\u307b"); put("bo", "\u307c"); 107 put("po", "\u307d"); put("ma", "\u307e"); put("mi", "\u307f"); 108 put("myi", "\u307f\u3043"); put("mye", "\u307f\u3047"); put("mya", "\u307f\u3083"); 109 put("myu", "\u307f\u3085"); put("myo", "\u307f\u3087"); put("mu", "\u3080"); 110 put("me", "\u3081"); put("mo", "\u3082"); put("lya", "\u3083"); 111 put("xya", "\u3083"); put("ya", "\u3084"); put("lyu", "\u3085"); 112 put("xyu", "\u3085"); put("yu", "\u3086"); put("lyo", "\u3087"); 113 put("xyo", "\u3087"); put("yo", "\u3088"); put("ra", "\u3089"); 114 put("ri", "\u308a"); put("ryi", "\u308a\u3043"); put("rye", "\u308a\u3047"); 115 put("rya", "\u308a\u3083"); put("ryu", "\u308a\u3085"); put("ryo", "\u308a\u3087"); 116 put("ru", "\u308b"); put("re", "\u308c"); put("ro", "\u308d"); 117 put("lwa", "\u308e"); put("xwa", "\u308e"); put("wa", "\u308f"); 118 put("wo", "\u3092"); put("nn", "\u3093"); put("xn", "\u3093"); 119 put("vu", "\u30f4"); put("va", "\u30f4\u3041"); put("vi", "\u30f4\u3043"); 120 put("vyi", "\u30f4\u3043"); put("ve", "\u30f4\u3047"); put("vye", "\u30f4\u3047"); 121 put("vo", "\u30f4\u3049"); put("vya", "\u30f4\u3083"); put("vyu", "\u30f4\u3085"); 122 put("vyo", "\u30f4\u3087"); 123 put("bb", "\u3063b"); put("cc", "\u3063c"); put("dd", "\u3063d"); 124 put("ff", "\u3063f"); put("gg", "\u3063g"); put("hh", "\u3063h"); 125 put("jj", "\u3063j"); put("kk", "\u3063k"); put("ll", "\u3063l"); 126 put("mm", "\u3063m"); put("pp", "\u3063p"); put("qq", "\u3063q"); 127 put("rr", "\u3063r"); put("ss", "\u3063s"); put("tt", "\u3063t"); 128 put("vv", "\u3063v"); put("ww", "\u3063w"); put("xx", "\u3063x"); 129 put("yy", "\u3063y"); put("zz", "\u3063z"); put("nb", "\u3093b"); 130 put("nc", "\u3093c"); put("nd", "\u3093d"); put("nf", "\u3093f"); 131 put("ng", "\u3093g"); put("nh", "\u3093h"); put("nj", "\u3093j"); 132 put("nk", "\u3093k"); put("nm", "\u3093m"); put("np", "\u3093p"); 133 put("nq", "\u3093q"); put("nr", "\u3093r"); put("ns", "\u3093s"); 134 put("nt", "\u3093t"); put("nv", "\u3093v"); put("nw", "\u3093w"); 135 put("nx", "\u3093x"); put("nz", "\u3093z"); put("nl", "\u3093l"); 136 put("-", "\u30fc"); put(".", "\u3002"); put(",", "\u3001"); put("?", "\uff1f"); put("/", "\u30fb"); 137 put("@", "\uff20"); put("#", "\uff03"); put("%", "\uff05"); put("&", "\uff06"); put("*", "\uff0a"); 138 put("+", "\uff0b"); put("=", "\uff1d"); put("(", "\uff08"); put(")", "\uff09"); 139 put("~", "\uff5e"); put("\"", "\uff02"); put("'", "\uff07"); put(":", "\uff1a"); put(";", "\uff1b"); 140 put("!", "\uff01"); put("^", "\uff3e"); put("\u00a5", "\uffe5"); put("$", "\uff04"); put("[", "\u300c"); 141 put("]", "\u300d"); put("_", "\uff3f"); put("{", "\uff5b"); put("}", "\uff5d"); 142 put("`", "\uff40"); put("<", "\uff1c"); put(">", "\uff1e"); put("\\", "\uff3c"); put("|", "\uff5c"); 143 put("1", "\uff11"); put("2", "\uff12"); put("3", "\uff13"); put("4", "\uff14"); put("5", "\uff15"); 144 put("6", "\uff16"); put("7", "\uff17"); put("8", "\uff18"); put("9", "\uff19"); put("0", "\uff10"); 145 }}; 146 147 /** Max length of the target text */ 148 private static final int MAX_LENGTH = 4; 149 150 151 /** 152 * Default constructor 153 */ 154 public Romkan() { 155 super(); 156 } 157 158 /*********************************************************************** 159 * LetterConverter's interface 160 ***********************************************************************/ 161 /** @see LetterConverter#convert */ 162 public boolean convert(ComposingText text) { 163 int cursor = text.getCursor(1); 164 165 if (cursor <= 0) { 166 return false; 167 } 168 169 StrSegment[] str = new StrSegment[MAX_LENGTH]; 170 int start = MAX_LENGTH; 171 int checkLength = Math.min(cursor, MAX_LENGTH); 172 for (int i = 1; i <= checkLength; i++) { 173 str[MAX_LENGTH - i] = text.getStrSegment(1, cursor - i); 174 start--; 175 } 176 177 StringBuffer key = new StringBuffer(); 178 while (start < MAX_LENGTH) { 179 for (int i = start; i < MAX_LENGTH; i++) { 180 key.append(str[i].string); 181 } 182 boolean upper = Character.isUpperCase(key.charAt(key.length() - 1)); 183 String match = Romkan.romkanTable.get(key.toString().toLowerCase()); 184 if (match != null) { 185 if (upper) { 186 match = match.toUpperCase(); 187 } 188 StrSegment[] out; 189 if (match.length() == 1) { 190 out = new StrSegment[1]; 191 out[0] = new StrSegment(match, str[start].from, str[MAX_LENGTH - 1].to); 192 text.replaceStrSegment(ComposingText.LAYER1, out, MAX_LENGTH - start); 193 } else { 194 out = new StrSegment[2]; 195 out[0] = new StrSegment(match.substring(0, match.length() - 1), 196 str[start].from, str[MAX_LENGTH - 1].to - 1); 197 out[1] = new StrSegment(match.substring(match.length() - 1), 198 str[MAX_LENGTH - 1].to, str[MAX_LENGTH - 1].to); 199 text.replaceStrSegment(1, out, MAX_LENGTH - start); 200 } 201 return true; 202 } 203 start++; 204 key.delete(0, key.length()); 205 } 206 207 return false; 208 } 209 210 /** @see LetterConverter#setPreferences */ 211 public void setPreferences(SharedPreferences pref) {} 212 } 213