PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/pe1/src/cs276/pe1/lucene/IMDBSpelling.java

https://github.com/sarnoff/CS276
Java | 57 lines | 46 code | 7 blank | 4 comment | 2 complexity | 1e5c77a6e4e4ed1b81dd425046b6c10f MD5 | raw file
  1. package cs276.pe1.lucene;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.InputStreamReader;
  5. import java.util.List;
  6. import org.apache.lucene.analysis.Analyzer;
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.index.IndexReader;
  10. import org.apache.lucene.queryParser.QueryParser;
  11. import org.apache.lucene.search.Hits;
  12. import org.apache.lucene.search.IndexSearcher;
  13. import org.apache.lucene.search.Query;
  14. import org.apache.lucene.store.Directory;
  15. import org.apache.lucene.store.FSDirectory;
  16. import org.apache.lucene.search.spell.Dictionary;
  17. import org.apache.lucene.search.spell.LuceneDictionary;
  18. import org.apache.lucene.search.spell.PlainTextDictionary;
  19. import org.apache.lucene.search.spell.SpellChecker;
  20. import cs276.pe1.spell.KGramSpellingCorrector;
  21. import cs276.pe1.spell.KGramWithEditDistanceSpellingCorrector;
  22. @SuppressWarnings("deprecation")
  23. public class IMDBSpelling {
  24. public static void main(String[] args) throws Exception {
  25. //Read search query
  26. InputStreamReader input = new InputStreamReader(System.in);
  27. BufferedReader reader = new BufferedReader(input);
  28. String search = reader.readLine();
  29. //Spelling correct query
  30. KGramWithEditDistanceSpellingCorrector k = new KGramWithEditDistanceSpellingCorrector();
  31. List<String> suggest = k.corrections(search);
  32. for(String s : suggest) search += " " + s;
  33. //Run Lucene search
  34. Directory index = FSDirectory.getDirectory(new File(new File(System.getProperty("user.home")),"cs276-index"));
  35. IndexSearcher searcher = new IndexSearcher(index);
  36. Analyzer analyzer = new StandardAnalyzer();
  37. QueryParser parser = new QueryParser("title",analyzer);
  38. Query query = parser.parse(search);
  39. Hits hits = searcher.search(query);
  40. //Display results
  41. System.out.println("Results: "+hits.length());
  42. int num = Math.min(20, hits.length());
  43. for(int i = 0; i < num; i++) {
  44. Document hit = hits.doc(i);
  45. System.out.println(" "+hit.get("title"));
  46. }
  47. index.close();
  48. searcher.close();
  49. }
  50. }