kssr3951

googleIME & Gson

Jul 20th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.lang.reflect.Type;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import com.google.gson.Gson;
  14. import com.google.gson.reflect.TypeToken;
  15.  
  16. public class Test {
  17.  
  18.     public static void main(String[] args) {
  19.         // 1文節
  20.         toIME("とうきょうとっきょきょかきょくきょくちょう");
  21.  
  22.         // 4文節
  23.         toIME("なかのくにおすまいのやまだたろうさん");
  24.  
  25.         // 末尾に「,」を付けると文節を区切らないよう指定できる。
  26.         // https://www.google.co.jp/ime/cgiapi.html
  27.         toIME("なかのくにおすまいのやまだたろうさん,");
  28.     }
  29.  
  30.     @SuppressWarnings("rawtypes")
  31.     public static String toIME(String kana) {
  32.         URL url;
  33.         try {
  34.                         url = new URL("http://google.co.jp/transliterate?langpair=ja-Hira%7cja&text="
  35.                                 + URLEncoder.encode(kana, "UTF8"));
  36.             URLConnection conn = url.openConnection();
  37.             InputStream in = conn.getInputStream();
  38.  
  39.             Map<String,String> map = Json2Map(in);
  40.             if (map == null) { return null; }
  41.             Iterator entries = map.entrySet().iterator();
  42.             StringBuilder sb = new StringBuilder();
  43.             while(entries.hasNext()) {
  44.                 Map.Entry entry = (Map.Entry)entries.next();
  45.                 sb.append((String)entry.getValue());
  46.             }
  47.             return sb.toString();
  48.         } catch (Exception e) {
  49.             e.printStackTrace();
  50.             return null;
  51.         }
  52.     }
  53.  
  54.     public static Map<String,String> Json2Map(InputStream googleIME) {
  55.         try {
  56.             BufferedReader br = new BufferedReader(new InputStreamReader(googleIME,"UTF-8"));
  57.             StringBuilder sb = new StringBuilder();
  58.             String line;
  59.             while ((line = br.readLine()) != null) {
  60.                 sb.append(line);
  61.             }
  62.             br.close();
  63.             System.out.println("------------------------------------------------");
  64.             System.out.println("googleIMEの処理結果 : " + sb.toString());
  65.             Gson gson = new Gson();
  66.             //Object[] oList = gson.fromJson(sb.toString(), Object[].class);
  67.  
  68.             Type listType = new TypeToken<List>(){}.getType();
  69.             List posts = (List) gson.fromJson(sb.toString(), listType);
  70.  
  71.             for (Object o : posts) {
  72.                 List list = (List) o;
  73.                 // 元の文字列
  74.                 System.out.println("元の文字列   : " + list.get(0).toString());
  75.  
  76.                 // 変換候補
  77.                 List koho = (List) list.get(1);
  78.                 // 変換候補1個目
  79.                 System.out.println("変換候補1   : " + koho.get(0));
  80.  
  81.                 // 変換候補全部
  82.                 System.out.print("変換候補全部 : ");
  83.                 for (Object oo : koho) {
  84.                     System.out.print(oo.toString() + " / ");
  85.                 }
  86.                 System.out.println();
  87.                 System.out.println();
  88.             }
  89.             return null;
  90.         } catch(Exception e) {
  91.             e.printStackTrace();
  92.             return null;
  93.         }
  94.     }
  95.  
  96. }
Add Comment
Please, Sign In to add comment