List of usage examples for org.apache.lucene.search.spell SpellChecker setSpellIndex
public void setSpellIndex(Directory spellIndexDir) throws IOException
spellIndex
is the same value as given in the constructor. From source file:fastcampus.lucene.example.search.SpellCheckerExample.java
License:Apache License
public static void main(String[] args) throws Exception { Directory directory = FSDirectory.open(Paths.get("./index/spell/")); SpellChecker spellChecker = new SpellChecker(directory); //Analyzer analyzer = new StandardAnalyzer(); // ? Analyzer analyzer = new Analyzer() { @Override/* w w w . j a v a 2 s . com*/ protected TokenStreamComponents createComponents(String s) { Reader reader = new StringReader(s); Tokenizer tokenizer = new StandardTokenizer(); tokenizer.setReader(reader); String name = "nfc_cf"; Normalizer2 normalizer = Normalizer2.getInstance(null, name, Normalizer2.Mode.DECOMPOSE); TokenFilter filter = new ICUNormalizer2Filter(tokenizer, normalizer); return new TokenStreamComponents(tokenizer, filter); } }; IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); //?? Writer? ? ? Path path = Paths.get("./data/spell/dic.txt"); spellChecker.setSpellIndex(directory); spellChecker.clearIndex(); spellChecker.indexDictionary(new PlainTextDictionary(path), indexWriterConfig, true); String wordForSuggestions = "?"; //spellChecker.setStringDistance(new LevensteinDistance()); //#Levenstein spellChecker.setStringDistance(new JaroWinklerDistance()); //Jaro-Winkler int suggestionsNumber = 1; String[] suggestions = spellChecker.suggestSimilar(wordForSuggestions, suggestionsNumber); if (suggestions != null && suggestions.length > 0) { for (String word : suggestions) { System.out.println("Did you mean:" + word); } } else { System.out.println("No suggestions found for word:" + wordForSuggestions); } }
From source file:org.silverpeas.core.index.search.model.DidYouMeanSearcher.java
License:Open Source License
/** * @param queryDescription/* w w w . j a va 2 s . c o m*/ * @return * @throws org.silverpeas.core.index.search.model.ParseException * @throws ParseException */ public String[] suggest(QueryDescription queryDescription) throws org.silverpeas.core.index.search.model.ParseException, IOException { spellCheckers.clear(); String[] suggestions = null; // The variable field is only used to parse the query String and to obtain the words that will // be used for the search final String field = "content"; if (StringUtil.isDefined(queryDescription.getQuery())) { // parses the query string to prepare the search Analyzer analyzer = indexManager.getAnalyzer(queryDescription.getRequestedLanguage()); QueryParser queryParser = new QueryParser(field, analyzer); Query parsedQuery; try { parsedQuery = queryParser.parse(queryDescription.getQuery()); } catch (org.apache.lucene.queryparser.classic.ParseException exception) { try { parsedQuery = queryParser.parse(QueryParser.escape(queryDescription.getQuery())); } catch (org.apache.lucene.queryparser.classic.ParseException pe) { throw new org.silverpeas.core.index.search.model.ParseException("DidYouMeanSearcher", pe); } } // splits the query to realize a separated search with each word this.query = parsedQuery.toString(field); StringTokenizer tokens = new StringTokenizer(query); // gets spelling index paths Set<String> spellIndexPaths = indexSearcher.getIndexPathSet(queryDescription.getWhereToSearch()); try { while (tokens.hasMoreTokens()) { SpellChecker spellCheck = new SpellChecker(FSDirectory.open(uploadIndexDir.toPath())); spellCheckers.add(spellCheck); String token = tokens.nextToken().replaceAll("\"", ""); for (String path : spellIndexPaths) { // create a file object with given path File file = new File(path + "Spell"); if (file.exists()) { // create a spellChecker with the file object FSDirectory directory = FSDirectory.open(file.toPath()); spellCheck.setSpellIndex(directory); // if the word exist in the dictionary, we stop the current treatment and search the // next word because the suggestSimilar method will return the same word than the given word if (spellCheck.exist(token)) { continue; } spellCheck.suggestSimilar(token, 1); } } } } catch (IOException e) { SilverLogger.getLogger(this).error(e.getMessage(), e); } suggestions = buildFinalResult(); } return suggestions; }
From source file:org.silverpeas.search.searchEngine.model.DidYouMeanSearcher.java
License:Open Source License
/** * @param queryDescription//w ww .j a va 2 s . c o m * @return * @throws org.silverpeas.search.searchEngine.model.ParseException * @throws ParseException */ public String[] suggest(QueryDescription queryDescription) throws org.silverpeas.search.searchEngine.model.ParseException, IOException { String[] suggestions = null; // The variable field is only used to parse the query String and to obtain the words that will // be used for the search final String field = "content"; if (StringUtil.isDefined(queryDescription.getQuery())) { // parses the query string to prepare the search Analyzer analyzer = new IndexManager().getAnalyzer(queryDescription.getRequestedLanguage()); QueryParser queryParser = new QueryParser(Version.LUCENE_36, field, analyzer); Query parsedQuery; try { parsedQuery = queryParser.parse(queryDescription.getQuery()); } catch (ParseException exception) { throw new org.silverpeas.search.searchEngine.model.ParseException("DidYouMeanSearcher", exception); } // splits the query to realize a separated search with each word this.query = parsedQuery.toString(field); StringTokenizer tokens = new StringTokenizer(query); // gets spelling index paths WAIndexSearcher waIndexSearcher = new WAIndexSearcher(); Set<String> spellIndexPaths = waIndexSearcher .getIndexPathSet(queryDescription.getSpaceComponentPairSet()); try { while (tokens.hasMoreTokens()) { SpellChecker spellCheck = new SpellChecker(FSDirectory.open(uploadIndexDir)); spellCheckers.add(spellCheck); String token = tokens.nextToken().replaceAll("\"", ""); for (String path : spellIndexPaths) { // create a file object with given path File file = new File(path + "Spell"); if (file.exists()) { // create a spellChecker with the file object FSDirectory directory = FSDirectory.open(file); spellCheck.setSpellIndex(directory); // if the word exist in the dictionary, we stop the current treatment and search the // next word because the suggestSimilar method will return the same word than the given word if (spellCheck.exist(token)) { continue; } spellCheck.suggestSimilar(token, 1); } } } } catch (IOException e) { SilverTrace.error("searchEngine", DidYouMeanIndexer.class.toString(), "root.EX_LOAD_IO_EXCEPTION", e); } suggestions = buildFinalResult(); } return suggestions; }