-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPass2.java
More file actions
209 lines (149 loc) · 7.02 KB
/
Pass2.java
File metadata and controls
209 lines (149 loc) · 7.02 KB
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
import java.io.*;
import java.util.*;
public class Pass2 {
// Key=term=token, file =document; term frequency = frequency of a token in that file; nums_docs = number of files the word appeared
//Default constructor for pass2
Pass2(HashTable globalhash, String output) throws Exception{
// define, declare writer to map filenames to indices
PrintWriter writer1 = new PrintWriter("mapping.txt");
// output directory of pass1 is input directory of pass2
File inputFolder = new File(output);
// store list of files of output directory in file array
File[] filesList = inputFolder.listFiles();
int n = filesList.length;
//Generating mapping file, just map indices to file array contents in that order
for(int i=0; i<filesList.length; i++){
writer1.println(String.format("%-5.5s",i) +" "+ String.format("%13.13s", filesList[i].toString().substring(7)));
}
//We have about 1000 files, so we need 1000 file pointers(Buffered readers)
//Buffered reader array to store buffered readers
BufferedReader[] readers = new BufferedReader[filesList.length];
double[] filelen = new double[filesList.length];
//Iterate over file list, create buffered reader pointers and store in buff reader array
for(int i=0; i<readers.length; i++){
BufferedReader br = new BufferedReader(new FileReader(filesList[i]));
readers[i] =br;
}
// Now, we are working with first entry of each file so we have about 1000 files, we need about 1000 strings to hold entries
String[] firstWords = new String[filesList.length];
//Use buffered readers of each file to read first entry of each file.
// buffered reader 0 helps in getting contents of file 0 (Hypothetically)
// Hypothetically because files in file array
for(int i=0; i<readers.length; i++){
try {
//firstline of ith file, get it by using buffreader of ith file
firstWords[i] = readers[i].readLine();
}
catch (Exception e){
System.out.println(e);
}
}
// Initialize writer for post
PrintWriter writer = new PrintWriter("posting.txt");
try{
String tempo="";
// This stores the most recent least lexicographic entry among 1000 first lines we grabbed
String previous = null;
//We Start entering posting records from 0th index
int start = 0;
double idf=0;
boolean nullcheck = true;
//check if any of 1000 lines we grabbed are null
for(int i=0; i<firstWords.length; i++){
if(firstWords[i] != null){
nullcheck = false;
break;
}
}
//Stop indexing if null
while(!nullcheck){
// Stores current least Lexicographic entry
String current ;
//We are trying to find least lexicographic entry among 1000 keys here
String firstAlphabetically = "";
int leastlexico =0;
if(firstWords[0] != null){
//Split the entry to two, entry has two things, term and term frequency
String[] arr = firstWords[0].split(" ");
// term of the entry
firstAlphabetically = arr[0] ;}
else {firstAlphabetically = "zzzzzzzzzzzzzzzzzzzzzzzzzz";}
for(int i =0; i<firstWords.length; i++){
if(firstWords[i] != null) {
String[] arr = firstWords[i].split(" ");
if (arr[0].compareTo(firstAlphabetically) < 0) {
firstAlphabetically = arr[0];
leastlexico = i;
}
}
}
//We found the least entry, An entry has term and term frequency, we need term/key
String[] arr = firstWords[leastlexico].split(" ");
double total_tokens= filelen[leastlexico];
//Got the key, least lexicograhic key
current = arr[0];
if(!current.equals(previous)){
previous = current;
//update global hashtable record, append start address to it
String value = globalhash.getName(current) + " "+start;
//updated
idf = (double) (1+(Math.log((1+n)/(1+Double.parseDouble(globalhash.getName(current))))));
tempo = String.format("%.8f",idf);
globalhash.insert(current, value);
//move to next index
start++;
}
else {
start++;
}
//Keep writing least lexicographic elements in posting file
//Entry will be doc_id, term frequency. Doc_id to Doc_name see mapping.txt
double tfidf = Double.parseDouble(arr[1])*Double.parseDouble(tempo);
String temp = String.format("%.8f",tfidf);
writer.println(String.format("%-5.5s",leastlexico) + String.format("%7.7s",temp) );
// If there is a hit at ith file, the next entry in that file is read to compare with other 1000 entries
String next = readers[leastlexico].readLine();
if(next == null){
firstWords[leastlexico] = null;
}
else {
//Reading next entry from the same file we got a hit on least lexico previously
firstWords[leastlexico] = new String(next);
}
nullcheck=true;
//Checking for null entries, loops stops if there is null
for(int p=0; p<firstWords.length; p++){
if(firstWords[p] != null){
nullcheck = false;
break;
}
}
}
}
finally {
//print global hashtable which is nothing but dict file
//Hashtable class takes care of creating files and righting bla bla...
globalhash.print("dict.txt");
//Flush file writers to dump content onto main memory completely and close
if(writer != null || writer1!=null){
writer.flush();
writer.close();
writer1.flush();
writer1.close();
//Test case
// Test string
// String s = "algorithm";
//
// Find index of test string in global hashtable
// int temp = globalhash.find(s);
// Got the index, now print it
// System.out.println("HashFunction('algorithm') = "+temp);
// System.out.println();
// Got the entry, print string, number of docs it is repeated in, starting address in post file.
// System.out.println("Dict record ["+temp+"] = "+globalhash.getaddr(temp)+" "+globalhash.getName(s));
//
System.out.println("\n");
}
}
}
}