-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.java
345 lines (323 loc) · 11.4 KB
/
Index.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import org.tartarus.snowball.ext.PorterStemmer;
import java.lang.Math;
/**
* This is the text-processing module of the search engine.
*
* @author Kaihan Xie
* @version v2.0
*/
public class Index
{
private static ArrayList<String> listOfDocs = new ArrayList<String>();
private static ArrayList<String> docContainer = new ArrayList<String>();
private String document;
/**
* Constructor for objects of class Index
*/
public Index()
{
// initialise instance variables
//docNo = 0;
//document = "";
}
public static String readFile(String fileName)
{
StringBuilder fullDoc = new StringBuilder();
String filename = (".\\collection\\" + fileName);
try
{
FileReader inputFile = new FileReader(filename);
try
{
Scanner parser = new Scanner(inputFile);
do{
fullDoc.append(parser.nextLine());
fullDoc.append("\n");
}while (parser.hasNextLine());
}
finally
{
//System.out.println(fullDoc);
System.out.println("The data has been loaded to the system!");
inputFile.
close();
}
}
catch(FileNotFoundException exception)
{
System.out.println(filename + " not found");
}
catch(IOException exception)
{
System.out.println("Unexpected I/O exception occurs");
}
String docs = fullDoc.toString();
return docs;
}
private static String delimeter(String docs)
{
StringBuilder fullDoc = new StringBuilder();
String regex =
"((?<=\\w)s?'(?:s\\b)?)"//1 Tokenize apostrophes as 's and s'
+ "|'([^']*)'"//2 Tokenize single quote
+ "|(\"([^\"]*)\")"//3 Tokenize double quote
+ "|(\\b\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}\\b)"//4 Tokenize IP address
+ "|(\\b[\\w.%-]+@[-.\\w]+\\.[A-Za-z]{2,4}\\b)"//5 Tokenize E-mail address
+ "|(\\b\\w*-\\w*\\b)"//7 Tokenize hyphenated words
+ "|(\\bhttps?://[A-Za-z]{2,4}.\\w+\\.[A-Za-z]{2,4}\\b)"//8 Tokenize URL
+ "|(\\b\\S.\\S.\\S\\b)"//9 Tokenize Acronym
+ "|((?:[A-Z]\\w*\\s*)+)" //10 Tokenize book title
+ "|(\\w+)";
int count = 0;
Matcher pat = Pattern.compile(regex).matcher(docs);
while (pat.find()) {
if (pat.group(1) != null) {
fullDoc.append(pat.group(1).trim());
fullDoc.append("\n");
System.out.println("#1 "+pat.group(1).trim());
}
else if (pat.group(2) != null)
{
fullDoc.append(pat.group(2).trim());
fullDoc.append("\n");
System.out.println("#2 "+pat.group(2).replace("\'","").trim());
}
else if (pat.group(3) != null)
{
fullDoc.append(pat.group(3).trim());
fullDoc.append("\n");
System.out.println("#3 "+pat.group(3).replace("\"","").trim());
}
else if (pat.group(4) != null)
{
fullDoc.append(pat.group(4).trim());
fullDoc.append("\n");
System.out.println("#4 "+pat.group(4).trim());
}
else if (pat.group(5) != null)
{
fullDoc.append(pat.group(5).replaceAll("-","").trim());
fullDoc.append("\n");
System.out.println("#5 "+pat.group(5).trim());
}
else if (pat.group(6) != null)
{
fullDoc.append(pat.group(6).trim());
fullDoc.append("\n");
System.out.println("#6 "+pat.group(6).trim());
}
else if (pat.group(7) != null)
{
fullDoc.append(pat.group(7).trim());
fullDoc.append("\n");
System.out.println("#7 "+pat.group(7).replaceAll("-","").trim());
}
else if (pat.group(8) != null)
{
fullDoc.append(pat.group(8).trim());
fullDoc.append("\n");
System.out.println("#8 "+pat.group(8).trim());
}
else if (pat.group(9) != null)
{
fullDoc.append(pat.group(9).trim());
fullDoc.append("\n");
System.out.println("#9 "+pat.group(9).trim());
}
else if (pat.group(10) != null)
{
fullDoc.append(pat.group(10).trim());
fullDoc.append("\n");
System.out.println("#10 "+pat.group(10).trim());
}
else if (pat.group(11) != null)
{
fullDoc.append(pat.group(11).replaceAll("\\d+","").trim());
fullDoc.append("\n");
System.out.println("#11 "+pat.group(11).replaceAll("\\d","").trim());
}
else if (pat.group(12) != null)
{
fullDoc.append(pat.group(12).replaceAll("[_.:\',;:!?]","").trim());
fullDoc.append("\n");
System.out.println("#11 "+pat.group(12).replaceAll("\\d","").trim());
}
}
writeToFile(fullDoc.toString(),"tokenList");
return fullDoc.toString();
}
private static String stopList(String fullDoc, String filename)
{
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
String stopWord = "(?i)\\b" + parser.nextLine() + "\\b\n";
fullDoc = fullDoc.replaceAll(stopWord,"");
}while (parser.hasNextLine());
}
finally
{
System.out.println("Words in the stop word list has been replaced!");
inputFile.close();
}
}
catch(FileNotFoundException exception)
{
System.out.println(filename + " not found");
}
catch(IOException exception)
{
System.out.println("Unexpected I/O exception occurs");
}
//fullDoc = fullDoc.replaceAll("\n+", "\n");
//System.out.println(fullDoc);
System.out.println("This is the end");
return fullDoc;
}
private static String SpaceRm(String docs)
{
docs = docs.replaceAll("\\s+"," ");
//remove all extra wthitespaces
docs = docs.replace('\ufeff',' ').trim();
//as the Filereader above reads a UTF encoded text with a UTF-16 BOM, a zero width no-break space(0xFEFF)
//shows up in the front of string, so it need to be replaced with the normal whitespace.
return docs;
}
private static String DuplicateRm(String docs)
{
String[] tokens = docs.split("\n");
StringBuilder forDup = new StringBuilder();
tokens = new HashSet<String>(Arrays.asList(tokens)).toArray(new String[0]);
for(String token : tokens) {
forDup.append(token + "\n");
}
String result = forDup.toString();
return result;
}
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().replaceAll("[->_<(),\";*|=+#!?]","").trim());
docsForSte.append("\n");
}
fullDoc = docsForSte.toString().replaceAll("\n+", "\n");;
//System.out.println(fullDoc);
return fullDoc;
}
private static int countTf(String word, String fullDoc)
{
int tf = 0;
for(String line : fullDoc.split("\n"))
{
if(word.equals(line))
{
tf+=1;
}
}
return tf;
}
public static int docList(String indexDir)
{
int docNum = 0;
File file = new File(".\\"+indexDir+"\\");
for(File f : file.listFiles())
{
System.out.println(f.getName());
listOfDocs.add(f.getName());
docNum++;
}
System.out.println(listOfDocs);
return docNum;
}
private static String getIdf(String line,int docNum)
{
double df = 0;
int i = 0;
for(i=0;i < line.length();i++)
{
if(line.charAt(i) == 'd')
{
df+=1;
}
}
return String.format("%.6f",Math.log(docNum/(df)));
}
private static void textProcess(String colDir, String indexDir, String stopWord)
{
StringBuilder tempDic = new StringBuilder();
StringBuilder completeDic = new StringBuilder();
int docNum = docList(colDir);
for(int i = 0;i < listOfDocs.size();i++)
{
String fullDoc = stemmer(stopList(delimeter(SpaceRm(readFile(listOfDocs.get(i)))), stopWord));
docContainer.add(fullDoc);
tempDic.append(fullDoc);
}
String dic = DuplicateRm(tempDic.toString());
//String dic = tempDic.toString().replaceAll("(?m)^\\s", "");
System.out.println(dic);
//String fullDoc = stemmer(stopList(delimeter(SpaceRm(readFile()))));
// StringBuilder docs = new StringBuilder();
int num = 0;
for(String line : dic.split("\n"))
{
int tf = 0;
int idf = 0;
int docId = 0;
StringBuilder tfCom = new StringBuilder();
completeDic.append(line + ",");
System.out.println("Process Complete "+num);num++;
for(docId = 0;docId < docContainer.size();docId++)
{
String curDoc = docContainer.get(docId);
innerLop: for(String line2 : curDoc.split("\n"))
{
if(line.equals(line2))
{
tf = countTf(line2,curDoc);
tfCom.append("d"+ listOfDocs.get(docId).replaceAll(".txt","") +","+tf+",");
break innerLop;
}
}
}
completeDic.append(tfCom.toString()+getIdf(tfCom.toString(),docNum)+"\n");
}
//System.out.println(completeDic.toString());
writeToFile(completeDic.toString(), indexDir);
System.out.println("Process Completed");
}
private static void writeToFile(String result, String indexDir)
{
try
{
PrintWriter outputFile = new PrintWriter(".\\" + indexDir + "\\myindex.txt");
System.out.println("Writing index to the file..");
for (String line : result.split("\n"))
outputFile.println(line);
outputFile.close();
}
catch(IOException e)
{
System.out.println("Error! Can't write to the file!");
}
}
public static void main(String args[])
{
textProcess(args[0], args[1], args[2]);
}
}