-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
143 lines (118 loc) · 5.27 KB
/
Search.java
File metadata and controls
143 lines (118 loc) · 5.27 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
package org.example.search.SimpleSearchEngine;
import org.example.search.SearchEngineInterface.AllSearchStrategy;
import org.example.search.SearchEngineInterface.AnySearchStrategy;
import org.example.search.SearchEngineInterface.NoneSearchStrategy;
import org.example.search.SearchEngineInterface.SimpleSearchEngine;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Search {
private SimpleSearchEngine searchStrategy;
// Set the search strategy at runtime
public void setSearchStrategy(SimpleSearchEngine searchStrategy) {
this.searchStrategy = searchStrategy;
}
public void getInput(String fileName) {
// Use a Scanner with UTF-8 encoding
Scanner sc = new Scanner(System.in, StandardCharsets.UTF_8);
ArrayList<String> nameWithMail = readFile(fileName);
if (nameWithMail.isEmpty()) {
System.out.println("No data found in the file.");
return;
}
Map<String, Set<Integer>> invertedIndex = buildInvertedIndex(nameWithMail);
while (true) {
System.out.println("\n=== MENU ===");
System.out.println("1. Find a person");
System.out.println("2. Print all people");
System.out.println("0. Exit");
int userChoice = sc.nextInt();
sc.nextLine(); // Consume the newline character
switch (userChoice) {
case 1:
System.out.println("\nSelect a matching strategy: ALL, ANY, NONE");
System.out.print("> ");
String strategy = sc.nextLine().toUpperCase().trim();
// Dynamically set the search strategy based on user input
switch (strategy) {
case "ALL":
setSearchStrategy(new AllSearchStrategy());
break;
case "ANY":
setSearchStrategy(new AnySearchStrategy());
break;
case "NONE":
setSearchStrategy(new NoneSearchStrategy());
break;
default:
System.out.println("Invalid strategy! Defaulting to ANY.");
setSearchStrategy(new AnySearchStrategy());
break;
}
System.out.println("\nEnter a name or email to search all suitable people.");
System.out.print("> ");
String query = sc.nextLine().toLowerCase().trim();
ArrayList<String> foundPeople = searchFoundPeople(query, invertedIndex, nameWithMail);
displayFoundPeople(foundPeople);
break;
case 2:
System.out.println("\n=== List of people ===");
displayFoundPeople(nameWithMail);
break;
case 0:
System.out.print("\nBye!");
return;
default:
System.out.println("\nIncorrect option! Try again.");
break;
}
}
}
private ArrayList<String> readFile(String fileName) {
ArrayList<String> nameWithMail = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
nameWithMail.add(line.trim());
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
return nameWithMail;
}
private Map<String, Set<Integer>> buildInvertedIndex(ArrayList<String> nameWithMail) {
Map<String, Set<Integer>> invertedIndex = new HashMap<>();
for (int index = 0; index < nameWithMail.size(); index++) {
String line = nameWithMail.get(index);
String[] words = line.toLowerCase().split(" ");
for (String word : words) {
invertedIndex.putIfAbsent(word, new HashSet<>());
invertedIndex.get(word).add(index);
}
}
return invertedIndex;
}
public ArrayList<String> searchFoundPeople(String query, Map<String, Set<Integer>> invertedIndex, ArrayList<String> nameWithMail) {
String[] queryWords = query.split(" ");
// Use the current strategy to perform the search
Set<Integer> resultIndexes = searchStrategy.search(queryWords, invertedIndex);
ArrayList<String> foundPeople = new ArrayList<>();
for (int index : resultIndexes) {
foundPeople.add(nameWithMail.get(index));
}
return foundPeople;
}
public void displayFoundPeople(ArrayList<String> foundPeople) {
if (foundPeople.isEmpty()) {
System.out.println("No matching people found.");
} else {
System.out.println(foundPeople.size() + " persons found:");
for (String person : foundPeople) {
System.out.println(person);
}
}
}
}