public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println( "Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir wordToRespell"); System.exit(1); } String spellCheckDir = args[0]; String wordToRespell = args[1]; Directory dir = FSDirectory.open(new File(spellCheckDir)); if (!IndexReader.indexExists(dir)) { System.out.println( "\nERROR: No spellchecker index at path \"" + spellCheckDir + "\"; please run CreateSpellCheckerIndex first\n"); System.exit(1); } SpellChecker spell = new SpellChecker(dir); // #A spell.setStringDistance(new LevensteinDistance()); // #B // spell.setStringDistance(new JaroWinklerDistance()); String[] suggestions = spell.suggestSimilar(wordToRespell, 5); // #C System.out.println(suggestions.length + " suggestions for '" + wordToRespell + "':"); for (String suggestion : suggestions) System.out.println(" " + suggestion); }
public ArrayList<String> didYouMean(String queryString) { ArrayList<String> similarWords = new ArrayList<String>(); String similarWordsConcat = ""; String listSuggest[] = null; try { spellDir = FSDirectory.open(new File(SearcherConfiguration.getDictionaryPath())); spellChecker = new SpellChecker(spellDir); spellChecker.setAccuracy(0.8f); /* * se query è composta da più keywords */ if (queryString.contains(" ")) { boolean found = false; String[] parts = queryString.split(" "); for (int i = 0; i < parts.length; i++) { listSuggest = spellChecker.suggestSimilar(parts[i], 10); if (listSuggest.length > 0) { found = true; similarWordsConcat += (listSuggest[listSuggest.length - 1]); similarWordsConcat += " "; } } if (found) Collections.addAll(similarWords, similarWordsConcat); } else { listSuggest = spellChecker.suggestSimilar(queryString, 10); int last = listSuggest.length - 1; if (listSuggest.length > 1) { if ((listSuggest[last]).equals(queryString)) last--; similarWords.add(listSuggest[last]); } } spellChecker.close(); } catch (IOException e) { e.printStackTrace(); } return similarWords; }
public void getSuggestions(String wordToRespell) { try { Directory indexDir = FSDirectory.open(Paths.get(SearchUtils.SPELL_INDEX_DIR)); // IndexReader indexReader = DirectoryReader.open(indexDir) ; SpellChecker spell = new SpellChecker(indexDir); // #A spell.setStringDistance(new LevensteinDistance()); // #B spell.setStringDistance(new JaroWinklerDistance()); String[] suggestions = spell.suggestSimilar(wordToRespell, 5); // #C System.out.println(suggestions.length + " suggestions for '" + wordToRespell + "':"); for (String suggestion : suggestions) System.out.println(" " + suggestion); } catch (Exception e) { e.printStackTrace(); } }