-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStage6.java
193 lines (181 loc) · 7.16 KB
/
Stage6.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
import helper.Help;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
/**
* this interface is used for defining the common strategy
* It helps in defining the functional interface to override
* for defining explicit functionality.
*/
interface Strategy{
/**
*
* @param toSearch to search word
* @param invertedIndex inverted mapping
* @param listOfPerson list of person details
* @return set of lines where did the query match
*/
Set<String> find(String toSearch,Map<String,Set<Integer>> invertedIndex,ArrayList<String> listOfPerson);
}
/**
* This class gives explicit implementation of strategy.
* strategies: all, any, none
* All - the program should print
* lines containing all the words from the query.
* Any - the program should print the
* lines containing at least one word from the query.
* None - the program should print lines
* that do not contain words from the query at all
*/
class ConcreteImplementation{
/**
*
* @param toSearch word to search
* @param invertedIndex contains mapping from index to document
* @param listOfPerson contains list of person details
* @return returns list of index of documents as per all strategy.
*/
public static Set<String> findAll(String toSearch, Map<String, Set<Integer>> invertedIndex, ArrayList<String> listOfPerson) {
Set<Integer> lineIndex = new HashSet<>();
boolean firstTimeEntering = true;
for (String personDetail : toSearch.split(" ")) {
personDetail = personDetail.toLowerCase();
if (invertedIndex.get(personDetail) == null) continue;
if (firstTimeEntering) {
firstTimeEntering = false;
lineIndex.addAll(invertedIndex.get(personDetail));
}
else {
Set<Integer> temp = new HashSet<>();
for (int index : invertedIndex.get(personDetail)) {
if (lineIndex.contains(index)) {
temp.add(index);
}
}
lineIndex = temp;
}
}
if (lineIndex.isEmpty()) {
return new HashSet<>(List.of("No matching person found."));
}
else {
return lineIndex.stream().map(listOfPerson::get).collect(Collectors.toSet());
}
}
/**
*
* @param toSearch word to search
* @param invertedIndex contains mapping from index to document
* @param listOfPerson contains list of person details
* @return returns list of index of documents as per any strategy.
*/
public static Set<String> findAny(String toSearch, Map<String, Set<Integer>> invertedIndex, ArrayList<String> listOfPerson){
Set<Integer> lineIndex = new HashSet<>();
for (String personDetail : toSearch.split(" ")) {
personDetail = personDetail.toLowerCase();
if(invertedIndex.get(personDetail) == null) continue;
lineIndex.addAll(invertedIndex.get(personDetail));
}
if (lineIndex.isEmpty()) {
return new HashSet<>(List.of("No matching person found."));
}
else {
return lineIndex.stream().map(listOfPerson::get).collect(Collectors.toSet());
}
}
/**
*
* @param toSearch word to search
* @param invertedIndex contains mapping from index to document
* @param listOfPerson contains list of person details
* @return returns list of index of documents as per none strategy.
*
*/
public static Set<String> findNone(String toSearch, Map<String, Set<Integer>> invertedIndex, ArrayList<String> listOfPerson) {
Set<Integer> lineIndex = new HashSet<>();
Set<Integer> temp = new HashSet<>();
for (String personDetail : toSearch.split(" ")) {
personDetail = personDetail.toLowerCase();
if (invertedIndex.get(personDetail) == null) {
continue;
}
temp.addAll(invertedIndex.get(personDetail));
}
for (int i = 0; i < listOfPerson.size(); i++) {
if(!temp.contains(i)){
lineIndex.add(i);
}
}
if (lineIndex.isEmpty()) {
return new HashSet<>(List.of("No matching person found."));
}
else {
return lineIndex.stream().map(listOfPerson::get).collect(Collectors.toSet());
}
}
}
public class Stage6 {
public static String readFile(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path)));
}
public static void print() {
System.out.println("==Menu==");
System.out.println("1. Find a person");
System.out.println("2. Print all people");
System.out.println("0. Exit");
}
public static Map<String,Set<Integer>> createInvertedIndex(ArrayList<String> listOfPerson) {
Map<String,Set<Integer>> invertedIndex = new HashMap<>();
for (int i = 0; i < listOfPerson.size(); i++) {
String[] persons = listOfPerson.get(i).split(" ");
for (String person : persons) {
person = person.toLowerCase();
invertedIndex.putIfAbsent(person, new HashSet<>());
invertedIndex.get(person).add(i);
}
}
return invertedIndex;
}
public static void main(String[] args) throws Exception {
String fileName = args[1];
String txt = readFile(fileName);
ArrayList<String> listOfPerson = new ArrayList<>();
Scanner sc = new Scanner(txt);
while (sc.hasNext()) {
listOfPerson.add(sc.nextLine());
}
Map<String,Set<Integer>> invertedIndex = createInvertedIndex(listOfPerson);
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
print();
int choice = Help.getChoice(scanner);
switch (choice) {
case 0 :
exit = Help.choice0();
break;
case 2 :
Help.choice2(listOfPerson);
break;
case 1 :
System.out.println("Select a matching strategy: ALL, ANY, NONE");
String strategy = scanner.nextLine();
System.out.println("Enter a name or email to search all suitable people.");
String toSearch = scanner.nextLine();
Strategy strategyImplementation = switch (strategy) {
case "ALL" -> ConcreteImplementation::findAll;
case "ANY" -> ConcreteImplementation::findAny;
case "NONE" -> ConcreteImplementation::findNone;
default -> throw new IllegalStateException("Unexpected value: " + strategy);
};
strategyImplementation.find(toSearch, invertedIndex, listOfPerson).forEach(System.out::println);
break;
default :
System.out.println("Incorrect option! Try again.");
}
}
scanner.close();
}
}