-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchEng.java
271 lines (250 loc) · 8.24 KB
/
SearchEng.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
import java.io.*;
import java.lang.Math;
import java.util.*;
import org.tartarus.snowball.ext.PorterStemmer;
/**
* Write a description of class SearchEng here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SearchEng
{
// instance variables - replace the example below with your own
private static Vector[] vsmModel;
private static double[] queryVector;
/**
* Constructor for objects of class SearchEng
*/
public SearchEng()
{
// initialise instance variables
}
private static void keyList()
{
// StringBuilder keyWord = new StringBuilder();
// // String filename = ("keywords.txt");
// // try
// // {
// // FileReader inputFile = new FileReader(filename);
// // try
// // {
// // Scanner parser = new Scanner(inputFile);
// // do{
// // //regular expression(?i) ignore the case and \\b set the word boundary
// // keyWord.append(parser.nextLine()+"\n");
// // }while (parser.hasNextLine());
// // }
// // finally
// // {
// // System.out.println("Key word list has been loaded!");
// // inputFile.close();
// // }
// // }
// // catch(FileNotFoundException exception)
// // {
// // System.out.println(filename + " not found");
// // }
// // catch(IOException exception)
// // {
// // System.out.println("Unexpected I/O exception occurs");
// // }
// return keyWord.toString();
}
private static String readIndex(String indexDir)
{
StringBuilder indexList = new StringBuilder();
String filename = (".\\" + indexDir + "\\myindex.txt");
try
{
FileReader inputFile = new FileReader(filename);
try
{
Scanner parser = new Scanner(inputFile);
do{
//regular expression(?i) ignore the case and \\b set the word boundary
indexList.append(parser.nextLine()+"\n");
}while (parser.hasNextLine());
}
finally
{
System.out.println("Key word list has been loaded!");
inputFile.close();
}
}
catch(FileNotFoundException exception)
{
System.out.println(filename + " not found");
}
catch(IOException exception)
{
System.out.println("Unexpected I/O exception occurs");
}
return indexList.toString();
}
private static String stemmer(String fullDoc)
{
StringBuilder docsForSte = new StringBuilder();
PorterStemmer stemmer = new PorterStemmer();
//PorterStemmer calss is from package org.tartarus.snowball.ext
for(String line : fullDoc.split("\n"))
{
stemmer.setCurrent(line);
stemmer.stem();
docsForSte.append(stemmer.getCurrent().trim());
docsForSte.append("\n");
}
fullDoc = docsForSte.toString();
System.out.println(fullDoc);
return fullDoc;
}
private static double calWeight(int tf,double idf)
{
return tf * idf;
}
private static void createVector(String keyword, String index)
{
int keyCount = keyword.split("\n").length;
queryVector = new double[keyCount];
docList(keyCount);
for(String key : keyword.split("\n"))
{
for(String indexLine : index.split("\n"))
{
if(indexLine.toLowerCase().contains(key.toLowerCase()))
{
String[] elements = indexLine.split(",");
String term = elements[0];
int docId = 0;
int tf = 0;
double idf = 0.0;
double weight = 0.0;
for(int i = 1;i < elements.length;i+=2)
{
if(i != (elements.length - 1))
{
Vector myVector = new Vector();
docId = toInteger(elements[i]);
tf = toInteger(elements[i+1]);
idf = Double.parseDouble(elements[elements.length-1]);
weight = calWeight(tf,idf);
vsmModel[docId-1].setModel(Arrays.asList(keyword.split("\n")).indexOf(key), weight);
queryVector[Arrays.asList(keyword.split("\n")).indexOf(key)] += weight;
}
}
}
}
//ArrayList<String> items = Arrays.asList(str.split(","));
}
}
public static void getConsine()
{
double molecule = 0;
double deno1 = 0;
double deno2 = 0;
double cosine = 0;
for(int i = 0;i < vsmModel.length;i++)
{
for(int j = 0;j < queryVector.length;j++)
{
molecule += vsmModel[i].getElement(j) * queryVector[j];
deno1 += Math.pow(vsmModel[i].getElement(j),2);
deno2 += Math.pow(queryVector[j],2);
}
double deno = Math.sqrt(deno1) * Math.sqrt(deno2);
if(deno != 0)
cosine = molecule / deno;
else
cosine = 0;
vsmModel[i].setScore(cosine);
}
}
public static void sortScore()
{
Vector temp = new Vector();
//use bubblesort here
; for (int i = 0;i < vsmModel.length;i++)
{
for (int j = 0;j < vsmModel.length- 1 - i;j++)
{
if (vsmModel[j].getScore() <= vsmModel[j+1].getScore())
{
temp = vsmModel[j];
vsmModel[j] = vsmModel[j+1];
vsmModel[j+1] = temp;
}
}
}
}
private static void docList(int keyCount)
{
int docNum = 0;
int docCount = 0;
File file = new File(".\\collection\\");
//File[] files = file.listFiles();
for(File f : file.listFiles())
{
System.out.println(f.getName());
//vsmModel[docNum].setToken("#" + docNum +" " + f.getName());
docNum++;
}
vsmModel = new Vector[docNum];
//StringBuilder vectorLine = new StringBuilder();
//int[] weightVec = new int[keyNo];
for(int i = 0;i < docNum;i++)
{
double[] initialArray = new double[keyCount];
vsmModel[i] = new Vector(initialArray);
}
for(File f : file.listFiles())
{
System.out.println(f.getName());
vsmModel[docCount].setToken(f.getName());
docCount++;
}
//return docNum;
//System.out.println(listOfDocs);
}
public static void displayQuery()
{
for(int i = 0;i < queryVector.length;i++)
System.out.println(queryVector[i]);
}
public static void displayVector()
{
for(int i = 0;i < vsmModel.length;i++)
System.out.println(Arrays.toString(vsmModel[i].getVectorModel()));
}
public static void disScore()
{
for(int i = 0;i < vsmModel.length;i++)
System.out.println(vsmModel[i].getScore());
}
public static void disName(int topNum)
{
for(int i = 0;i < topNum;i++)
System.out.println(vsmModel[i].getToken());
}
public static int toInteger(String line)
{
return Integer.parseInt(line.replaceAll("d",""));
}
public static void main(String args[])
{
StringBuilder keyword = new StringBuilder();
String index = readIndex(args[0]);
int topNum = Integer.valueOf(args[1]);
//String keyword = stemmer(keyList());
for(int i = 2;i < args.length;i++)
{
keyword.append(args[i]+"\n");
}
createVector(keyword.toString(), index);
getConsine();
displayVector();//
//disName(topNum);//
//disScore();//
sortScore();
disName(topNum);//
}
}