-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolr.java
351 lines (312 loc) · 10.6 KB
/
solr.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.sax.WriteOutContentHandler;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
public class solr {
PrintWriter logfile;
int num_keywords, num_files, num_fileswithkeywords;
Map<String, Integer> keyword_counts;
Date timestamp;
List<String> keywords;
/**
* constructor DO NOT MODIFY
*/
public solr() {
keywords = new ArrayList<String>();
num_keywords = 0;
num_files = 0;
num_fileswithkeywords = 0;
keyword_counts = new HashMap<String, Integer>();
timestamp = new Date();
try {
logfile = new PrintWriter("log.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* destructor DO NOT MODIFY
*/
protected void finalize() throws Throwable {
try {
logfile.close();
} finally {
super.finalize();
}
}
/**
* main() function instantiate class and execute DO NOT MODIFY
*/
public static void main(String[] args) {
solr instance = new solr();
instance.run();
}
/**
* execute the program DO NOT MODIFY
*/
private void run() {
// Open input file and read keywords
try {
BufferedReader keyword_reader = new BufferedReader(new FileReader(
"keywords.txt"));
String str;
while ((str = keyword_reader.readLine()) != null) {
keywords.add(str);
num_keywords++;
keyword_counts.put(str, 0);
}
keyword_reader.close();
} catch (IOException e) {
e.printStackTrace();
}
// Open all pdf files, process each one
String outputFile = "output/" + "geotags.txt";
PrintWriter outfile_New;
try {
outfile_New = new PrintWriter(outputFile);
File pdfdir = new File("./vault");
File[] pdfs = pdfdir.listFiles(new PDFFilenameFilter());
//outfile_New.println("<doc>");
for (File pdf : pdfs) {
num_files++;
processfile(pdf, outfile_New);
System.out.println(num_files+" processed out of 2068");
}
//outfile_New.print("/<doc>");
outfile_New.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Print output file
/*try {
PrintWriter outfile = new PrintWriter("output.txt");
outfile.print("Keyword(s) used: ");
if (num_keywords > 0)
outfile.print(keywords.get(0));
for (int i = 1; i < num_keywords; i++)
outfile.print(", " + keywords.get(i));
outfile.println();
outfile.println("No of files processed: " + num_files);
outfile.println("No of files containing keyword(s): "
+ num_fileswithkeywords);
outfile.println();
outfile.println("No of occurrences of each keyword:");
outfile.println("----------------------------------");
for (int i = 0; i < num_keywords; i++) {
String keyword = keywords.get(i);
outfile.println("\t" + keyword + ": "
+ keyword_counts.get(keyword));
}
outfile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
}
/**
* Process a single file
*
* Here, you need to: - use Tika to extract text contents from the file -
* (optional) check OCR quality before proceeding - search the extracted
* text for the given keywords - update num_fileswithkeywords and
* keyword_counts as needed - update log file as needed
*
* @param f
* File to be processed
*/
private void processfile(File f, PrintWriter outfile) {
/***** YOUR CODE GOES HERE *****/
// to update the log file with a search hit, use:
// updatelog(keyword,f.getName());
String filename = new String(f.toString());
//filename = filename.replaceAll(".pdf", "");
filename = filename.replaceAll(".\\\\vault\\\\", ""); //<---CHANGED for Windows compatibility. change back for Mac
//filename = "output/" + filename + ".txt"; //<---CHANGED for Windows compatibility. change back for Mac
outfile.println("<doc>");
outfile.println("<field name = \"id\">" +filename + "</field>");
System.out.println(filename);
try {
PDFParser parser = new PDFParser();
InputStream fis = new FileInputStream(f);
StringWriter writer = new StringWriter();
ContentHandler handler = new WriteOutContentHandler(writer);
Metadata metadata = new Metadata();
parser.parse(fis, handler, metadata, new ParseContext());
String content = writer.toString();
content = content.replaceAll("\n", " ");
//String test = "M&^%$at____---t88 ** hu";
//test = test.replaceAll("[^A-Za-z0-9]", "");
System.out.print("Removing unnecessary chars....");
content = content.replaceAll("[^A-Za-z ]", ""); //remove all non alphanumeric characters
System.out.print("Done!\n");
String[] keyword = content.split(" ");
HashMap<String, Integer> keywords_freq = new HashMap<String, Integer>();
for (int i = 0; i < keyword.length; i++) {
if (keywords_freq.containsKey(keyword[i])) {
keywords_freq.put(keyword[i],
keywords_freq.get(keyword[i]) + 1);
} else {
keywords_freq.put(keyword[i], 1);
}
}
//System.out.println("THE SIZE OF THE KEYWORDS (before sort): "+keyword.length);
int[] freq = new int[keyword.length];
for (int i = 0; i < keyword.length; i++) {
freq[i] = keywords_freq.get(keyword[i]);
}
for (int c = 0; c < keyword.length - 1; c++) {
for (int d = 0; d < keyword.length - c - 1; d++) {
if (freq[d] > freq[d + 1]) /* For descending order use < */
{
int swap;
swap = freq[d];
freq[d] = freq[d + 1];
freq[d + 1] = swap;
String s = "";
s = keyword[d];
keyword[d] = keyword[d + 1];
keyword[d + 1] = s;
}
}
}
//for (int i = 0; i < keyword.length; i++) {
// System.out
// .println("keyword:" + keyword[i] + " freq:" + freq[i]);
// }
//PrintWriter outfile = new PrintWriter(filename);
ArrayList<String> keyword_print = new ArrayList<String>();
for (int i = keyword.length - 1; i > 0; i--) {
if (!keyword_print.contains(keyword[i])) {
keyword_print.add(keyword[i]);
//outfile.println(keyword[i]);
}
}
//System.out.println("THE SIZE OF THE KEYWORDS ARRAY(initialization): "+keyword_print.size());
//***************************************************************************** //GEOTAGGING PORTION
//*****************************************************************************
// private boolean geoTag(String term){
Pattern p = Pattern.compile("(\\-?\\d+(\\.\\d+))"); //<--check compatibility with mac: (\-?\d+(\.\d+))
Matcher m;
String term;
boolean found = false;
System.out.println("Searching doc for word matches...");
for(int i = 0; i < keyword_print.size(); i++){
//regex to find the latitude and longitude
term = keyword_print.get(i);
BufferedReader br;
try {
br = new BufferedReader(new FileReader("US.txt")); //<---Replace with US.txt for full version
String line;
ArrayList<String> coords = new ArrayList<String>();
boolean looking = true;
while ((line = br.readLine()) != null && looking) {
// process the line.
if(term.length() > 4 && line.contains(" "+term+" ")){
m = p.matcher(line);
//print the line the term occurs
//System.out.println(line);
while (m.find()) {
coords.add(m.group());
}
//print the content of the pdf
outfile.print("<field name = \"content\">");
String pdf_content;
for(int k = 0; k < keyword_print.size(); k++){
pdf_content = keyword_print.get(k);
outfile.print(pdf_content+" ");
}
System.out.println(" Term: "+term);
outfile.print("</field>");
//print the coordinates
for(int k = 0; k < coords.size(); k++){
//System.out.println(coords.get(k));
outfile.println("");
if(k%2 == 0) outfile.print("<field name=\"lat\">");
else outfile.print("<field name=\"long\">");
outfile.print(coords.get(k)+"</field>");
}
outfile.println("</doc>");
//System.out.println("DONE WITH THIS LAT/LONG");
found = true;
looking = false;
i = keyword_print.size(); // end the loop
//outPut(term, coords.get(0), coords.get(1));
}
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return false;
}
if(!found){
//print the content of the pdf
outfile.print("<field name = \"content\">");
String pdf_content;
//System.out.println("THE SIZE OF THE KEYWORDS ARRAY: "+keyword_print.size());
for(int k = 0; k < keyword.length; k++){
pdf_content = keyword[k];
outfile.print(pdf_content+" ");
}
outfile.print("</field>");
outfile.println("");
outfile.println("<field name=\"lat\">0.0</field>");
outfile.println("<field name=\"long\">0.0</field>");
outfile.println("</doc>");
}
System.out.println("Done with word matching!");
} catch (IOException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
/**
* Update the log file with search hit Appends a log entry with the system
* timestamp, keyword found, and filename of PDF file containing the keyword
* DO NOT MODIFY
*/
private void updatelog(String keyword, String filename) {
timestamp.setTime(System.currentTimeMillis());
logfile.println(timestamp + " -- \"" + keyword + "\" found in file \""
+ filename + "\"");
logfile.flush();
}
/**
* Filename filter that accepts only *.pdf DO NOT MODIFY
*/
static class PDFFilenameFilter implements FilenameFilter {
private Pattern p = Pattern.compile(".*\\.pdf",
Pattern.CASE_INSENSITIVE);
public boolean accept(File dir, String name) {
Matcher m = p.matcher(name);
return m.matches();
}
}
}