-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.java
More file actions
265 lines (202 loc) · 8.99 KB
/
1.java
File metadata and controls
265 lines (202 loc) · 8.99 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
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
import java.io.*;
import java.util.*;
class DocumentVector
{
public static void main(String args[]) throws IOException
{
final int TOTAL_NO_DOCUMENT = 3;
String s,token;
StringTokenizer st;
TreeMap<String,Double> hm = new TreeMap<String,Double>();
Double sum =0.0;
for(int i=1;i<=TOTAL_NO_DOCUMENT;i++)//reading from 3 files
{
FileReader fr =new FileReader("hello" + i + ".txt");//reading from 3 files
BufferedReader br = new BufferedReader(fr);
//dumping the words in the dictionary
while((s=br.readLine()) != null)
{ st = new StringTokenizer(s,";, \n\t");
while(st.hasMoreTokens())
{
token = st.nextToken();
if(hm.get(token) == null)
{
hm.put(token,1.0);
}
else
{
Double value = hm.get(token);
hm.put(token,value +1);
}
// System.out.println(token);
}
//System.out.println(s);
}
fr.close();
}
Set<Map.Entry<String,Double>> set = hm.entrySet();
for(Map.Entry<String,Double> me : set)//dispalying key-value pairs
{
System.out.print(me.getKey() + ":");
System.out.println(me.getValue() );
}
for(Map.Entry<String,Double> me : set)//converting to idf values
{
Double value = (Math.log( TOTAL_NO_DOCUMENT/me.getValue())) / (Math.log(2)) ;//to calculate to the base 2
// System.out.print(me.getKey() + ":");
me.setValue(value);
}
//creating array of values
Double[] mapValues = new Double[hm.size()];
int pos=0;
for(Map.Entry<String,Double> me : set)
{
mapValues[pos] = me.getValue() ;
pos++;
//storing the set value in an array
}
for(Map.Entry<String,Double> me : set)//dispalying key-value pairs
{
System.out.print(me.getKey() + ":");
System.out.println(me.getValue() );
}
//creating string of keys
String[] mapKeys = new String[hm.size()];
pos =0;
for( String key : hm.keySet() )
{
mapKeys[pos++]=key;
}
//creating vector for each document
Vector< Vector<Double> > docuVector = new Vector<Vector<Double>>(TOTAL_NO_DOCUMENT);//vector of (vectors of double for each document )
Vector< Double > documentMagnitudeVector = new Vector< Double >(TOTAL_NO_DOCUMENT);//storing magnitude of each document vector
Double queryMagnitude=0.0;//storing magnitude of query vector
//Where Magic Happens !!
for(int i=1;i<=TOTAL_NO_DOCUMENT;i++)
{
Vector<Double> v = new Vector<Double>(hm.size());
for(int j=0 ; j<hm.size() ; j++)//initialising each element to 0
{
v.addElement(0.0);
}
FileReader fr =new FileReader("hello" + i + ".txt");//reading from 3 files
BufferedReader br = new BufferedReader(fr);
//checking frequency of words in dictionary and creating vector for each document
while((s=br.readLine()) != null)
{ st = new StringTokenizer(s,";, \n\t");
while(st.hasMoreTokens())
{
token = st.nextToken();
int index = Arrays.binarySearch(mapKeys,token);
double vectorValue = v.elementAt(index);
v.setElementAt(vectorValue + 1,index);
}
}
//after this, storing tdf*idf values in the vector
pos=0;
sum=0.0;
for(Double val : v)
{
//System.out.println("val: " + val + "pos: " + pos + "mapvalues: " + mapValues[pos]);
Double newVal = val* mapValues[pos];
v.setElementAt(newVal,pos);
sum += newVal*newVal;//calculating the magnitude for each vector
++pos;
}
//adding this vector to vector containing document vectors
docuVector.add(v);
//now storing the magnitude of this docuVector
documentMagnitudeVector.add(Math.sqrt(sum));
}
//input query
// 1. Create a Scanner using the InputStream available.
Scanner scanner = new Scanner( System.in );
// 2. Don't forget to prompt the user
System.out.print( "Input the query : " );
// 3. Use the Scanner to read a line of text from the user.
String query = scanner.nextLine();
Vector<Double> queryVector = new Vector<Double>(hm.size());
st = new StringTokenizer(query,";, \n\t");
for(int j=0 ; j<hm.size() ; j++)//initialising each element to 0 in the queryVector
{
queryVector.addElement(0.0);
}
Double maxFrequency = -1.0;//element to store max frequency of query vector
while(st.hasMoreTokens())
{
token = st.nextToken();
int index = Arrays.binarySearch(mapKeys,token);
double vectorValue = queryVector.elementAt(index);
queryVector.setElementAt(vectorValue + 1,index);
if(queryVector.get(index) > maxFrequency) //storing max frequency of query
maxFrequency = queryVector.get(index);
}
//after this, storing tdf*idf/maxFrequency values in the queryVector
pos=0;
sum = 0.0;
for(Double val : queryVector)
{
//System.out.println("val: " + val + "pos: " + pos + "mapvalues: " + mapValues[pos]);
Double newVal = (val * mapValues[pos] ) / maxFrequency;
queryVector.setElementAt(newVal,pos);
sum += newVal*newVal;//calculating the magnitude for query vector
++pos;
}
queryMagnitude = Math.sqrt(sum);
//displaying the vector
System.out.println("\n printing document vectors");
for(Vector<Double> v:docuVector)
{
for(Double val : v)
{
System.out.print(" " + val);
}
System.out.println("\n");
}
//displaying the query vector
System.out.println("\nPrinting the query vector:\n");
for(Double val : queryVector)
{
System.out.print(" " + val);
}
//
//display magnitude of query
System.out.println("\nmagnitude of query vector: " + queryMagnitude);
//displaying the magnitude vector
System.out.println("\nPrinting the document magnitude vector:\n");
System.out.println("no of ele in docu magni: " + documentMagnitudeVector.size());
for(Double val : documentMagnitudeVector)
{
System.out.print(" " + val);
}
System.out.println();
/*
Calculating Cosine Similarity Measure !
We have docuVector[] && queryVector and
We have documentMagnitudeVector && queryMagnitude
We'll do :
(docuVector[i]*queryVector)/(documentMagnitudeVector[i]*queryMagnitude)
*/
TreeMap<Double,Integer> cosSim = new TreeMap<Double,Integer>(Collections.reverseOrder());
for(int i=0;i<docuVector.size();i++){
Vector<Double> currDocVec=docuVector.get(i);
Double cosSimCurr=0.0;
for(int j=0;j<currDocVec.size();j++){
cosSimCurr+=(currDocVec.get(j)*queryVector.get(j));
}
cosSimCurr/=(documentMagnitudeVector.get(i)*queryMagnitude);
//System.out.println(cosSimCurr);
// cosSim.add(cosSimCurr);
cosSim.put(cosSimCurr,i+1);
}
System.out.println("Cosine Similarity Measure : ");
Set<Map.Entry<Double,Integer>> set1 = cosSim.entrySet();
for(Map.Entry<Double,Integer> me : set1)
{
System.out.println(me.getKey());
}
System.out.println("Documents to view in Order : ");
for(Map.Entry<Double,Integer> me:set1)
System.out.println(me.getValue());
}
}