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

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

Introduction

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

Prototype

public String[] suggestSimilar(String word, int numSug) throws IOException 

Source Link

Document

Suggest similar words.

Usage

From source file:org.codesearch.searcher.server.util.STAlternativeSuggestor.java

License:Open Source License

/**
 * Returns a list of autocomplete suggestions.
 * @param queryString/*from   w  ww  .ja  va 2s  .c  o  m*/
 * @return String[] of completion suggestions
 * @throws IOException
 */
public List<String> suggest(String queryString) throws IOException {
    SpellChecker spellChecker = new SpellChecker(spellIndexDirectory);
    String[] spresult = spellChecker.suggestSimilar(queryString, 10);
    if (spresult.length == 0) {
        return null;
    }
    LinkedList<String> suggestions = new LinkedList<String>();
    suggestions.addAll(Arrays.asList(spresult));

    return suggestions;
}

From source file:org.olat.search.service.spell.CheckCallable.java

License:Apache License

@Override
public Set<String> call() throws Exception {
    try {/* w  w w.  j a v  a  2  s.c om*/
        SpellChecker spellChecker = spellCheckerService.getSpellChecker();
        if (spellChecker != null) {
            String[] words = spellChecker.suggestSimilar(query, 5);
            // Remove dublicate 
            Set<String> filteredList = new TreeSet<String>();
            for (String word : words) {
                filteredList.add(word);
            }
            return filteredList;
        }
    } catch (IOException e) {
        log.warn("Can not spell check", e);
    }
    return new HashSet<String>();
}

From source file:org.silverpeas.core.index.search.model.DidYouMeanSearcher.java

License:Open Source License

/**
 * @param queryDescription/*  w w w .j a v a  2  s.  com*/
 * @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.core.index.search.model.DidYouMeanSearcher.java

License:Open Source License

private String[] buildFinalResult() throws IOException {
    StringBuilder currentSentence = new StringBuilder();
    StringTokenizer tokens = new StringTokenizer(query);
    int countTokens = tokens.countTokens();
    // building one sentence
    for (SpellChecker spellCheck : spellCheckers) {
        String currentToken = tokens.nextToken();
        String[] suggestWords = spellCheck.suggestSimilar(currentToken, 1);
        // quote and boolean operator managing
        // actions for suggested word
        if (!ArrayUtil.isEmpty(suggestWords)) {
            // beginning
            getPrefixOperator(currentSentence, currentToken, false);
            // word
            currentSentence.append(suggestWords[0]);
            // end
            getSuffixOperator(currentSentence, currentToken, false);
        } else if (countTokens > 1) {
            // actions if hasn't suggested word
            // begin
            getPrefixOperator(currentSentence, currentToken, true);
            // word
            currentSentence.append(currentToken);
            // end
            getSuffixOperator(currentSentence, currentToken, true);
        }/*  www  .j  av  a 2  s .com*/

    }

    return new String[] {
            currentSentence.toString().replaceAll("\\+", "").replaceAll("-", "").replaceAll("  ", " ").trim() };

}

From source file:org.silverpeas.search.searchEngine.model.DidYouMeanSearcher.java

License:Open Source License

/**
 * @param queryDescription//  w w w. j a  v  a  2s  .  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;
}

From source file:org.tinymce.spellchecker.LuceneSpellCheckerServlet.java

License:Open Source License

protected List<String> findSuggestions(String word, String lang, int maxSuggestions)
        throws SpellCheckException {
    SpellChecker checker = (SpellChecker) getChecker(lang);
    try {/*w  ww.  j a va  2 s . co m*/
        String[] suggestions = checker.suggestSimilar(word, maxSuggestions);
        return Arrays.asList(suggestions);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to find suggestions", e);
        throw new SpellCheckException("Failed to find suggestions", e);
    }
}

From source file:org.watermint.sourcecolon.org.opensolaris.opengrok.web.SearchHelper.java

License:Open Source License

private static void getSuggestion(String term, SpellChecker checker, List<String> result) throws IOException {
    if (term == null) {
        return;//  w w  w . j  ava2 s . c o m
    }
    String[] toks = TABSPACE.split(term, 0);
    for (String tok : toks) {
        if (tok.length() <= 3) {
            continue;
        }
        result.addAll(Arrays.asList(checker.suggestSimilar(tok.toLowerCase(), 5)));
    }
}

From source file:prman.model.SpellCheckManager.java

License:Open Source License

public String[] suggestSimilar(String word, Locale loc, int max) {
    SpellChecker sp = getSpellChecker(loc);
    String[] toReturn;//from  ww  w  .  ja va2s. c o m
    try {
        if (sp != null)
            toReturn = sp.suggestSimilar(word, max);
        else
            toReturn = new String[0];
    } catch (IOException _ioe) {
        Logger.getLogger(getClass().getName()).log(Level.WARNING, "Spell checker error", _ioe);
        toReturn = new String[0];
    }

    return toReturn;
}