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);
  }
Example #2
0
  /** Generate a spelling suggestion for the definitions stored in defs */
  public void createSpellingSuggestions() {
    IndexReader indexReader = null;
    SpellChecker checker;

    try {
      log.info("Generating spelling suggestion index ... ");
      indexReader = DirectoryReader.open(indexDirectory);
      checker = new SpellChecker(spellDirectory);
      // TODO below seems only to index "defs" , possible bug ?
      Analyzer analyzer = AnalyzerGuru.getAnalyzer();
      IndexWriterConfig iwc = new IndexWriterConfig(SearchEngine.LUCENE_VERSION, analyzer);
      iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
      checker.indexDictionary(new LuceneDictionary(indexReader, QueryBuilder.DEFS), iwc, false);
      log.info("done");
    } catch (IOException e) {
      log.log(Level.SEVERE, "ERROR: Generating spelling: {0}", e);
    } finally {
      if (indexReader != null) {
        try {
          indexReader.close();
        } catch (IOException e) {
          log.log(Level.WARNING, "An error occured while closing reader", e);
        }
      }
      if (spellDirectory != null) {
        spellDirectory.close();
      }
    }
  }
  /**
   * check the existence of the given word in the index
   *
   * @param indexPath index path's
   * @param word String to check
   * @return
   * @throws IOException
   */
  private boolean checkExistingWord(String indexPath, String word) throws IOException {

    File file = new File(indexPath);
    FSDirectory directory = FSDirectory.getDirectory(file);
    SpellChecker spellChecker = new SpellChecker(directory);
    return spellChecker.exist(word);
  }
  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;
  }
Example #5
0
  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();
    }
  }