Skip to content

Commit eb227b2

Browse files
author
gustavo-castro_Localiza
committed
Feature: Classe Buscador para ser um novo motor de busca baseado em listas invertidas
1 parent 745e4da commit eb227b2

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package tp03.src.storage.structures.ListaInvertida;
2+
3+
import java.util.*;
4+
import tp03.src.storage.structures.ListaInvertida.ElementoLista;
5+
import tp03.src.storage.structures.ListaInvertida.ListaInvertida;
6+
import tp03.src.storage.structures.ListaInvertida.ListaInvertidaUtils;
7+
8+
public class Buscador {
9+
10+
private ListaInvertida listaSeries;
11+
private ListaInvertida listaEpisodios;
12+
private ListaInvertida listaAtores;
13+
14+
public Buscador(ListaInvertida listaSeries, ListaInvertida listaEpisodios, ListaInvertida listaAtores) {
15+
this.listaSeries = listaSeries;
16+
this.listaEpisodios = listaEpisodios;
17+
this.listaAtores = listaAtores;
18+
}
19+
20+
public List<Integer> buscarSeries(String termos) throws Exception {
21+
return buscar(termos, listaSeries);
22+
}
23+
24+
public List<Integer> buscarEpisodios(String termos) throws Exception {
25+
return buscar(termos, listaEpisodios);
26+
}
27+
28+
public List<Integer> buscarAtores(String termos) throws Exception {
29+
return buscar(termos, listaAtores);
30+
}
31+
32+
private List<Integer> buscar(String termos, ListaInvertida lista) throws Exception {
33+
// Normalizar e processar os termos
34+
ListaInvertidaUtils utils = new ListaInvertidaUtils();
35+
String[] termosProcessados = utils.extractTerms(termos);
36+
Map<Integer, Float> relevancias = new HashMap<>();
37+
38+
for (String termo : termosProcessados) {
39+
ElementoLista[] elementos = lista.read(termo);
40+
if (elementos != null) {
41+
for (ElementoLista elemento : elementos) {
42+
int id = elemento.getId();
43+
float tf = elemento.getFrequencia();
44+
float idf = calcularIDF(lista, termo);
45+
float tfidf = tf * idf;
46+
47+
relevancias.put(id, relevancias.getOrDefault(id, 0f) + tfidf);
48+
}
49+
}
50+
}
51+
52+
// Ordenar os IDs por relevância
53+
List<Map.Entry<Integer, Float>> listaOrdenada = new ArrayList<>(relevancias.entrySet());
54+
listaOrdenada.sort((a, b) -> Float.compare(b.getValue(), a.getValue()));
55+
56+
// Retornar apenas os IDs ordenados
57+
List<Integer> idsOrdenados = new ArrayList<>();
58+
for (Map.Entry<Integer, Float> entry : listaOrdenada) {
59+
idsOrdenados.add(entry.getKey());
60+
}
61+
62+
return idsOrdenados;
63+
}
64+
65+
private float calcularIDF(ListaInvertida lista, String termo) throws Exception {
66+
int totalDocumentos = lista.numeroEntidades();
67+
ElementoLista[] elementos = lista.read(termo);
68+
int documentosComTermo = (elementos != null) ? elementos.length : 0;
69+
70+
if (documentosComTermo == 0) {
71+
return 0;
72+
}
73+
74+
return (float) Math.log((double) totalDocumentos / documentosComTermo);
75+
}
76+
}

tp03/src/storage/structures/ListaInvertida/ListaInvertidaUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static String normalize(String str) {
4646

4747
// Remove stop words de um array de termos e retorna um novo array sem as stop
4848
// words
49-
private String[] extractTerms(String texto) {
49+
public String[] extractTerms(String texto) {
5050
texto = texto.toLowerCase();
5151
String[] termosSeparados = texto.split(" ");
5252
ArrayList<String> result = new ArrayList<>();

0 commit comments

Comments
 (0)