-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2.java
More file actions
237 lines (210 loc) · 6.56 KB
/
Copy pathp2.java
File metadata and controls
237 lines (210 loc) · 6.56 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
import java.io.*;
//package ChatBot;
import java.util.*;
import java.nio.file.*;
import java.util.Map.Entry;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
//import ChatBot;
public class p2 {
//attributes
private String[] myDocs;
private ArrayList<String> termAList;
private ArrayList<ArrayList<Doc>> docLists;
private double[] docLength;
ChatBot cb;
/**
* Construct an inverted index using tf-idf weighting
* @Tuheena Singh
* @param docs List of input strings or file names
*
*/
public void p24(String[] docs) {
//instanciate the attributes
myDocs = docs;
termAList = new ArrayList<String>();
docLists = new ArrayList<ArrayList<Doc>>();
ArrayList<Doc> docList;
for(int i=0;i<myDocs.length;i++) {
String[] words = myDocs[i].split(" ");
String word;
for(int j=0;j<words.length;j++) {
boolean match = false;
word = words[j];
if(!termAList.contains(word)) {
termAList.add(word);
docList = new ArrayList<Doc>();
Doc doc = new Doc(i, 1); //raw term frequence is one
docList.add(doc);
docLists.add(docList);
}
else {
int index = termAList.indexOf(word);
docList = docLists.get(index);
for(Doc did:docList) {
if(did.docId == i) {
did.tw++;
match = true;
break;
}
}
if(!match) {
Doc doc = new Doc(i,1);
docList.add(doc);
System.out.println(docLists);
}
}
}
}
// compute the tf.idf
int N = myDocs.length;
docLength = new double[N];
for(int i=0;i<termAList.size();i++) {
docList = docLists.get(i);
int df = docList.size();
Doc doc;
for(int j=0;j<docList.size();j++) {
doc = docList.get(j);
double tfidf = (1+Math.log10(doc.tw)) * Math.log10(N/df*1.0);
docLength[doc.docId] += Math.pow(tfidf,2);
doc.tw = tfidf;
docList.set(j,doc);
}
}
for(int i=0;i<N;i++) {
docLength[i] = Math.sqrt(docLength[i]);
}
}
public String readFileAsString(String fileName)throws Exception
{
String data = "";
data = new String(Files.readAllBytes(Paths.get(fileName)));
return data;
}
//Binary search for a stop word
public int searchStopWord(String key,String[] stopWords) {
Arrays.sort(stopWords);
int lo = 0;
int hi = stopWords.length -1;
while(lo <= hi) {
int mid = lo + (hi-lo)/2;
int result = key.compareTo(stopWords[mid]);
if (result < 0) hi = mid-1;
else if(result > 0) lo = mid +1;
else return mid;
}
return -1;
}
/**
* Compute matching score for documents by using cosine similarity with a value in [0,1]
* @param query user query in free form text
*/
public void rankSearch(String[] query) {
//resultset that contains the matching documents, each of which has an id and a matching score
HashMap<Integer, Double> docs = new HashMap<Integer, Double>();
//TO BE COMPLETED
ArrayList<Doc> docList;
for(String phrase : query)
{
int index = termAList.indexOf(phrase);
/* if phrase is not found*/
if(index == -1)
continue;
docList = docLists.get(index);
double termWeight = Math.log(myDocs.length*1.0/docList.size());
Doc doc;
for(int i = 0; i< docList.size(); i++)
{
doc = docList.get(i);
double scoreVal = termWeight * doc.tw;
if(!docs.containsKey(doc.docId))
{
docs.put(doc.docId, scoreVal);
}
else
{
scoreVal += docs.get(doc.docId);
docs.put(doc.docId, scoreVal);
}
}
}
/*Normalization of values */
double word = 0;
for(Entry<Integer, Double> entry : docs.entrySet())
{
word += entry.getValue();
}
HashMap<Integer, Double> nzDocs = new HashMap<Integer, Double>();
for(Entry<Integer, Double> entry : docs.entrySet())
{
nzDocs.put(entry.getKey(), entry.getValue()/word);
}
System.out.println("Original Value : "+docs);
System.out.println("Nomalized Value : "+ nzDocs);
Set<Map.Entry<Integer, Double>> mapSet = nzDocs.entrySet();
Map.Entry<Integer, Double> elementAt5 = (Map.Entry<Integer, Double>) mapSet.toArray()[0];
// System.out.println(elementAt5.getKey()); //This is giving the best match of Symptom Acc to user input.
System.out.println("The Best Match Symption is : " + myDocs[elementAt5.getKey()]);
}
/**
* Return the string representation of the index
*/
public String toString() {
String outString = new String();
ArrayList<Doc> docList;
for(int i=0;i<termAList.size();i++) {
outString += String.format("%-15s", termAList.get(i));
docList = docLists.get(i);
for(int j=0;j<docList.size();j++) {
outString += docList.get(j) + "\t";
}
outString += "\n";
}
return outString;
}
public static void main(String[] args) throws Exception {
//Creating the Frame
ChatBot cb=new ChatBot("Chat Bot");
cb.setSize(800,605);
cb.setLocation(50,50);
p2 p2 = new p2();
String data = p2.readFileAsString("stopwords.txt");
String datase = p2.readFileAsString("dis_sym_dataset_norm.csv");
String[] sub;
sub=datase.split("\n");
String[] search=sub[0].split(",");
p2.p24(search);
System.out.println(p2);
String[] query1 = { "fever"};
String[] query2 = { "ache"};
String[] query3 = { "neck","pain"};
String[] query4 = { "stomach","head"};
p2.rankSearch(query1);
p2.rankSearch(query2);
p2.rankSearch(query3);
p2.rankSearch(query4);
}
}
/**
*
* Document class that contains the document id and the term weight in tf-idf
*/
class Doc {
int docId;
double tw;
//ArrayList<Integer> positionList;
public Doc(int did, double tw) {
docId = did;
this.tw = tw;
}
public String toString() {
String docIdString = docId + ":" + tw;
return docIdString;
}
}
//© 2021 GitHub, Inc.