-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMySearchEngine.java
105 lines (104 loc) · 4.73 KB
/
MySearchEngine.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
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
public class MySearchEngine
{
public MySearchEngine()
{
}
public static void main(String args[]) throws Exception
{
List<String> resultList = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
ArrayList<String> hashStrings = new ArrayList<String>();
FileScanner fileScan = new FileScanner();
System.out.println("Welcome to Kang's Search Engine!");
String instruct = args[0];
if (instruct.equals("index"))
{
String dirPath = args[1];
String targetPath = args[2];
String stopWordsPath = args[3];
while (!fileScan.isDir(dirPath))
{
System.out.println("Error, the inputted path is not a valid diractory.");
System.out.print("Please try again to enter the directory to be indexed: ");
dirPath = scan.nextLine().trim();
}
while (!fileScan.isFile(stopWordsPath))
{
System.out.println("Error, the inputted stop words list path is not a valid file.");
System.out.print("Please try again to enter the stop words list file path: ");
stopWordsPath = scan.nextLine().trim();
}
if (!fileScan.isDir(targetPath))
{
File target = new File(targetPath);
target.mkdir();
}
System.out.println(" Indexing " + dirPath + " to " + targetPath +
" using stop words from " + stopWordsPath);
long startTime=System.currentTimeMillis();
Indexer inde = new Indexer();
hashStrings = inde.indexing(inde.fileToTokens(dirPath, stopWordsPath), dirPath, targetPath);
long endTime=System.currentTimeMillis();
double excTime = (double)(endTime - startTime) / 1000;
System.out.println("Index completed. Whole session completed in " + excTime + "s.");
}
else if (instruct.equals("search"))
{
Searcher search = new Searcher();
String indexPath = args[1];
String numberOfDocsString = args[2];
Indexer inde = new Indexer();
String[] keywordList = Arrays.copyOfRange(args, 3, args.length);
while (!fileScan.isDir(indexPath))
{
System.out.println("Error, the inputted path is not a valid diractory.");
System.out.print("Please try again to enter the directory to be searched: ");
indexPath = scan.nextLine().trim();
}
while (!numberOfDocsString.matches("[0-9]+"))
{
System.out.println("Error, the inputted information is not a valid itteger number.");
System.out.print("Please try again to enter the number of files to be shown: ");
numberOfDocsString = scan.nextLine().trim();
}
int numberOfDocs = Integer.parseInt(numberOfDocsString);
String keywordString = new String();
for (String keyword : keywordList)
{
keywordString = keywordString + "," + keyword;
}
keywordString = keywordString.substring(1);
System.out.println("Searching " + indexPath + " for keywords " + keywordString + " and showing top " + numberOfDocsString + " results.");
ArrayList<String> keywordArray = new ArrayList<String>();
Tokenizer toke = new Tokenizer();
keywordArray = toke.fileToWords(keywordString);
keywordList = inde.queryToStemmed(keywordArray);
Hashtable<String, Double> result = search.generateDocVector(search.fileToIndex(indexPath), keywordList);
resultList = search.sortResult(result);
DecimalFormat decimalFormat=new DecimalFormat();
decimalFormat.applyPattern("#0.000");
for (int index = 0; index < Math.min(resultList.size(), numberOfDocs); index++)
{
System.out.println(resultList.get(index) + " " + decimalFormat.format(result.get(resultList.get(index))));
}
}
else if (instruct.equals("correct"))
{
WordCorrection corrector = new WordCorrection();
String[] inputtedList = Arrays.copyOfRange(args, 1, args.length);
String inputtedString = new String();
for (String inputted : inputtedList)
{
inputtedString = inputtedString + " " + inputted;
}
corrector.correction(inputtedString);
}
else
{
System.out.println("Error, please enter a valid instruction.");
}
}
}