-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearcher.java
More file actions
234 lines (194 loc) · 8.34 KB
/
Searcher.java
File metadata and controls
234 lines (194 loc) · 8.34 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
import com.medallia.word2vec.Word2VecModel;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class Searcher {
boolean useStemmer;
boolean useSemantic;
Ranker ranker;
String outputPath;
static String queryNumber;
int queryRndNum;
public Searcher(Dictionary Dictionaries, String postingPath) {
ranker = new Ranker(Dictionaries,postingPath);
outputPath="";
//queryRndNum = 0;
useStemmer = false;
useSemantic = true;
}
/**
* this function get query - words to search on posting filesE
* @param query - String of words
*/
public ArrayList<String> stringQuery(String query) throws IOException {
ranker.useStemmer = useStemmer;
String [] splitTermsQuery = query.split(" ");
if (useSemantic){
splitTermsQuery = semanticValues(splitTermsQuery);
}
//in this step we need parse the words in query and send them after parse to Ranker
splitTermsQuery = parseQuery(splitTermsQuery);
Random random = new Random();
ArrayList<String> rankDocs = ranker.rankDocsByQuery(splitTermsQuery); //send query to ranker
File tempFile = new File(outputPath);
if (tempFile.exists()) {
FileInputStream inputStream = new FileInputStream(outputPath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String lastLine = "";
line = bufferedReader.readLine();
while (line != null) {
if (line.equals("")){
String queryNum = lastLine.split(" ")[0];
//queryRndNum = Integer.parseInt(queryNum) + 1;
//queryRndNum++;
break;
}
else {
lastLine = bufferedReader.readLine();
//queryRndNum++;
if (lastLine == null || lastLine.equals("")) {
String queryNum = line.split(" ")[0];
//queryRndNum = Integer.parseInt(queryNum) + 1;
//queryRndNum++;
break;
}
line = bufferedReader.readLine();
}
}
}
else{
queryRndNum = 100;
}
queryRndNum = random.nextInt(900)+100;
writeResults(rankDocs,""+queryRndNum); //send specific words
return rankDocs;
}
public ArrayList<String> stringQueryFromPath(String query) throws IOException {
ranker.useStemmer = useStemmer;
String [] splitTermsQuery = query.split(" ");
if (useSemantic){
splitTermsQuery = semanticValues(splitTermsQuery);
}
//in this step we need parse the words in query and send them after parse to Ranker
splitTermsQuery = parseQuery(splitTermsQuery);
ArrayList<String> rankDocs = ranker.rankDocsByQuery(splitTermsQuery); //send query to ranker
return rankDocs;
}
/**
* this function get path for queries file
* @param queryPath - string of pathS
*/
public HashMap<String,ArrayList<String>> pathQuery(String queryPath) throws IOException {
FileInputStream inputStream = new FileInputStream(queryPath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
ArrayList<String> rankDocs = new ArrayList<>();
HashMap<String,ArrayList<String>> allRankDocs = new HashMap<>();
String line = bufferedReader.readLine();
String queryWords = "";
String [] splitLine = {};
String queryNumber = "";
while (line!=null){
if (line.contains("<num>")){
splitLine = line.split(" ");
queryNumber = splitLine[2];
}
if (line.contains("<title>")){
splitLine = line.split("<title> ");
queryWords = splitLine[1];
rankDocs = stringQueryFromPath(queryWords.toString());
allRankDocs.put(queryNumber,rankDocs);
writeResults(rankDocs,queryNumber); //send specific words
}
line = bufferedReader.readLine();
}
inputStream.close();
bufferedReader.close();
return allRankDocs;
}
/**
* this function parse the terms in the query and return an array of parse words
* @param query - array of terms
* @return array of terms after parse
*/
/////////////////////////////////////////////////////////////need to implement !!!!!!!!!!!!!!!!!!!
public String [] parseQuery (String [] query){
//String [] queryAfterParse = new String[query.length];
ArrayList<String> entities = new ArrayList<>();
int counter = 0;
Parse parser = null;
Stemmer stemmer = new Stemmer();
String entitie="";
try {
parser = new Parse();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//*********************************************//
for (int i=0 ; i<query.length ;i++) {
if (!query[i].equals("") && !query[i].equals("-")) {
if(useStemmer){
query[i] = stemmer.stem(query[i]);
}
if (parser.isNumber(query[i])) {
parser.removePunctuation(query, i);
if (parser.isExpression(query, i)) {
query[i] = parser.typeOfExpression(query, i);
} else if (parser.isPrice(query, i)) {
query[i] = parser.typeOfPrice(query, i);
} else if (parser.isPercent(query, i)) {
query[i] = parser.typeOfPercent(query, i);
} else if (parser.isKgsOrKm(query, i)) {
query[i] = parser.typeOfKgs(query, i);
} else if (parser.isGMT(query, i)) {
query[i] = parser.typeOfGMT(query, i);
} else if (parser.isDate(query, i)) {
query[i] = parser.typeOfDate(query, i);
} else if (parser.isRegularNumber(query, i)) {
query[i] = parser.typeOfRegularNumber(query, i);
}
}
}
}
return query;
}
public void writeResults (ArrayList<String> rankDocs, String queryNum) throws IOException {
if (!outputPath.equals("")) {
FileWriter posting = new FileWriter(outputPath, true);
BufferedWriter writer = new BufferedWriter(posting);
for (String s : rankDocs) {
writer.write(queryNum + " 0 " + s + " 1" + " 42.38" + " mt");
writer.newLine();
}
writer.close();
}
}
public String [] semanticValues (String [] query) {
String [] queryWithSemanicWords = query;
ArrayList<String> matchesWords = new ArrayList<>();
try {
Word2VecModel model = Word2VecModel.fromTextFile(new File("src\\word2vec.c.output.model.txt"));
com.medallia.word2vec.Searcher semanticSearcher = model.forSearch();
int numOfResultInList = 50;
for (int i=0 ; i<query.length ; i++) {
List<com.medallia.word2vec.Searcher.Match> matches = semanticSearcher.getMatches(query[i], numOfResultInList);
//matchesWords.add(matches.get(1).match());
matchesWords.add(matches.get(2).match());
}
queryWithSemanicWords = new String[matchesWords.size()];
for (int i =0 ; i<matchesWords.size(); i++) {
queryWithSemanicWords[i] = matchesWords.get(i);
}
}
catch (IOException e) {
// e.printStackTrace();
return queryWithSemanicWords;
} catch (com.medallia.word2vec.Searcher.UnknownWordException e) {
// e.printStackTrace();
return queryWithSemanicWords;
}
return queryWithSemanicWords;
}
}