-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndexer.java
141 lines (136 loc) · 5.74 KB
/
Indexer.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
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class Indexer
{
public Indexer()
{
}
public ArrayList<ArrayList<String>> fileToTokens(String dirPath, String stopWordsPath) throws Exception
{
Tokenizer tokenizer = new Tokenizer();
FileScanner fileScan = new FileScanner();
Scanner scan = new Scanner(System.in);
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
Hashtable<String, Integer> stopWords = tokenizer.stopWords(stopWordsPath);
String[] files = fileScan.readDir(dirPath);
System.out.println("There are " + files.length + " files in directory " + dirPath + ".");
for (int index = 0; index < files.length; index++)
{
long startTime=System.currentTimeMillis();
System.out.println("Tokenizing " + dirPath + "\\" + files[index] + "...");
String file = fileScan.readFile(dirPath + "\\" + files[index]);
ArrayList<String> words = tokenizer.fileToWords(file);
ArrayList<String> tokens = new ArrayList<String>();
for (int index2 = 0; index2 < words.size(); index2++)
{
if(stopWords.containsKey(words.get(index2)))
continue;
else
{
Stemmer stem = new Stemmer();
stem.add(words.get(index2).toCharArray(), words.get(index2).length());
stem.stem();
tokens.add(stem.toString());
}
}
long endTime=System.currentTimeMillis();
double excTime = (double)(endTime - startTime) / 1000;
System.out.println("Completed in " + excTime + "s");
result.add(tokens);
}
return result;
}
public String[] queryToStemmed(ArrayList<String> keywordArray)
{
String[] keywordList = new String[keywordArray.size()];
for (int index = 0; index < keywordArray.size(); index++)
{
Stemmer stem = new Stemmer();
String keyword = keywordArray.get(index);
stem.add(keyword.toCharArray(), keyword.length());
stem.stem();
keywordList[index] = stem.toString();
}
return keywordList;
}
public ArrayList<String> indexing(ArrayList<ArrayList<String>> result, String dirPath, String targetPath)
{
FileScanner fileScan = new FileScanner();
String[] fileList = fileScan.readDir(dirPath);
Hashtable<String, Record> indexTable = new Hashtable<String, Record>();
System.out.println("Indexing terms.");
for (int index = 0; index < result.size(); index ++)
{
ArrayList<String> fileTokens = result.get(index);
for (int index2 = 0; index2 < fileTokens.size(); index2 ++)
{
String token = fileTokens.get(index2);
Record record = new Record();
if (!indexTable.containsKey(token))
{
record.setTerm(token);
record.setDf(record.getDf() + 1);
record.addDocIdTf(fileList[index], 1);
}
else if (indexTable.containsKey(token))
{
record = indexTable.get(token);
if (!record.getDocIdTfList().containsKey(fileList[index]))
{
record.setDf(record.getDf() + 1);
record.addDocIdTf(fileList[index], 1);
}
else if (record.getDocIdTfList().containsKey(fileList[index]))
{
record.addTfByOne(fileList[index]);
}
}
indexTable.put(token, record);
}
}
System.out.println("There are " + indexTable.size() + " terms.");
return indexToString(indexTable, fileList.length, targetPath);
}
public ArrayList<String> indexToString(Hashtable<String, Record> indexTable, int numberOfFiles, String targetPath)
{
Enumeration<String> tokens;
ArrayList<String> hashStrings = new ArrayList<String>();
tokens = indexTable.keys();
while (tokens.hasMoreElements())
{
String token = tokens.nextElement();
String hashString = token + ",";
Record record = indexTable.get(token);
Enumeration<String> docIdTfs = record.getDocIdTfList().keys();
while (docIdTfs.hasMoreElements())
{
String docId = docIdTfs.nextElement().toString();
int tf = record.getDocIdTfList().get(docId).intValue();
hashString = hashString + docId.replaceAll(",", "") + "," + tf + ",";
}
hashString = hashString.substring(0, hashString.length() - 1);
double idf = record.caculateIdf(numberOfFiles);
DecimalFormat decimalFormat=new DecimalFormat();
decimalFormat.applyPattern("#0.000");
String idfString = decimalFormat.format(idf);
hashString = hashString + "," + idfString;
hashStrings.add(hashString);
}
try
{
PrintWriter outputFile = new PrintWriter(targetPath + "\\index.txt");
//outputFile.println("test");
for (int index = 0; index < hashStrings.size(); index++)
{
outputFile.println(hashStrings.get(index));
}
outputFile.close();
}
catch(IOException exception)
{
System.out.println("I/O error happend when write file");
}
return hashStrings;
}
}