-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanRetrieval.java
314 lines (277 loc) · 14.9 KB
/
BooleanRetrieval.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
312
313
314
import java.io.File;
import java.util.*;
/*
* Inverted Index for information retrieval
*
* Created by Tony Vazgar on 8/22/18.
* Copyright © 2018 Tony Vazgar. All rights reserved.
* Contact: luis.vazquezga@udlap.mx
*/
public class BooleanRetrieval{
/*
* Metodo para indexar los archivos.
*/
public static Map<String, LinkedList<Integer>> index(String[] files){
Scanner scanner;
File file;
Set<String> diccionario = new HashSet<String>(); //Here goes every word but its repited
List diccionariOrdenado; //To clone the dictionary but with no repited words
ArrayList<String[]> palabrasArchivos = new ArrayList<String[]>(); //Here goes every word of each document to storage them and have not to read the files many times
Map<String, LinkedList<Integer>> invertedIndex = new HashMap<String, LinkedList<Integer>>(); //The "dictionary" for the inverted index with its respective posting list
///////////////////////
System.out.println("Leyendo el archivo........");
//For every document in the array of documents we storage thw words to make it faster and not have to read every moment
for(int i = 0; i < files.length; i++){
try{
file = new File("./" + files[i] + ".txt");
scanner = new Scanner(file);
ArrayList<String> lecturas = new ArrayList<String>();
while (scanner.hasNext()){
String word = scanner.next().toLowerCase() ;
diccionario.add(word); //Se agregan al diccionario global
lecturas.add(word); //Se a palabras de cada archivo
}
int numberOfWords = lecturas.size(); //Para saber el numero de palabras para el texto en el que vamos
String[] palabras = new String[numberOfWords]; //Se hace arreglo para cada uno exacto
for (int j = 0; j < numberOfWords; j++) {
palabras[j] = lecturas.get(j); //Se agregan
}
Arrays.sort(palabras); //Se guardan en orden alfabetico
palabrasArchivos.add(palabras); //se agrega el conjuto de palabras al repositorio global
}catch (Exception e){
System.err.println("No existe ese documento :(");
}
}
System.out.println("Lectura finalizada.");
diccionariOrdenado = new ArrayList(diccionario); //To delete duplicates and only have one word
Collections.sort(diccionariOrdenado);
ArrayList<Map<String,Integer>> mapDeMaps = new ArrayList<>();
/*
*For each word in a document and for each word in the dictionary
* we create the linkedList to represent the posting lists
*/
//
for(int archivo = 0; archivo < palabrasArchivos.size(); archivo++){
LinkedList<Integer> post = new LinkedList<Integer>();
String[] conjuntoPalabras = palabrasArchivos.get(archivo);
String palabra = "";
Map<String, Integer> dictionary = new HashMap<String, Integer>();
for(int p = 0; p < conjuntoPalabras.length;p++){
palabra = conjuntoPalabras[p];
if(diccionario.contains(palabra)){
dictionary.put(palabra, archivo+1);
}
}
mapDeMaps.add(dictionary);
}
for(Object w: diccionariOrdenado){ //Cada palabra en el diccionariOrdenado
String word = (String) w;
LinkedList<Integer> post = new LinkedList<Integer>();
for(int i = 0; i < mapDeMaps.size(); i++){ //Cada diccionario (indica palabra = indice)
Map<String, Integer> cadaDictionary = mapDeMaps.get(i);
String[] palabras = cadaDictionary.keySet().toArray(new String[cadaDictionary.size()]);
Integer[] numDocs = cadaDictionary.values().toArray(new Integer[cadaDictionary.size()]);
for(int j = 0; j < palabras.length; j++){
if(word.equals(palabras[j])){
post.add(numDocs[i]);
}
}
}
invertedIndex.put(word, post);
}
return invertedIndex;
}
/*
* Metodos para buscar, intersectar y booleanos
*/
public static LinkedList<Integer> buscarPalabra(String palabra, Map<String, LinkedList<Integer>> invertedIndex) {
LinkedList<Integer> posts = new LinkedList<>();
Iterator word = invertedIndex.keySet().iterator();
while (word.hasNext()){
String p = (String)word.next();
if(p.equals(palabra)){
posts = invertedIndex.get(p);
}
}
return posts;
}
public static LinkedList<Integer> intersect(LinkedList<Integer> p1, LinkedList<Integer> p2){
/*
* IMPLEMENTATION OF THE NEXT ALGORITHM:
*
* INTERSECT(p1, p2)
* answer←⟨⟩
* while p1 ̸= NIL and p2 ̸= NIL
* do if docID(p1) = docID(p2)
* then ADD(answer, docID(p1))
* p1 ← next(p1)
* p2 ← next(p2)
* else if docID(p1) < docID(p2)
* then p1 ← next(p1)
* else p2 ← next(p2)
* return answer
*/
LinkedList<Integer> answer = new LinkedList<>();
LinkedList<Integer> pos1 = (LinkedList<Integer>) p1.clone();
LinkedList<Integer> pos2 = (LinkedList<Integer>) p2.clone();
while (!pos1.isEmpty() && !pos2.isEmpty()){
if(pos1.getFirst() == pos2.getFirst()){
answer.add(pos1.getFirst());
pos1.removeFirst();
pos2.removeFirst();
}else{
if (pos1.getFirst() < pos2.getFirst()){
pos1.removeFirst();
}else{
pos2.removeFirst();
}
}
}
print("Las palabras buscadas están en el archivo: " + answer.toString());
return answer;
}
public static List<Integer> not(LinkedList<Integer> p1, LinkedList<Integer> p2){
LinkedList<Integer> answer = (LinkedList<Integer>) p1.clone();
LinkedList<Integer> remove = (LinkedList<Integer>) p2.clone();
for (Integer i: remove) {
if(answer.contains(i)){
answer.remove(i);
}
}
return answer;
}
public static LinkedList<Integer> or(LinkedList<Integer> p1, LinkedList<Integer> p2){
LinkedList<Integer> answer = (LinkedList<Integer>) p1.clone();
LinkedList<Integer> remove = (LinkedList<Integer>) p2.clone();
for (Integer element: remove) {
if(!answer.contains(element))
answer.add(element);
}
Collections.sort(answer);
return answer;
}
/*
* Metodo para hacer la interfaz de usuario en consola
*/
public static void menu(Map<String, LinkedList<Integer>> invertedIndex){
boolean i = true;
/*
* word1 AND word2 AND NOT word3
* word1 AND word2 OR word3
* word1 OR word2 AND NOT word3
*/
try {
while (i) {
Scanner scanner = new Scanner(System.in);
print("________________________________________________");
print("Type the number of one of the options below:\n");
print("0) Print the inverted index.");
print("1) word1 AND word2 AND NOT word3");
print("2) word1 AND word2 OR word3");
print("3) word1 OR word2 AND NOT word3");
print("4) word1 AND word2");
print("5) word1 OR word2");
int option = scanner.nextInt();
switch (option) {
case 0:
imprimirIndex(invertedIndex);
print("");
break;
case 1:
print("word1 AND word2 AND NOT word3");
print(" word1: ");
String word1 = new Scanner(System.in).nextLine();
print(" word2: ");
String word2 = new Scanner(System.in).nextLine();
print(" word3: ");
String word3 = new Scanner(System.in).nextLine();
LinkedList<Integer> r1 = intersect(buscarPalabra(word1, invertedIndex), buscarPalabra(word2, invertedIndex));
print(word1 + " --> " + buscarPalabra(word1,invertedIndex).toString());
print(word2 + " --> " + buscarPalabra(word2,invertedIndex).toString());
print(word3 + " --> " + buscarPalabra(word3,invertedIndex).toString());
print("\nRESULT FOR THE QUERY:\n" + word1 + " AND " + word2 + " AND NOT " + word3 + " --> " + not(r1, buscarPalabra(word3, invertedIndex)).toString());
break;
case 2:
print("word1 AND word2 OR word3");
print(" word1: ");
String word21 = new Scanner(System.in).nextLine();
print(" word2: ");
String word22 = new Scanner(System.in).nextLine();
print(" word3: ");
String word23 = new Scanner(System.in).nextLine();
print(word21 + " --> " + buscarPalabra(word21,invertedIndex).toString());
print(word22 + " --> " + buscarPalabra(word22,invertedIndex).toString());
print(word23 + " --> " + buscarPalabra(word23,invertedIndex).toString());
LinkedList<Integer> and = intersect(buscarPalabra(word21, invertedIndex), buscarPalabra(word22, invertedIndex));
print("\nRESULT FOR THE QUERY:\n" + word21 + " AND " + word22 + " OR " + word23 + " --> " + or(and, buscarPalabra(word23, invertedIndex)).toString());
break;
case 3:
print("word1 OR word2 AND NOT word3");
print(" word1: ");
String word31 = new Scanner(System.in).nextLine();
print(" word2: ");
String word32 = new Scanner(System.in).nextLine();
print(" word3: ");
String word33 = new Scanner(System.in).nextLine();
LinkedList<Integer> or = or(buscarPalabra(word31,invertedIndex),buscarPalabra(word32,invertedIndex));
print(word31 + " --> " + buscarPalabra(word31,invertedIndex).toString());
print(word32 + " --> " + buscarPalabra(word32,invertedIndex).toString());
print(word33 + " --> " + buscarPalabra(word33,invertedIndex).toString());
print("\nRESULT FOR THE QUERY:\n" + word31 + " OR " + word32 + " AND NOT " + word33 + " --> " + not(or, buscarPalabra(word33, invertedIndex)).toString());
break;
case 4:
print("word1 AND word2");
print(" word1: ");
String word41 = new Scanner(System.in).nextLine();
print(" word2: ");
String word42 = new Scanner(System.in).nextLine();
print(word41 + " --> " + buscarPalabra(word41,invertedIndex).toString());
print(word42 + " --> " + buscarPalabra(word42,invertedIndex).toString());
print("\nRESULT FOR THE QUERY:\n" + word41 + " AND " + word42 + " --> " + intersect(buscarPalabra(word41, invertedIndex), buscarPalabra(word42, invertedIndex)).toString());
break;
case 5:
print("word1 OR word2");
print(" word1: ");
String word51 = new Scanner(System.in).nextLine();
print(" word2: ");
String word52 = new Scanner(System.in).nextLine();
print(word51 + " --> " + buscarPalabra(word51,invertedIndex).toString());
print(word52 + " --> " + buscarPalabra(word52,invertedIndex).toString());
print("\nRESULT FOR THE QUERY:\n" + word51 + " OR " + word52 + " --> " + or(buscarPalabra(word51, invertedIndex), buscarPalabra(word52, invertedIndex)).toString());
break;
default:
print("Only numbers bewteen 1 and 4");
}
}
}catch (InputMismatchException e){
System.err.println("Only numbers with the option acepted");
}
}
/*
* Main y metodos para impresion
*/
public static void main(String[] args) {
Map<String, LinkedList<Integer>> invertedIndex;
////////////
/*
*To chance the documents for the desired ones only chance the names of the files[] array
*
*/
String files[] = {"1", "2", "3"};
//String files[] = {"texto1", "texto2", "texto3"};
invertedIndex = index(files);
menu(invertedIndex);
}
private static void imprimirIndex(Map<String, LinkedList<Integer>> invertedIndex){
print("********************************************");
print("The inverted index for the documents are:\n");
print("DICTIONARY POSTINGS" );
for (String word: invertedIndex.keySet()) {
print(String.format("%-15s", word) + " --> " + invertedIndex.get(word).toString());
}
print("********************************************");
}
private static void print(String string){
System.out.println(string);
}
}