/** * Build an invertedIndexMap based on given directory, do partial search on the invertedIndexMap * and output the result to a certain file. Output the specific invertedIndexMap of provided * directory to a certain file if required. * * @param args the argument that input to test the program. */ public static void main(String[] args) { ArgumentParser argParser = new ArgumentParser(args); if (argParser.hasFlag("-d") && argParser.hasFlag("-q") && argParser.hasFlag("-t")) { String rootPath = argParser.getValue("-d"); File root = new File(rootPath); String queryFilePath = argParser.getValue("-q"); File queryFile = new File(queryFilePath); try { int threads = Integer.parseInt(argParser.getValue("-t")); InvertedIndex wordsMap = new InvertedIndex(); InvertedIndexBuilder mapBuilder = new InvertedIndexBuilder(wordsMap, threads); mapBuilder.parseDir(root); PartialSearch partialSearcher = new PartialSearch(wordsMap, threads); partialSearcher.partialSearch(queryFile, "searchresults.txt"); if (argParser.hasFlag("-i")) { wordsMap.outputToFile("invertedindex.txt"); } } catch (Exception e) { System.out.println("<threads> should be an Integer."); } } else { System.out.println( "Usage: java -cp project2.jar Driver -d <directory> -q <queryfile> -t <threads> [-i]"); } }
/** Create the inverted index from search results */ public void buildIndex() { System.out.println("Indexing results ...."); invIdx.clear(); ArrayList<ResultNode> resultList = result.getResultNodes(); for (ResultNode rn : resultList) { rn.setTerms(); invIdx.addDocument(rn); } }
public static void main(String[] args) throws IOException { InvertedIndex invertedIndex = new InvertedIndex(); if (args.length != 1) { System.out.println("Usage: java -jar InvertedIndexMain.jar <file>"); System.exit(1); } invertedIndex.buildFromTextFile(args[0]); Console console = System.console(); /** * Loop through queries. Return "No hits!" if query String has no results. Use exit! to exit the * loop and programm. If query has a result, print result and highlight the query words. */ EvaluateBenchmark bench = new EvaluateBenchmark(invertedIndex); bench.readBenchmarkFile(); while (true) { String query = console .readLine( "Enter your search" + " query (separate words with spaces, enter exit! to leave):") .toLowerCase(); if (query.equals("exit!")) { break; } String[] words = query.split("\\W+"); ArrayList<Pair> queryResult = invertedIndex.processQuery(words); if (queryResult.isEmpty() || query.isEmpty()) { System.out.println("No hits!"); } else { for (int i = 0; i < 3 && i < queryResult.size(); i++) { int docId = queryResult.get(i).documentId; String text = invertedIndex.documents.get(docId - 1); String ansiRed = "\u001B[31m"; String ansiReset = "\u001B[0m"; for (String word : words) { text = text.replaceAll("((?i)" + word + ")", ansiRed + "$1" + ansiReset); } System.out.println("\n" + text + "\n"); queryResult.get(i).prettyPrint(); } } } }
@Override public void run() { ArrayList<String> querylist = new ArrayList<String>(); if (inputline != null && !inputline.isEmpty()) { String[] wordarray = inputline.split("\\s"); for (String words : wordarray) { normalizeWord(words); querylist.add(words); } ArrayList<SearchResults> results = index.partialSearch(querylist); lock.acquireWriteLock(); map.put(inputline, results); lock.releaseWriteLock(); } decrementPending(); }