-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentDistanceExample.java
200 lines (168 loc) · 4.28 KB
/
DocumentDistanceExample.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
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;
import java.util.*;
/**
* Document Distance algorithm
*
* @author Ethan
*
*/
public class DocumentDistanceExample
{
public static Hashtable<String, Integer> doc1Freq;
public static Hashtable<String, Integer> doc2Freq;
public static double documentDistance(List<String> document1, List<String> document2)
{
// append document lines into one string
String doc1 = appendDocument(document1);
String doc2 = appendDocument(document2);
// Split documents into words
ArrayList<String> doc1Split = splitDocument(doc1);
ArrayList<String> doc2Split = splitDocument(doc2);
// Compute word frequencies
computeFrequencies(doc1Split, doc2Split);
// Compute dot product
int dotProduct = computeDotProduct();
// Divide dot product by the length(magnitude) of the two vectors
double lengthOfBothVectors = computeLengthOfBothVectors(doc1Freq, doc2Freq);
double result = ((double) dotProduct / lengthOfBothVectors);
result = Math.acos(result);
return result;
}
public static void main(String[] args)
{
try
{
String filePath = new File("").getAbsolutePath();
String doc1path = new String(filePath+"/src/miscellaneous/document_distance_inputs/doc1.txt");
String doc2path = new String(filePath+"/src/miscellaneous/document_distance_inputs/doc2.txt");
List<String> document1 = new ArrayList<String>(Files.readAllLines(Paths.get(doc1path)));
List<String> document2 = new ArrayList<String>(Files.readAllLines(Paths.get(doc2path)));
double result = documentDistance(document1, document2);
System.out.println(result);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
// ========================= UTILITIES ========================= //
/**
* O(n)
*
* @param document
* @return
*/
public static String appendDocument(List<String> document)
{
String doc = "";
for(String line : document)
{
doc = doc.concat(line+" ");
}
return doc;
}
/**
* O(n)
* Regex is not reliable and can grow exponentially or linearly
*
* @param doc
* @return
*/
public static ArrayList<String> splitDocument(String doc)
{
ArrayList<String> docSplit = new ArrayList<String>();
for(String word : doc.split("\\s+"))
{
word = word.replaceAll("[^A-Za-z0-9]", "");
docSplit.add(word.toLowerCase());
}
return docSplit;
}
/**
* O(doc1 * doc2)
*
* @param doc1
* @param doc2
*/
public static void computeFrequencies(ArrayList<String> doc1, ArrayList<String> doc2)
{
doc1Freq = new Hashtable<String, Integer>();
doc2Freq = new Hashtable<String, Integer>();
for(String D1 : doc1)
{
if(doc1Freq.containsKey(D1))
{
doc1Freq.put(D1, doc1Freq.get(D1) + 1);
}
else
{
doc1Freq.put(D1, 1);
}
}
for (String D2: doc2)
{
if(doc2Freq.containsKey(D2))
{
doc2Freq.put(D2, doc2Freq.get(D2) + 1);
}
else
{
doc2Freq.put(D2, 1);
}
}
}
/**
* O(n^2)
*
* @return
*/
public static int computeDotProduct()
{
int sum = 0;
Set<String> keysOfDoc1 = doc1Freq.keySet();
Set<String> keysOfDoc2 = doc2Freq.keySet();
for(String word1 : keysOfDoc1)
{
for(String word2 : keysOfDoc2)
{
if(word1.equals(word2))
{
sum += doc1Freq.get(word1) * doc2Freq.get(word2);
}
}
}
return sum;
}
/**
* O(n)
*
* @param doc1Freq
* @param doc2Freq
* @return
*/
public static double computeLengthOfBothVectors(Hashtable<String, Integer> doc1Freq, Hashtable<String, Integer> doc2Freq)
{
double lengthOfVector1 = magnitudeOfVector(doc1Freq);
double lengthOfVector2 = magnitudeOfVector(doc2Freq);
return lengthOfVector1 * lengthOfVector2;
}
// ========================= SUB UTILITIES ========================= //
/**
* O(n)
*
* @param docFreq
* @return
*/
public static double magnitudeOfVector(Hashtable<String, Integer> docFreq)
{
double sumOfSqrs = 0;
Set<String> keysOfDoc = docFreq.keySet();
for(String key : keysOfDoc)
{
sumOfSqrs = Math.pow(docFreq.get(key), 2) + sumOfSqrs;
}
return Math.sqrt(sumOfSqrs);
}
}