Example usage for org.apache.lucene.search.spell SpellChecker setStringDistance

List of usage examples for org.apache.lucene.search.spell SpellChecker setStringDistance

Introduction

In this page you can find the example usage for org.apache.lucene.search.spell SpellChecker setStringDistance.

Prototype

public void setStringDistance(StringDistance sd) 

Source Link

Document

Sets the StringDistance implementation for this SpellChecker instance.

Usage

From source file:aos.lucene.tools.SpellCheckerExample.java

License:Apache License

public static void main(String[] args) throws IOException {

    if (args.length != 2) {
        LOGGER.info("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir wordToRespell");
        System.exit(1);/*  www  . ja va2  s  . c  o  m*/
    }

    String spellCheckDir = args[0];
    String wordToRespell = args[1];

    Directory dir = FSDirectory.open(new File(spellCheckDir));
    if (!IndexReader.indexExists(dir)) {
        LOGGER.info("\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
    LOGGER.info(suggestions.length + " suggestions for '" + wordToRespell + "':");
    for (String suggestion : suggestions)
        LOGGER.info("  " + suggestion);
}

From source file:com.jaeksoft.searchlib.spellcheck.SpellCheck.java

License:Open Source License

public SpellCheck(ReaderLocal reader, SpellCheckRequest request, SpellCheckField spellCheckField)
        throws ParseException, SyntaxError, IOException, SearchLibException {
    fieldName = spellCheckField.getName();
    SpellChecker spellchecker = reader.getSpellChecker(fieldName);
    Set<String> wordSet = new LinkedHashSet<String>();

    Set<Term> set = request.getTermSet(spellCheckField.getName());
    for (Term term : set)
        if (term.field().equals(fieldName))
            wordSet.add(term.text());// www .  ja  v  a  2  s  . co  m
    int suggestionNumber = spellCheckField.getSuggestionNumber();
    float minScore = spellCheckField.getMinScore();
    synchronized (spellchecker) {
        spellchecker.setAccuracy(minScore);
        spellchecker.setStringDistance(spellCheckField.getStringDistance().getNewInstance());
        spellCheckItems = new ArrayList<SpellCheckItem>();
        for (String word : wordSet) {
            String[] suggestions = spellchecker.suggestSimilar(word, suggestionNumber);
            int s = 1;
            if (suggestions != null)
                s += suggestions.length;
            SuggestionItem[] suggestionItems = new SuggestionItem[s];
            int i = 0;
            suggestionItems[i++] = new SuggestionItem(word);
            if (suggestions != null) {
                for (String suggestion : suggestions)
                    suggestionItems[i++] = new SuggestionItem(suggestion);
                spellCheckItems.add(new SpellCheckItem(word, suggestionItems));
            }
        }
    }
    List<String> highers = new ArrayList<String>(spellCheckItems.size());
    for (SpellCheckItem spellcheckItem : spellCheckItems) {
        spellcheckItem.computeFrequency(reader, fieldName);
        String higher = spellcheckItem.getHigher();
        if (higher != null)
            highers.add(higher);
    }
    suggestion = StringUtils.join(highers, ' ');
}

From source file:com.leavesfly.lia.tool.SpellCheckerExample.java

License:Apache License

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);//from w  w w .  j a  v  a2s . c o  m
    }

    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);
}

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/*from w  w  w.  ja  va2 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:fr.mael.microrss.dao.impl.GenericDaoImpl.java

License:Open Source License

/**
 * @see fr.mael.jmusic.dao.GenericDao#getSuggestions(java.lang.String)
 *///from   www  .j  ava2s. c  om
@Override
public String[] getSuggestions(String query) throws IOException {
    Directory directory = FSDirectory
            .open(new File(configuration.getIndexDir() + "/spell_" + getPersistentClass().getName()));
    SpellChecker spell = new SpellChecker(directory);
    spell.setStringDistance(new LevensteinDistance());
    spell.setAccuracy(configuration.getSuggestionAccuracy());
    return spell.suggestSimilar(query, configuration.getSuggestionNumber());
}