-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchEngine.java
225 lines (212 loc) · 7.54 KB
/
SearchEngine.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
import java.io.*;
import java.util.*;
public class SearchEngine
{
InvertedPageIndex invPageIndex;
public SearchEngine()
{
invPageIndex = new InvertedPageIndex();
}
public String printArrList(ArrayList<SearchResult> a)
{
if (a.size()==0) {return("No webpage contains the given phrase");}
else
{
String out = a.get(0).p.name/*+" "+a.get(0).relevance*/;
for(int i=1; i<a.size(); i++)
{
out = out+", "+a.get(i).p.name/*+" "+a.get(i).relevance*/;
}
return out;
}
}
public String printSet(MySet<PageEntry> m)
{
if(m!=null)
{
Node<PageEntry> penode = m.linklist.head;
if(penode==null) {return("No webpage contains the given phrase");}
String s = penode.getElement().name;
penode = penode.getNext();
while(penode!=null)
{
s = s + ", " + penode.getElement().name;
penode = penode.getNext();
}
return s;
}
else return("m is null!");
}
public String getx(String s)
{
String temps = s.substring(32);
int spaceid = 0;
while(temps.charAt(spaceid)!=' ') {spaceid++;}
return temps.substring(0,spaceid);
}
public String gety(String s)
{
String temps = s.substring(32);
int spaceid = 0;
while(temps.charAt(spaceid)!=' ') {spaceid++;}
return temps.substring(spaceid+1);
}
public String[] getInputArray(String s)
{
if(s.substring(0,34).equals("queryFindPagesWhichContainAllWords"))
{
return (s.substring(35)).toLowerCase().split(" ");
}
else if(s.substring(0,41).equals("queryFindPagesWhichContainAnyOfTheseWords"))
{
return (s.substring(42)).toLowerCase().split(" ");
}
else
{
return (s.substring(33)).toLowerCase().split(" ");
}
}
public void performAction(String actionMessage)
{
if(actionMessage.length()>6)
{
if(actionMessage.substring(0,7).equals("addPage"))
{
String y = actionMessage.substring(8);
String x = y.toLowerCase();
if(x.equals("stacks")) x=x.substring(0,x.length()-1);
if(x.equals("applications")) x=x.substring(0,x.length()-1);
if(x.equals("structures")) x=x.substring(0,x.length()-1);
PageEntry newpage = new PageEntry(x);
invPageIndex.addPage(newpage);
}
else if(actionMessage.substring(0,30).equals("queryFindPagesWhichContainWord"))
{
String y = actionMessage.substring(31);
String x = y.toLowerCase();
if(x.equals("stacks")) x=x.substring(0,x.length()-1);
if(x.equals("applications")) x=x.substring(0,x.length()-1);
if(x.equals("structures")) x=x.substring(0,x.length()-1);
String answer = "";
MySet<PageEntry> tempo = invPageIndex.getPagesWhichContainWord(x);
if(tempo==null) {System.out.println("No webpage contains word "+y);}
else if(tempo.IsEmpty()) {System.out.println("No webpage contains word "+y);}
// else
// {
// PageEntry page1;
// while(!tempo.IsEmpty())
// {
// page1 = tempo.remove();
// if (page1!=null)
// answer = answer + ", " + page1.name;
// }
// }
// if(answer.length()<=2) System.out.print("");
// else System.out.println(answer.substring(2));
Node<PageEntry> page1node = tempo.linklist.head;
ArrayList<SearchResult> answerList = new ArrayList();
while(page1node!=null)
{
SearchResult tempsr = new SearchResult(page1node.getElement(),page1node.getElement().getTermFrequency(x));
answerList.add(tempsr);
page1node = page1node.getNext();
}
MySort m = new MySort();
ArrayList finall = (m.sortThisList(answerList));
if(finall!=null && finall.size()>0)
{
System.out.println(printArrList(finall));
}
}
else if(actionMessage.substring(0,31).equals("queryFindPositionsOfWordInAPage"))
{
String wordd = getx(actionMessage);
String wordx = wordd.toLowerCase();
String pagey = gety(actionMessage);
if(wordx.equals("stacks")) wordx=wordx.substring(0,wordx.length()-1);
if(wordx.equals("applications")) wordx=wordx.substring(0,wordx.length()-1);
if(wordx.equals("structures")) wordx=wordx.substring(0,wordx.length()-1);
String answer = "";
MySet<PageEntry> temppagelist = invPageIndex.setOfPages.MySetclone();
PageEntry temppage;
PageEntry actualpagey = null;
while(!temppagelist.IsEmpty())
{
temppage = temppagelist.remove();
if(temppage.name.equals(pagey))
actualpagey = temppage;
}
if(actualpagey==null) {System.out.println("No webpage "+pagey+" found");}
int hashid = invPageIndex.hashtable.getHashIndex(wordx);
if(!invPageIndex.hashtable.hm.containsKey(hashid)) {System.out.println("Webpage "+ pagey +" does not contain word "+wordd);}
MyLinkedList<WordEntry> tempwelist = invPageIndex.hashtable.hm.get(hashid);
if(tempwelist==null) {System.out.print("");}
else
{ Node<WordEntry> tempnode = tempwelist.head;
WordEntry tempwe = null;
while(tempnode!=null)
{
if(tempnode.getElement().indexList.head.getElement().page.name.equals(pagey) && tempnode.getElement().word.equals(wordx))
break;
tempnode = tempnode.getNext();
}
if(tempnode==null) {System.out.println("Webpage "+ pagey +" does not contain word "+wordd);}
else{tempwe=tempnode.getElement();}
if(tempwe==null){}
else
{
Node<Position> temppos = tempwe.indexList.head;
while(temppos!=null)
{
if(temppos.getElement().page.name.equals(actualpagey.name))
{
answer = answer + ", " + temppos.getElement().wordIndex;
}
temppos = temppos.getNext();
}
if(answer.length()<=2) {System.out.println("Webpage "+ pagey +" does not contain word "+wordd);}
else System.out.println(answer.substring(2));
}
}
}
else if(actionMessage.substring(0,34).equals("queryFindPagesWhichContainAllWords"))
{
String[] inpArr = getInputArray(actionMessage);
MySet<PageEntry> answer = invPageIndex.getPagesWhichContainWord(inpArr[0]);
for(int i=1; i<inpArr.length; i++)
{
answer = answer.intersection(invPageIndex.getPagesWhichContainWord(inpArr[i]));
}
//System.out.println(printSet(answer));
ArrayList<SearchResult> finalarr = invPageIndex.mysetToArrayList(answer,inpArr,false);
MySort m = new MySort();
System.out.println(printArrList(m.sortThisList(finalarr)));
//return m.sortThisList(finalarr);
}
else if(actionMessage.substring(0,41).equals("queryFindPagesWhichContainAnyOfTheseWords"))
{
String[] inpArr = getInputArray(actionMessage);
MySet<PageEntry> answer = invPageIndex.getPagesWhichContainWord(inpArr[0]);
for(int i=1; i<inpArr.length; i++)
{
answer = answer.union(invPageIndex.getPagesWhichContainWord(inpArr[i]));
}
//System.out.println(printSet(answer));
ArrayList<SearchResult> finalarr = invPageIndex.mysetToArrayList(answer,inpArr,false);
MySort m = new MySort();
System.out.println(printArrList(m.sortThisList(finalarr)));
}
else if(actionMessage.substring(0,32).equals("queryFindPagesWhichContainPhrase"))
{
String[] inpArr = getInputArray(actionMessage);
MySet<PageEntry> answer = invPageIndex.getPagesWhichContainPhrase(inpArr);
ArrayList<SearchResult> finalarr = invPageIndex.mysetToArrayList(answer,inpArr,true);
MySort m = new MySort();
System.out.println(printArrList(m.sortThisList(finalarr)));
//System.out.println(printSet(answer));
//System.out.println(printSet(invPageIndex.setOfPages));
}
}
else System.out.println("No message matched with the " + actionMessage);
}
}