Mercurial > hg > mpdl-group
comparison software/mpdl-services/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/translator/MicrosoftTranslator.java @ 23:e845310098ba
diverse Korrekturen
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 27 Nov 2012 12:35:19 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
22:6a45a982c333 | 23:e845310098ba |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.cms.translator; | |
2 | |
3 import java.util.ArrayList; | |
4 | |
5 import com.memetix.mst.language.Language; | |
6 import com.memetix.mst.translate.Translate; | |
7 import com.memetix.mst.detect.Detect; | |
8 | |
9 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
10 | |
11 public class MicrosoftTranslator { | |
12 private static final String KEY = "474A4E72DB217E37031EC190ACB4159378A6917C"; // free key to use with Microsoft server | |
13 private static String[] DEST_LANGUAGES = {"eng", "ger", "fra"}; | |
14 | |
15 private MicrosoftTranslator() { | |
16 // nothing: prevent instantiation | |
17 } | |
18 | |
19 public static String[] getDestLanguages() { | |
20 return DEST_LANGUAGES; | |
21 } | |
22 | |
23 public static String translate(String query, String toLanguageStr) throws ApplicationException { | |
24 if (toLanguageStr == null) | |
25 throw new ApplicationException("MicrosoftTranslator: toLanguageStr is null"); | |
26 String translation = null; | |
27 try { | |
28 Translate.setKey(KEY); // Set the API key once per JVM. It is set statically and applies to all services | |
29 String langId = de.mpg.mpiwg.berlin.mpdl.lt.general.Language.getInstance().getLanguageId(toLanguageStr); // e.g. "de" is delivered from "deu" | |
30 Language toLanguage = Language.fromString(langId); | |
31 translation = Translate.execute(query, toLanguage); | |
32 translation = translation.toLowerCase(); | |
33 } catch (Exception e) { | |
34 throw new ApplicationException(e); | |
35 } | |
36 return translation; | |
37 } | |
38 | |
39 public static String translate(String query, String fromLanguageStr, String toLanguageStr) throws ApplicationException { | |
40 if (fromLanguageStr == null) | |
41 throw new ApplicationException("MicrosoftTranslator: toLanguageStr is null"); | |
42 if (toLanguageStr == null) | |
43 throw new ApplicationException("MicrosoftTranslator: fromLanguageStr is null"); | |
44 String translation = null; | |
45 try { | |
46 Translate.setKey(KEY); | |
47 String fromLangId = de.mpg.mpiwg.berlin.mpdl.lt.general.Language.getInstance().getLanguageId(fromLanguageStr); // e.g. "de" is delivered from "deu" | |
48 String toLangId = de.mpg.mpiwg.berlin.mpdl.lt.general.Language.getInstance().getLanguageId(toLanguageStr); // e.g. "de" is delivered from "deu" | |
49 Language fromLanguage = Language.fromString(fromLangId); | |
50 Language toLanguage = Language.fromString(toLangId); | |
51 translation = Translate.execute(query, fromLanguage, toLanguage); | |
52 translation = translation.toLowerCase(); | |
53 } catch (Exception e) { | |
54 throw new ApplicationException(e); | |
55 } | |
56 return translation; | |
57 } | |
58 | |
59 public static ArrayList<String> translate(String[] query, String fromLanguageStr, String[] toLanguagesStr) throws ApplicationException { | |
60 ArrayList<String> translations = new ArrayList<String>(); | |
61 for (int i=0; i<toLanguagesStr.length; i++) { | |
62 String toLanguageStr = toLanguagesStr[i]; | |
63 String[] translationsArray = translate(query, fromLanguageStr, toLanguageStr); | |
64 for (int j=0; j<translationsArray.length; j++) { | |
65 String translation = translationsArray[j]; | |
66 translations.add(translation); | |
67 } | |
68 } | |
69 return translations; | |
70 } | |
71 | |
72 public static String[] translate(String[] query, String fromLanguageStr, String toLanguageStr) throws ApplicationException { | |
73 if (fromLanguageStr == null) | |
74 throw new ApplicationException("MicrosoftTranslator: toLanguageStr is null"); | |
75 if (toLanguageStr == null) | |
76 throw new ApplicationException("MicrosoftTranslator: fromLanguageStr is null"); | |
77 String[] translation = null; | |
78 try { | |
79 Translate.setKey(KEY); | |
80 String fromLangId = de.mpg.mpiwg.berlin.mpdl.lt.general.Language.getInstance().getLanguageId(fromLanguageStr); // e.g. "de" is delivered from "deu" | |
81 String toLangId = de.mpg.mpiwg.berlin.mpdl.lt.general.Language.getInstance().getLanguageId(toLanguageStr); // e.g. "de" is delivered from "deu" | |
82 Language fromLanguage = Language.fromString(fromLangId); | |
83 Language toLanguage = Language.fromString(toLangId); | |
84 translation = Translate.execute(query, fromLanguage, toLanguage); | |
85 for (int i=0; i<translation.length; i++) { | |
86 translation[i] = translation[i].toLowerCase(); | |
87 } | |
88 } catch (Exception e) { | |
89 throw new ApplicationException(e); | |
90 } | |
91 return translation; | |
92 } | |
93 | |
94 public static String detectLanguageCode(String query) throws ApplicationException { | |
95 String langCode = null; | |
96 try { | |
97 Detect.setKey(KEY); | |
98 Language detectedLanguage = Detect.execute(query); | |
99 if (detectedLanguage != null) | |
100 langCode = detectedLanguage.toString(); | |
101 } catch (Exception e) { | |
102 throw new ApplicationException(e); | |
103 } | |
104 return langCode; | |
105 } | |
106 | |
107 public static String detectLanguageName(String query) throws ApplicationException { | |
108 String langName = null; | |
109 try { | |
110 Detect.setKey(KEY); | |
111 Language detectedLanguage = Detect.execute(query); | |
112 if (detectedLanguage != null) | |
113 langName = detectedLanguage.getName(Language.GERMAN); | |
114 } catch (Exception e) { | |
115 throw new ApplicationException(e); | |
116 } | |
117 return langName; | |
118 } | |
119 | |
120 } |