GoogleDictionary.java
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* GoogleTranslater.java
*
* Created on March 23, 2007, 10:01 PM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Baseado no language-translate:
*
* http://code.google.com/p/language-translate/
*
* 09/12/2009
*
*/
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
/*
* 3rd party Apache Libs
*/
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
/*
* 3rd party Jericho libs
*/
import au.id.jericho.lib.html.*;
/**
* A module for taking an input string along with country codes (source/target)
* and converts it to the target language. It uses Google's Language Tool
* site to do the actual translating.
*
* This program was done to simplify and speed up my ability to translate
* quick message.
*
* @author uuklanger
*/
public class GoogleDictionary {
String paginaHtml = null;
// =============================================================
// Constructors and Init
// =============================================================
/**
* Creates a new instance of GoogleTranslater
*/
public GoogleDictionary() {
}
// =============================================================
// Public Methods
// =============================================================
public void setDictionary(String word, String sourcelang, String targetlang) {
String s = word;
try {
s = URLEncoder.encode(word, "UTF-8");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(GoogleDictionary.class.getName()).log(Level.SEVERE, null, ex);
}
paginaHtml = getUrl("http://www.google.com/dictionary?source=translation&hl=pt&q="+ s + "&langpair=" + sourcelang + "|" + targetlang);
}
public void setTranslate(String frase, String sourcelang, String targetlang) {
String s = frase;
try {
s = URLEncoder.encode(frase, "UTF-8");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(GoogleDictionary.class.getName()).log(Level.SEVERE, null, ex);
}
paginaHtml = getUrl("http://www.google.com/translate_t?hl=en&ie=UTF8&text=" + s + "&langpair=" + sourcelang + "%7C" + targetlang);
}
/**
* translates source string (sin) into a response using the source/target
* languages.
*
* @param sin String in
* @param sourcelang source language (ISO3166 code)
* @param targetlang target language (ISO3166 code)
* @return returns a text string in the target language
*/
public String getUrl(String url) {
HttpClient client = new HttpClient();
HttpMethod method = null;
String responsebody = null;
try {
//System.out.println(url);
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
method = new GetMethod(url);
method.setFollowRedirects(true);
client.executeMethod(method);
responsebody = method.getResponseBodyAsString();
method.releaseConnection();
} catch (HttpException he) {
System.err.println("Http error connecting to '" + url + "'");
System.err.println(he.getMessage());
} catch (IOException ioe){
System.err.println("Unable to connect to '" + url + "'");
} catch(Exception ex) {
System.err.println("EXCEPTION: " + ex.getMessage());
} // end-try-catch
return responsebody;
}
// =============================================================
// Private Methods
// =============================================================
/**
* Using jericho libraries, this will extract the response
* text from the HTML blob returned from google Language Tools.
*
* @param html HTML blob
* @return Response text
*/
public int getClassesGramaticais() {
//if (true) return html;
String attribute = null;
Element linkElement = null;
List linkElements = null;
String traducao;
int classes = ClassesGramaticais.UNKNOWN;
try {
Source source = new Source(paginaHtml);
source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr
source.fullSequentialParse();
linkElements=source.findAllElements(HTMLElementName.SPAN);
for (Iterator i=linkElements.iterator(); i.hasNext();) {
linkElement=(Element)i.next();
attribute=linkElement.getAttributeValue("title");
if (attribute != null) {
if (attribute.equals("Part-of-speech")) {
traducao = linkElement.getContent().extractText();
if (traducao.compareToIgnoreCase("article")==0)
classes |= ClassesGramaticais.ARTIGO;
if (traducao.compareToIgnoreCase("adjective")==0)
classes |= ClassesGramaticais.ADJETIVO;
if (traducao.compareToIgnoreCase("adverb")==0)
classes |= ClassesGramaticais.ADVERBIO;
if (traducao.compareToIgnoreCase("conjunction")==0)
classes |= ClassesGramaticais.CONJUNCAO;
if (traducao.compareToIgnoreCase("interjection")==0)
classes |= ClassesGramaticais.INTERJEICAO;
if (traducao.compareToIgnoreCase("numeral")==0)
classes |= ClassesGramaticais.NUMERAL;
if (traducao.compareToIgnoreCase("preposition")==0)
classes |= ClassesGramaticais.PREPOSICAO;
if (traducao.compareToIgnoreCase("pronoun")==0)
classes |= ClassesGramaticais.PRONOME;
if (traducao.compareToIgnoreCase("noun")==0)
classes |= ClassesGramaticais.SUBSTANTIVO;
if (traducao.compareToIgnoreCase("auxiliary")==0)
classes |= ClassesGramaticais.VERBO;
if (traducao.compareToIgnoreCase("verb")==0)
classes |= ClassesGramaticais.VERBO;
/* Esses aqui sao unknown
if (traducao.compareToIgnoreCase("prefix")==0)
traducao = "unk";
if (traducao.compareToIgnoreCase("abbreviation")==0)
traducao = "unk";
if (traducao.compareToIgnoreCase("phrase")==0)
traducao = "unk";
if (traducao.compareToIgnoreCase("particle")==0)
traducao = "unk";
*/
} // end-if
} // end-if
} // end-for
} catch(Exception ex) {
System.err.println("EXCEPTION: " + ex.getMessage());
} // end-try-catch
return classes;
}
public String getSignificado() {
//if (true) return html;
String attribute = null;
Element linkElement = null;
List linkElements = null;
String significado="";
try {
Source source = new Source(paginaHtml);
source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr
source.fullSequentialParse();
linkElements=source.findAllElements(HTMLElementName.SPAN);
for (Iterator i=linkElements.iterator(); i.hasNext();) {
linkElement=(Element)i.next();
attribute=linkElement.getAttributeValue("title");
if (attribute != null) {
if (attribute.equals("Part-of-speech")) {
significado += "\n" + linkElement.getContent().extractText() + "\n";
} // end-if
} // end-if
attribute=linkElement.getAttributeValue("class");
if (attribute != null) {
if (attribute.equals("dct-tt")) {
significado += "\t" + linkElement.getContent().extractText() + "\n";
} // end-if
} // end-if
} // end-for
} catch(Exception ex) {
System.err.println("EXCEPTION: " + ex.getMessage());
} // end-try-catch
return significado;
}
// =============================================================
// TEST MAIN
// =============================================================
/**
* Test main for development
* @param args the command line arguments
*/
public static void main(String[] args) {
GoogleDictionary gt = new GoogleDictionary();
String palavra = "casa";
int classes = ClassesGramaticais.UNKNOWN;
String significado;
gt.setDictionary(palavra,"pt","en");
classes = gt.getClassesGramaticais();
significado = gt.getSignificado();
System.out.print(palavra + " [" + ClassesGramaticais.getClassesGramaticaisStr(classes)+"]\n");
System.out.println("significado:\n" + significado);
System.exit(0);
}
} // end-class