Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TranslateCommand.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo.commands;
  23. import com.md87.charliebravo.Command;
  24. import com.md87.charliebravo.Followup;
  25. import com.md87.charliebravo.InputHandler;
  26. import com.md87.charliebravo.Response;
  27. import java.io.BufferedReader;
  28. import java.io.IOException;
  29. import java.io.InputStreamReader;
  30. import java.net.MalformedURLException;
  31. import java.net.URL;
  32. import java.net.URLConnection;
  33. import java.net.URLEncoder;
  34. import java.nio.charset.Charset;
  35. import org.json.JSONException;
  36. import org.json.JSONObject;
  37. /**
  38. *
  39. * @author chris
  40. */
  41. public class TranslateCommand implements Command {
  42. protected static final String[][] LANGUAGES = {
  43. {"AFRIKAANS", "af"},
  44. {"ALBANIAN", "sq"},
  45. {"AMHARIC", "am"},
  46. {"ARABIC", "ar"},
  47. {"ARMENIAN", "hy"},
  48. {"AZERBAIJANI", "az"},
  49. {"BASQUE", "eu"},
  50. {"BELARUSIAN", "be"},
  51. {"BENGALI", "bn"},
  52. {"BIHARI", "bh"},
  53. {"BULGARIAN", "bg"},
  54. {"BURMESE", "my"},
  55. {"CATALAN", "ca"},
  56. {"CHEROKEE", "chr"},
  57. {"CHINESE", "zh"},
  58. {"CHINESE (SIMPLIFIED)", "zh-CN"},
  59. {"CHINESE (TRADITIONAL)", "zh-TW"},
  60. {"SIMPLIFIED CHINESE", "zh-CN"},
  61. {"TRADITIONAL CHINESE", "zh-TW"},
  62. {"CROATIAN", "hr"},
  63. {"CZECH", "cs"},
  64. {"DANISH", "da"},
  65. {"DHIVEHI", "dv"},
  66. {"DUTCH", "nl"},
  67. {"ENGLISH", "en"},
  68. {"ESPERANTO", "eo"},
  69. {"ESTONIAN", "et"},
  70. {"FILIPINO", "tl"},
  71. {"FINNISH", "fi"},
  72. {"FRENCH", "fr"},
  73. {"GALICIAN", "gl"},
  74. {"GEORGIAN", "ka"},
  75. {"GERMAN", "de"},
  76. {"GREEK", "el"},
  77. {"GUARANI", "gn"},
  78. {"GUJARATI", "gu"},
  79. {"HEBREW", "iw"},
  80. {"HINDI", "hi"},
  81. {"HUNGARIAN", "hu"},
  82. {"ICELANDIC", "is"},
  83. {"INDONESIAN", "id"},
  84. {"INUKTITUT", "iu"},
  85. {"ITALIAN", "it"},
  86. {"JAPANESE", "ja"},
  87. {"KANNADA", "kn"},
  88. {"KAZAKH", "kk"},
  89. {"KHMER", "km"},
  90. {"KOREAN", "ko"},
  91. {"KURDISH", "ku"},
  92. {"KYRGYZ", "ky"},
  93. {"LAOTHIAN", "lo"},
  94. {"LATVIAN", "lv"},
  95. {"LITHUANIAN", "lt"},
  96. {"MACEDONIAN", "mk"},
  97. {"MALAY", "ms"},
  98. {"MALAYALAM", "ml"},
  99. {"MALTESE", "mt"},
  100. {"MARATHI", "mr"},
  101. {"MONGOLIAN", "mn"},
  102. {"NEPALI", "ne"},
  103. {"NORWEGIAN", "no"},
  104. {"ORIYA", "or"},
  105. {"PASHTO", "ps"},
  106. {"PERSIAN", "fa"},
  107. {"POLISH", "pl"},
  108. {"PORTUGUESE", "pt-PT"},
  109. {"PUNJABI", "pa"},
  110. {"ROMANIAN", "ro"},
  111. {"RUSSIAN", "ru"},
  112. {"SANSKRIT", "sa"},
  113. {"SERBIAN", "sr"},
  114. {"SINDHI", "sd"},
  115. {"SINHALESE", "si"},
  116. {"SLOVAK", "sk"},
  117. {"SLOVENIAN", "sl"},
  118. {"SPANISH", "es"},
  119. {"SWAHILI", "sw"},
  120. {"SWEDISH", "sv"},
  121. {"TAJIK", "tg"},
  122. {"TAMIL", "ta"},
  123. {"TAGALOG", "tl"},
  124. {"TELUGU", "te"},
  125. {"THAI", "th"},
  126. {"TIBETAN", "bo"},
  127. {"TURKISH", "tr"},
  128. {"UKRAINIAN", "uk"},
  129. {"URDU", "ur"},
  130. {"UZBEK", "uz"},
  131. {"UIGHUR", "ug"},
  132. {"VIETNAMESE", "vi"},
  133. };
  134. public void execute(final InputHandler handler, Response response, String line) throws MalformedURLException, IOException, JSONException {
  135. String target = "en";
  136. String text = line;
  137. int offset;
  138. if ((offset = text.lastIndexOf(" into ")) > -1) {
  139. final String lang = text.substring(offset + 6);
  140. for (String[] pair : LANGUAGES) {
  141. if (pair[0].equalsIgnoreCase(lang)) {
  142. target = pair[1];
  143. text = text.substring(0, offset);
  144. }
  145. }
  146. }
  147. URL url = new URL("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=%7C" + target + "&q=" +
  148. URLEncoder.encode(text, Charset.defaultCharset().name()));
  149. URLConnection connection = url.openConnection();
  150. connection.addRequestProperty("Referer", "http://chris.smith.name/");
  151. String input;
  152. StringBuilder builder = new StringBuilder();
  153. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  154. while((input = reader.readLine()) != null) {
  155. builder.append(input);
  156. }
  157. JSONObject json = new JSONObject(builder.toString());
  158. if (json.getInt("responseStatus") != 200) {
  159. throw new IOException(json.getString("responseDetails"));
  160. }
  161. response.sendMessage("that translates to \""
  162. + json.getJSONObject("responseData").getString("translatedText") + "\"");
  163. response.addFollowup(new LanguageFollowup(json.getJSONObject("responseData")));
  164. }
  165. protected static class LanguageFollowup implements Followup {
  166. private final JSONObject result;
  167. public LanguageFollowup(JSONObject result) {
  168. this.result = result;
  169. }
  170. public boolean matches(String line) {
  171. return line.equals("language");
  172. }
  173. public void execute(final InputHandler handler, Response response, String line) throws Exception {
  174. final String target = result.getString("detectedSourceLanguage");
  175. for (String[] pair : LANGUAGES) {
  176. if (pair[1].equalsIgnoreCase(target)) {
  177. response.sendMessage("the language was detected as "
  178. + pair[0].charAt(0) + pair[0].substring(1).toLowerCase());
  179. return;
  180. }
  181. }
  182. response.sendMessage("the language was detected as '"
  183. + target + "', but I don't know what that is");
  184. }
  185. }
  186. }