-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pais.java
311 lines (227 loc) · 10.1 KB
/
Pais.java
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package buscapaises;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Pais implements Serializable {
private String nome;
private String idh;
private String areaTotal;
private Map<String, Integer> nomeOficial;
private Map<String, Integer> capital;
private Map<String, Integer> linguas;
private Map<String, Integer> religiao;
private Map<String, Integer> moeda;
public Map<String, Integer> descricao;
private float pontuacao;
private transient TratamentoDeTexto tratamentoDeTexto;
public Pais(String nome, String wikipediaUrl, TratamentoDeTexto tratamentoDeTexto) throws IOException {
this.tratamentoDeTexto = tratamentoDeTexto;
this.nome = nome;
Document htmlDocument = Jsoup.connect(wikipediaUrl).get();
this.nomeOficial = buscarNome(htmlDocument);
this.capital = buscarCapital(htmlDocument);
this.linguas = buscarLingua(htmlDocument);
this.religiao = buscarReligiao(htmlDocument);
this.areaTotal = buscarAreaTotal(htmlDocument);
this.moeda = buscarMoeda(htmlDocument);
this.idh = buscarIdh(htmlDocument);
this.descricao = buscarDescricao(htmlDocument);
}
private Map<String, Integer> buscarNome(Document htmlDocument) throws NullPointerException {
try {
Element nomeElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr:first-child td:first-child b:first-of-type");
String nomeTexto = tratamentoDeTexto.limparTexto(nomeElement.text());
ArrayList<String> palavras = tratamentoDeTexto.quebrarTextoEmPalavras(nomeTexto);
palavras = tratamentoDeTexto.removerStopWords(palavras);
return tratamentoDeTexto.computarFrequenciaDasPalavras(palavras);
} catch (NullPointerException e) {
e.printStackTrace();
return new HashMap<String, Integer>();
}
}
private Map<String, Integer> buscarCapital(Document htmlDocument) throws NullPointerException {
try {
Element capitalElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr td:first-child a[title=Capital]").parent().parent().nextElementSibling();
String capitalTexto = tratamentoDeTexto.limparTexto(capitalElement.selectFirst("a:first-of-type").text());
ArrayList<String> palavras = tratamentoDeTexto.quebrarTextoEmPalavras(capitalTexto);
palavras = tratamentoDeTexto.removerStopWords(palavras);
return tratamentoDeTexto.computarFrequenciaDasPalavras(palavras);
} catch (NullPointerException e) {
e.printStackTrace();
return new HashMap<String, Integer>();
}
}
private Map<String, Integer> buscarLingua(Document htmlDocument) throws NullPointerException {
try {
Element linguaElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr td:first-child a[title=Língua oficial]").parent().parent().nextElementSibling();
String linguaTexto = tratamentoDeTexto.limparTexto(
linguaElement.select("a").size() > 0 ? linguaElement.select("a").text() : linguaElement.text()
);
ArrayList<String> palavras = tratamentoDeTexto.quebrarTextoEmPalavras(linguaTexto);
palavras = tratamentoDeTexto.removerStopWords(palavras);
return tratamentoDeTexto.computarFrequenciaDasPalavras(palavras);
} catch (NullPointerException e) {
e.printStackTrace();
return new HashMap<String, Integer>();
}
}
private Map<String, Integer> buscarReligiao(Document htmlDocument) throws NullPointerException {
try {
Element religiaoElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr td:first-child a[title=Religião oficial]");
if(religiaoElement != null) {
String religiaoTexto = tratamentoDeTexto.limparTexto(religiaoElement.parent().parent().nextElementSibling().select("a").text());
ArrayList<String> palavras = tratamentoDeTexto.quebrarTextoEmPalavras(religiaoTexto);
palavras = tratamentoDeTexto.removerStopWords(palavras);
return tratamentoDeTexto.computarFrequenciaDasPalavras(palavras);
}
return null;
} catch(NullPointerException e) {
e.printStackTrace();
return null;
}
}
private String buscarAreaTotal(Document htmlDocument) throws NullPointerException {
try {
Element areaElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr td:first-child a[title=Lista de países e territórios por área]").parent().parent().parent().nextElementSibling();
String areaTexto = areaElement.select("td:nth-child(2)").text();
areaTexto = areaTexto.replaceAll("\\[(.*?)\\]", " ");
areaTexto = areaTexto.split("\\W*( km²)\\W*")[0].replaceAll("[^\\d.,/\\s]", "").trim() + " km²";
areaTexto = areaTexto.replaceAll("(?<=\\d)\\s(?=\\d)", ".");
return areaTexto;
} catch(NullPointerException e) {
e.printStackTrace();
return null;
}
}
private Map<String, Integer> buscarMoeda(Document htmlDocument) throws NullPointerException {
try {
Element moedaElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr td:first-child a[title=Moeda]").parent().parent().nextElementSibling();
if(moedaElement.select("a").size() > 0) {
String moedaTexto = tratamentoDeTexto.limparTexto(
moedaElement.select("a").text().split("[(]")[0]
);
ArrayList<String> palavras = tratamentoDeTexto.quebrarTextoEmPalavras(moedaTexto);
palavras = tratamentoDeTexto.removerStopWords(palavras);
return tratamentoDeTexto.computarFrequenciaDasPalavras(palavras);
}
return new HashMap<String, Integer>();
} catch(NullPointerException e) {
e.printStackTrace();
return new HashMap<String, Integer>();
}
}
private String buscarIdh(Document htmlDocument) throws NullPointerException {
try {
Element idhElement = htmlDocument.selectFirst(".mw-parser-output tbody:first-of-type tr td:first-child a[title=Índice de Desenvolvimento Humano]").parent().parent().nextElementSibling();
String idhTexto = idhElement.text().split(" ")[0];
return idhTexto;
} catch (NullPointerException e) {
return null;
}
}
private Map<String, Integer> buscarDescricao(Document htmlDocument) throws NullPointerException {
try {
Elements paragrafos = htmlDocument.select("p");
String descricao = "";
for(Element p : paragrafos) {
descricao += p.text();
}
descricao = tratamentoDeTexto.limparTexto(descricao);
ArrayList<String> palavras = tratamentoDeTexto.quebrarTextoEmPalavras(descricao);
palavras = tratamentoDeTexto.removerStopWords(palavras);
return tratamentoDeTexto.computarFrequenciaDasPalavras(palavras);
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
public float gerarPontuacao(String palavraChave) {
int campoAtributo = 0;
ArrayList<Map<String,Integer>> listaAtributos = new ArrayList<>();
if(this.nomeOficial != null) listaAtributos.add(this.nomeOficial);
if(this.capital != null) listaAtributos.add(this.capital);
if(this.linguas != null) listaAtributos.add(this.linguas);
if(this.religiao != null) listaAtributos.add(this.religiao);
if(this.moeda != null) listaAtributos.add(this.moeda);
for(Map<String,Integer> atributo : listaAtributos) {
if(atributo.equals(this.nomeOficial))
campoAtributo = 1;
if(atributo.equals(this.capital))
campoAtributo = 2;
if(atributo.equals(this.linguas))
campoAtributo = 3;
if(atributo.equals(this.religiao))
campoAtributo = 4;
if(atributo.equals(this.moeda))
campoAtributo = 5;
if(atributo.containsKey(palavraChave))
this.pontuacao += (1.0/campoAtributo)*atributo.get(palavraChave);
}
if(this.descricao.containsKey(palavraChave))
this.pontuacao += 0.1*this.descricao.get(palavraChave);
return this.pontuacao;
}
public String getNome() {
return this.nome;
}
public static Map<String, String> buscarPaisesDaOnu() throws IOException, NullPointerException {
try {
Document html = Jsoup.connect("https://pt.wikipedia.org/wiki/Estados-membros_das_Na%C3%A7%C3%B5es_Unidas").get();
Elements letterCountries = html.select("h3 ~ ul");
Map<String, String> countries = new HashMap<String, String>();
for (int i = 0; i < 23; i++) {
for(Element line : letterCountries.get(i).select("li a")) {
if(!line.text().matches(".*\\d+.*") && !line.absUrl("href").contains(".svg") && line.hasAttr("title") && line.text().length() > 0) {
countries.put(line.text(), line.absUrl("href"));
}
}
}
return countries;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
}
}
public static boolean salvarPais(Pais pais, String diretorio) throws IOException {
try {
File file = new File(diretorio);
File parent = file.getParentFile();
if(!parent.exists())
parent.mkdir();
file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(diretorio);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(pais);
out.close();
fileOut.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static Pais lerPais(String diretorio) throws IOException, ClassNotFoundException {
try {
FileInputStream file = new FileInputStream(diretorio);
ObjectInputStream inputStream = new ObjectInputStream(file);
Pais pais = (Pais) inputStream.readObject();
inputStream.close();
file.close();
return pais;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
}