-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleQAProgram.java
More file actions
290 lines (243 loc) · 8.11 KB
/
SimpleQAProgram.java
File metadata and controls
290 lines (243 loc) · 8.11 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
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
class item {
String name;
Double[] tfidf;
item(String name) {
this.name = name;
this.tfidf = new Double[225];
for(int i=0;i<tfidf.length;i++) {
tfidf[i]= (double) 0;
}
}
item(String name, Double[] tfidf){
this.name=name;
this.tfidf=tfidf;
}
}
class item1{
String name;
Double[] tfidf;
item1(String name){
this.name = name;
this.tfidf = new Double[1400];
for(int i=0;i<tfidf.length;i++) {
tfidf[i]=(double) 0;
}
}
item1(String name, Double[] tfidf){
this.name=name;
this.tfidf=tfidf;
}
}
class numbers{
Double a;
Double[] b;
numbers(Double a, Double[] b){
this.a=a;
this.b=b;
}
}
public class SimpleQAProgram {
public static void main(String args[]) throws IOException {
String file = args[0];
String file1 = args[1];
BufferedReader queries = new BufferedReader(new FileReader(args[0]));
BufferedReader abstracts = new BufferedReader(new FileReader(args[1]));
String[] closed_class_stop_words = {"a","the","an","and","or","but","about","above","after","along","amid","among",
"as","at","by","for","from","in","into","like","minus","near","of","off","on",
"onto","out","over","past","per","plus","since","till","to","under","until","up",
"via","vs","with","that","can","cannot","could","may","might","must",
"need","ought","shall","should","will","would","have","had","has","having","be",
"is","am","are","was","were","being","been","get","gets","got","gotten",
"getting","seem","seeming","seems","seemed",
"enough", "both", "all", "your", "those", "this", "these",
"their", "the", "that", "some", "our", "no", "neither", "my",
"its", "his", "her", "every", "either", "each", "any", "another",
"an", "a", "just", "mere", "such", "merely", "right", "no", "not",
"only", "sheer", "even", "especially", "namely", "as", "more",
"most", "less", "least", "so", "enough", "too", "pretty", "quite",
"rather", "somewhat", "sufficiently", "same", "different", "such",
"when", "why", "where", "how", "what", "who", "whom", "which",
"whether", "why", "whose", "if", "anybody", "anyone", "anyplace",
"anything", "anytime", "anywhere", "everybody", "everyday",
"everyone", "everyplace", "everything", "everywhere", "whatever",
"whenever", "whereever", "whichever", "whoever", "whomever", "he",
"him", "his", "her", "she", "it", "they", "them", "its", "their",
"theirs","you","your","yours","me","my","mine","I","we","us","much","and/or"};
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
String line;
//Calculate the occurrence of words of the queries
ArrayList<item> ques = new ArrayList<item>();
int count_q=0; //query number
while((line = queries.readLine())!=null){
String[] words = line.split("\\s+");
if((!words[0].equals(".I"))&&(!words[0].equals(".W"))) {
words = line.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"); //remove punctuation and numbers
for(int i=0;i<words.length;i++) {
if(!isFound(words[i],closed_class_stop_words)) { //removing stop words
if(isFoundinArrayList(words[i], ques)==-1) { //check if the word is in the arraylist
item a = new item(words[i]);
a.tfidf[count_q-1]=(double) 1;
ques.add(a);
}else {
int index = isFoundinArrayList(words[i],ques);
Double[] newA = ques.get(index).tfidf;
newA[count_q-1]+=1;
item newI = new item(words[i],newA);
ques.set(index, newI);
}
}
}
}
if(words[0].equals(".I")) {
count_q++;
}
}
//Calculate TFIDF of queries
for(int i=0; i<ques.size();i++) {
int cal=0;
for(int j=0;j<ques.get(i).tfidf.length;j++) {
if(ques.get(i).tfidf[j]>0)
cal++;
}
Double idf = Math.log(225/cal);
Double[] temp = ques.get(i).tfidf;
for(int j=0;j<temp.length;j++) {
if(temp[j]!=0)
temp[j]=temp[j]*idf;
}
item newI = new item(ques.get(i).name,temp);
ques.set(i, newI);
}
//Calculate occurrence of words of abstracts
String line1;
int count_a=1;
ArrayList<item1> abs = new ArrayList<item1>();
line1 = abstracts.readLine();
while((line1 = abstracts.readLine())!=null){
String[] check = line1.split("\\s+");
if(check[0].equals(".W")) {
while(true){
line1 = abstracts.readLine();
if(line1==null)
break;
check = line1.split("\\s+");
if(check[0]!=null && check[0].equals(".I")){
count_a++;
break;
}
String[] words = line1.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"); //remove punctuation and numbers
if(words.length==0)
continue;
for(int i=0;i<words.length;i++) {
if(!isFound(words[i],closed_class_stop_words)) { //removing stop words
if(isFoundinArrayList(words[i],ques)!=-1) {
if(isFoundinArrayList1(words[i], abs)==-1) { //check if the word is in the arraylist
item1 a = new item1(words[i]);
a.tfidf[count_a-1]=(double) 1;
abs.add(a);
}else {
int index = isFoundinArrayList1(words[i],abs);
Double[] newA = abs.get(index).tfidf;
newA[count_a-1]+=1;
item1 newI = new item1(words[i],newA);
abs.set(index, newI);
}
}
}
}
}
if(line1==null)
break;
}
}
//Calculate TFIDF of abstracts
for(int i=0; i<abs.size();i++) {
int cal=0;
for(int j=0;j<abs.get(i).tfidf.length;j++) {
if(abs.get(i).tfidf[j]>0)
cal+=1;
}
if(cal==0)
continue;
Double idf = Math.log(1400/cal);
Double[] temp = abs.get(i).tfidf;
for(int j=0;j<temp.length;j++) {
if(temp[j]!=0)
temp[j]=temp[j]*idf;
}
item1 newI = new item1(abs.get(i).name,temp);
abs.set(i, newI);
}
//Calculate Cosine Similarity
ArrayList<numbers> match = new ArrayList<numbers>();
for(int i=0;i<225;i++) {
for(int j=0;j<ques.size();j++) {
if(ques.get(j).tfidf[i]!=0) {
Double a = ques.get(j).tfidf[i];
int index = isFoundinArrayList1(ques.get(j).name,abs);
if(index!=-1) {
Double[] b = abs.get(index).tfidf;
numbers num = new numbers(a,b);
match.add(num);
}
}
}
HashMap<Integer,Double> cos_s = similarityCalc(match);
for(int a_id: cos_s.keySet()) {
out.write(i+1 + " " + a_id + " " + cos_s.get(a_id) + "\n");
}
}
out.close();
queries.close();
abstracts.close();
}
public static boolean isFound(String word, String[] words) {
for(int i=0;i<words.length;i++) {
if(words[i].equals(word))
return true;
}
return false;
}
public static int isFoundinArrayList(String word, ArrayList<item> items) {
for(int i=0;i<items.size();i++) {
if(items.get(i).name.equals(word))
return i;
}
return -1;
}
public static int isFoundinArrayList1(String word, ArrayList<item1> items1) {
for(int i=0;i<items1.size();i++) {
if(items1.get(i).name.equals(word))
return i;
}
return -1;
}
public static HashMap<Integer, Double> similarityCalc(ArrayList<numbers> match){
HashMap<Integer, Double> sim_table = new HashMap<>();
Double nume = (double) 0; //numerator
Double deno1 =(double) 0; //denominator
Double deno2= (double) 0;
Double cosS;
for(int i=0;i<1400;i++) {
for(int j=0;j<match.size();j++) {
Double a = match.get(j).a;
Double b = match.get(j).b[i];
if(i==485){
System.out.println(a+" "+b);
}
nume+=a*b;
deno1+=Math.pow(a, 2);
deno2+=Math.pow(b, 2);
}
if(deno1*deno2==0)
cosS=(double) 0;
else
cosS=nume/Math.sqrt(deno1*deno2);
sim_table.put(i+1, cosS);
if(i==485){
System.out.println("///////cos= "+ cosS);
}
}
return sim_table;
}
}