Java 类org.apache.lucene.search.spell.SpellChecker 实例源码

项目:sjk    文件:SpellCheckerServiceImpl.java   
@PostConstruct
public void reset() {
    String indexDir = appConfig.getAllSpellCheckerDir();
    try {
        Directory spellcheckDir = FSDirectory.open(new File(indexDir));
        if (!IndexReader.indexExists(spellcheckDir)) {
            logger.info("Please reset index firstly!");
            return;
        }
        SpellChecker newSpellChecker = new SpellChecker(spellcheckDir);
        newSpellChecker.setStringDistance(new JaroWinklerDistance());
        newSpellChecker.setAccuracy(0.7f);
        if (spellChecker == null) {
            spellChecker = newSpellChecker;
        } else {
            final Closeable preSpellChecker = spellChecker;
            spellChecker = newSpellChecker;
            IOUtils.closeQuietly(preSpellChecker);
        }
    } catch (Exception e) {
        logger.error("Exception", e);
    }
}
项目:Camel    文件:LuceneSuggestionStrategy.java   
@Override
public String[] suggestEndpointOptions(Set<String> names, String unknownOption) {
    // each option must be on a separate line in a String
    StringBuilder sb = new StringBuilder();
    for (String name : names) {
        sb.append(name);
        sb.append("\n");
    }
    StringReader reader = new StringReader(sb.toString());

    try {
        PlainTextDictionary words = new PlainTextDictionary(reader);

        // use in-memory lucene spell checker to make the suggestions
        RAMDirectory dir = new RAMDirectory();
        SpellChecker checker = new SpellChecker(dir);
        checker.indexDictionary(words, new IndexWriterConfig(new KeywordAnalyzer()), false);

        return checker.suggestSimilar(unknownOption, maxSuggestions);
    } catch (Exception e) {
        // ignore
    }

    return null;
}
项目:jspellchecker    文件:LuceneSpellCheckerServlet.java   
protected List<String> findMisspelledWords(Iterator<String> checkedWordsIterator,
                                           String lang) throws SpellCheckException {
    List<String> misspelledWordsList = new ArrayList<String>();
    SpellChecker checker = (SpellChecker) getChecker(lang);
    try {
        while (checkedWordsIterator.hasNext()) {
            String word = checkedWordsIterator.next();
            if (!word.equals("") && !checker.exist(word)) {
                misspelledWordsList.add(word);
            }
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to find misspelled words", e);
        throw new SpellCheckException("Failed to find misspelled words", e);
    }
    return misspelledWordsList;
}
项目:SciGraph    文件:VocabularyNeo4jImpl.java   
@Inject
public VocabularyNeo4jImpl(GraphDatabaseService graph,
    @Nullable @IndicatesNeo4jGraphLocation String neo4jLocation, CurieUtil curieUtil,
    NodeTransformer transformer) throws IOException {
  this.graph = graph;
  this.curieUtil = curieUtil;
  this.transformer = transformer;
  if (null != neo4jLocation) {
    Directory indexDirectory =
        FSDirectory.open((new File(new File(neo4jLocation), "index/lucene/node/node_auto_index"))
            .toPath());
    Directory spellDirectory =
        FSDirectory.open((new File(new File(neo4jLocation), "index/lucene/spellchecker"))
            .toPath());
    spellChecker = new SpellChecker(spellDirectory);
    try (IndexReader reader = DirectoryReader.open(indexDirectory)) {
      IndexWriterConfig config = new IndexWriterConfig(new KeywordAnalyzer());
      spellChecker.indexDictionary(new LuceneDictionary(reader, NodeProperties.LABEL
          + LuceneUtils.EXACT_SUFFIX), config, true);
    }
  } else {
    spellChecker = null;
  }
}
项目:t4f-data    文件:SpellCheckerExample.java   
public static void main(String[] args) throws IOException {

        if (args.length != 2) {
            LOGGER.info("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)) {
            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);
    }
项目:sjk    文件:SearchServiceImpl.java   
/**
 * 拼写错误的提示修正
 * 
 * @param keyword
 * @return
 */
@Override
public List<String> spellChecker(Integer typeId, String sword) {
    try {
        List<String> strList = new ArrayList<String>();
        SpellChecker sp = new SpellChecker(FSDirectory.getDirectory(typeId == appConfig.getGameTypeId() ? appConfig
                .getGameSpellIndexDir() : appConfig.getSoftSpellIndexDir()), new JaroWinklerDistance());
        String[] suggestions = sp.suggestSimilar(sword, appConfig.getSuggestNum());
        for (int i = 0; i < suggestions.length; i++)
            strList.add(suggestions[i]);
        return strList;
    } catch (IOException e) {
        logger.error("Exception", e);

        return null;
    }
}
项目:sjk    文件:SearchServiceImpl.java   
/**
 * 初始化SpellIndex
 * 
 * @throws IOException
 */
public void initSpellIndex(boolean isGame) throws IOException {
    FileOPHelper.del(isGame ? appConfig.getGameSpellIndexDir() : appConfig.getSoftSpellIndexDir());
    Directory spellIndexDir = FSDirectory.getDirectory(new File(isGame ? appConfig.getGameSpellIndexDir()
            : appConfig.getSoftSpellIndexDir()));
    SpellChecker spellChecker = new SpellChecker(spellIndexDir);
    spellChecker.indexDictionary(new PlainTextDictionary(new File(isGame ? appConfig.getGameSuggestDict()
            : appConfig.getSoftSuggestDict())));
    spellIndexDir.close();
}
项目:sjk    文件:SpellCheckerServiceImpl.java   
@PostConstruct
public void reset() {
    String indexDir = appConfig.getAllSpellCheckerDir();
    try {
        Directory spellcheckDir = FSDirectory.open(new File(indexDir));
        if (!IndexReader.indexExists(spellcheckDir)) {
            logger.info("Please reset index firstly!");
            return;
        }
        SpellChecker newSpellChecker = new SpellChecker(spellcheckDir);
        newSpellChecker.setStringDistance(new JaroWinklerDistance());
        newSpellChecker.setAccuracy(0.7f);
        if (spellChecker == null) {
            spellChecker = newSpellChecker;
        } else {
            final Closeable preSpellChecker = spellChecker;
            spellChecker = newSpellChecker;
            IOUtils.closeQuietly(preSpellChecker);
        }
    } catch (Exception e) {
        logger.error("Exception", e);
    }
}
项目:hmftools    文件:TreatmentCurator.java   
@NotNull
private static SpellChecker createIndexSpellchecker(@NotNull final Directory index) throws IOException {
    final Directory spellCheckerDirectory = new RAMDirectory();
    final IndexReader indexReader = DirectoryReader.open(index);
    final Analyzer analyzer = new SimpleAnalyzer();
    final IndexWriterConfig config = new IndexWriterConfig(analyzer);
    final Dictionary dictionary = new HighFrequencyDictionary(indexReader, DRUG_TERMS_FIELD, 0.0f);
    final SpellChecker spellChecker = new SpellChecker(spellCheckerDirectory);

    spellChecker.indexDictionary(dictionary, config, false);
    spellChecker.setAccuracy(SPELLCHECK_ACCURACY);
    return spellChecker;
}
项目:hmftools    文件:TreatmentCurator.java   
@NotNull
private static Analyzer spellcheckAnalyzer(@NotNull final SpellChecker spellChecker) {
    return new Analyzer() {
        @Override
        protected TokenStreamComponents createComponents(@NotNull final String field) {
            final Tokenizer source = new WhitespaceTokenizer();
            source.setReader(new StringReader(field));
            final SpellCheckerTokenFilter spellCheckFilter = new SpellCheckerTokenFilter(defaultTokenFilter(source), spellChecker);
            final TokenFilter concatenatingFilter = new ConcatenatingFilter(spellCheckFilter, ' ');
            return new TokenStreamComponents(source, concatenatingFilter);
        }
    };
}
项目:document-management-system    文件:IndexHelper.java   
protected void buildSpellCheckerIndex(SearchFactory searchFactory) {
    IndexReader reader = null;
    Directory dir = null;
    long _entr = System.currentTimeMillis();
    File spellCheckIndexDir = new File("lucene_index/spellcheck");
    log.info("Building SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath());
    ReaderProvider readerProvider = searchFactory.getReaderProvider();

    try {
        reader = readerProvider.openReader(searchFactory.getDirectoryProviders(NodeDocumentVersion.class)[0]);
        dir = FSDirectory.open(spellCheckIndexDir);
        SpellChecker spell = new SpellChecker(dir);
        spell.clearIndex();
        spell.indexDictionary(new LuceneDictionary(reader, NodeDocument.TEXT_FIELD));
        spell.close();
        dir.close();
        dir = null;
        long _exit = System.currentTimeMillis();
        log.info("Took {1} (ms) to build SpellChecker index in {0}",
                spellCheckIndexDir.getAbsolutePath(), String.valueOf((_exit - _entr)));
    } catch (Exception exc) {
        log.error("Failed to build spell checker index!", exc);
    } finally {
        if (dir != null) {
            try {
                dir.close();
            } catch (Exception zzz) {
            }
        }
        if (reader != null) {
            readerProvider.closeReader(reader);
        }
    }
}
项目:UMLS-Terminology-Server    文件:AtomClassSearchHandler.java   
@Override
public void setProperties(Properties p) throws Exception {

  // Initialize acronyms map
  if (p.containsKey("acronymsFile")) {
    final BufferedReader in = new BufferedReader(
        new FileReader(new File(p.getProperty("acronymsFile"))));
    String line;
    while ((line = in.readLine()) != null) {
      String[] tokens = FieldedStringTokenizer.split(line, "\t");
      if (!acronymExpansionMap.containsKey(tokens[0])) {
        acronymExpansionMap.put(tokens[0], new HashSet<String>(2));
      }
      acronymExpansionMap.get(tokens[0]).add(tokens[1]);
    }
    in.close();
  } else {
    throw new Exception("Required property acronymsFile not present.");
  }

  // Initialize spell checker
  if (p.containsKey("spellingFile") && p.containsKey("spellingIndex")) {
    // expect properties to have "spellingFile" and "spellingIndex"
    final File dir = new File(p.getProperty("spellingIndex"));
    final Directory directory = FSDirectory.open(dir);
    spellChecker =
        new SpellChecker(directory, new LuceneLevenshteinDistance());
    final IndexWriterConfig indexWriterConfig =
        new IndexWriterConfig(Version.LATEST, new WhitespaceAnalyzer());
    spellChecker.indexDictionary(
        new PlainTextDictionary(new File(p.getProperty("spellingFile"))),
        indexWriterConfig, false);

  } else {
    throw new Exception(
        "Required property spellingFile or spellingIndex not present.");
  }
}
项目:jspellchecker    文件:LuceneSpellCheckerServlet.java   
protected List<String> findSuggestions(String word, String lang, int maxSuggestions) throws SpellCheckException {
    SpellChecker checker = (SpellChecker) getChecker(lang);
    try {
        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);
    }
}
项目:search    文件:IndexBasedSpellCheckerTest.java   
@Test
public void testAlternateDistance() throws Exception {
  TestSpellChecker checker = new TestSpellChecker();
  NamedList spellchecker = new NamedList();
  spellchecker.add("classname", IndexBasedSpellChecker.class.getName());

  File indexDir = createTempDir();
  spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
  spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
  spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
  spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName());
  SolrCore core = h.getCore();
  String dictName = checker.init(spellchecker, core);
  assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME,
          dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
  RefCounted<SolrIndexSearcher> holder = core.getSearcher();
  SolrIndexSearcher searcher = holder.get();
  try {
  checker.build(core, searcher);
  SpellChecker sc = checker.getSpellChecker();
  assertTrue("sc is null and it shouldn't be", sc != null);
  StringDistance sd = sc.getStringDistance();
  assertTrue("sd is null and it shouldn't be", sd != null);
  assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance);
  } finally {
    holder.decref();
  }
}
项目:webdsl    文件:SearchSuggester.java   
public static synchronized void forceSpellCheckerRenewal(String indexPath){
    SpellChecker sp = spellCheckMap.get(indexPath);
    if(sp!=null) {
        try {
            sp.close();
        } catch (IOException e) {
            org.webdsl.logging.Logger.error("EXCEPTION",e);
        }
    }
    spellCheckMap.remove(indexPath);
}
项目:Lucee4    文件:LuceneSearchCollection.java   
private void indexSpellCheck(String id) throws SearchException  {
    if(!spellcheck) return;

    IndexReader reader=null;
    FSDirectory spellDir=null;

    Resource dir = _createSpellDirectory(id);
    try {
        File spellFile = FileWrapper.toFile(dir);
        spellDir = FSDirectory.getDirectory(spellFile);
        reader = _getReader(id,false);
        Dictionary dictionary = new LuceneDictionary(reader,"contents");

        SpellChecker spellChecker = new SpellChecker(spellDir);
        spellChecker.indexDictionary(dictionary);

    }
    catch(IOException ioe) {
        throw new SearchException(ioe);
    }
    finally {
        flushEL(reader);
        closeEL(reader);
    }
}
项目:magicspells    文件:SpellImpl.java   
/**
    * Constructor. Initializes directory for indexing and dictionary
    * 
    * @param props
    *            properties file Object
    * @throws IOException
    *             when dictionary or directory path is/are invalid or
    *             inaccessible
    */
   public SpellImpl(Properties props) throws IOException {
directory = FSDirectory.open(new File(props.getProperty(
    "index_directory", "res/index")));
dictionary = new PlainTextDictionary(new File(props.getProperty(
    "dictionary_path", "res/dict_en.txt")));
spellChecker = new SpellChecker(directory);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,
    analyzer);
spellChecker.clearIndex();
spellChecker.indexDictionary(dictionary, config, true);
spellChecker.setAccuracy(Float.parseFloat(props.getProperty(
    "default_accuracy", "0.75")));
setMaxSuggestions(Integer.parseInt(props.getProperty("max_suggestions",
    "5")));
   }
项目:olat    文件:Index.java   
private void createSpellCheckSearcher(boolean indexNewlyBuilt) {
    try {
        log.info("Create spell checker on new index ...");
        synchronized (createSpellCheckSearcherLock) {// o_clusterOK by:pb if service is only configured on one vm, which is recommended way
            closeSpellCheckSearcher();
            if (indexNewlyBuilt) {
                replaceSpellCheckFiles();
            }

            final File spellDictionaryFile = new File(searchModule.getSpellCheckerIndexPath());
            final Directory spellIndexDirectory = FSDirectory.open(spellDictionaryFile);
            if (!IndexReader.indexExists(spellIndexDirectory)) {
                log.error("SpellChecker index does not exist [" + spellDictionaryFile.getAbsolutePath() + "]");
                return;
            }
            spellChecker = new SpellChecker(spellIndexDirectory);
            spellChecker.setAccuracy(0.7f);
        }

        if (indexNewlyBuilt) {
            log.info("Cleanup old spell checker index files ...");
            cleanupSpellCheckFiles();
        }
    } catch (IOException ex) {
        log.error("SpellChecker couldn't be created.", ex);
    }

}
项目:NYBC    文件:IndexBasedSpellCheckerTest.java   
@Test
public void testAlternateDistance() throws Exception {
  TestSpellChecker checker = new TestSpellChecker();
  NamedList spellchecker = new NamedList();
  spellchecker.add("classname", IndexBasedSpellChecker.class.getName());

  File indexDir = new File(TEMP_DIR, "spellingIdx" + new Date().getTime());
  indexDir.mkdirs();
  spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
  spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
  spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
  spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName());
  SolrCore core = h.getCore();
  String dictName = checker.init(spellchecker, core);
  assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME,
          dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
  RefCounted<SolrIndexSearcher> holder = core.getSearcher();
  SolrIndexSearcher searcher = holder.get();
  try {
  checker.build(core, searcher);
  SpellChecker sc = checker.getSpellChecker();
  assertTrue("sc is null and it shouldn't be", sc != null);
  StringDistance sd = sc.getStringDistance();
  assertTrue("sd is null and it shouldn't be", sd != null);
  assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance);
  } finally {
    holder.decref();
  }
}
项目:search-core    文件:IndexBasedSpellCheckerTest.java   
@Test
public void testAlternateDistance() throws Exception {
  TestSpellChecker checker = new TestSpellChecker();
  NamedList spellchecker = new NamedList();
  spellchecker.add("classname", IndexBasedSpellChecker.class.getName());

  File indexDir = new File(TEMP_DIR, "spellingIdx" + new Date().getTime());
  indexDir.mkdirs();
  spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath());
  spellchecker.add(AbstractLuceneSpellChecker.FIELD, "title");
  spellchecker.add(AbstractLuceneSpellChecker.SPELLCHECKER_ARG_NAME, spellchecker);
  spellchecker.add(AbstractLuceneSpellChecker.STRING_DISTANCE, JaroWinklerDistance.class.getName());
  SolrCore core = h.getCore();
  String dictName = checker.init(spellchecker, core);
  assertTrue(dictName + " is not equal to " + SolrSpellChecker.DEFAULT_DICTIONARY_NAME,
          dictName.equals(SolrSpellChecker.DEFAULT_DICTIONARY_NAME) == true);
  RefCounted<SolrIndexSearcher> holder = core.getSearcher();
  SolrIndexSearcher searcher = holder.get();
  try {
  checker.build(core, searcher);
  SpellChecker sc = checker.getSpellChecker();
  assertTrue("sc is null and it shouldn't be", sc != null);
  StringDistance sd = sc.getStringDistance();
  assertTrue("sd is null and it shouldn't be", sd != null);
  assertTrue("sd is not an instance of " + JaroWinklerDistance.class.getName(), sd instanceof JaroWinklerDistance);
  } finally {
    holder.decref();
  }
}
项目:opensearchserver    文件:SpellCheckCacheItem.java   
@Override
protected void populate(Timer timer) throws Exception {
    LuceneDictionary dict = reader.getLuceneDirectionary(field);
    spellChecker = new SpellChecker(new RAMDirectory());
    spellChecker.indexDictionary(dict, new IndexWriterConfig(
            Version.LUCENE_36, null), true);
}
项目:opensearchserver    文件:SpellCheckCache.java   
public SpellChecker get(ReaderLocal reader, String fieldName)
        throws SearchLibException {
    try {
        return getAndJoin(new SpellCheckCacheItem(reader, fieldName), null)
                .getSpellChecker();
    } catch (Exception e) {
        throw new SearchLibException(e);
    }
}
项目:tfidf-topology    文件:TermFilter.java   
public void prepare(Map conf, TridentOperationContext context){
    super.prepare(conf, context);
    File dir = new File(System.getProperty("user.home") + "/dictionaries");
    Directory directory;
    try {
        directory = FSDirectory.open(dir);
        spellchecker = new SpellChecker(directory);
        StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        URL dictionaryFile = TermFilter.class.getResource("/dictionaries/fulldictionary00.txt");
        spellchecker.indexDictionary(new PlainTextDictionary(new File(dictionaryFile.toURI())), config, true);
    } catch (Exception e) {
        LOG.error(e.toString());
    }
}
项目:t4f-data    文件:CreateSpellCheckerIndex.java   
public static void main(String[] args) throws IOException {

    if (args.length != 3) {
      LOGGER.info("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir IndexDir IndexField");
      System.exit(1);
    }

    String spellCheckDir = args[0];
    String indexDir = args[1];
    String indexField = args[2];

    LOGGER.info("Now build SpellChecker index...");
    Directory dir = FSDirectory.open(new File(spellCheckDir));
    SpellChecker spell = new SpellChecker(dir);     //#A
    long startTime = System.currentTimeMillis();

    Directory dir2 = FSDirectory.open(new File(indexDir));
    IndexReader r = DirectoryReader.open(dir2);     //#B
    try {
      spell.indexDictionary(new LuceneDictionary(r, indexField));  //#C
    } finally {
      r.close();
    }
    dir.close();
    dir2.close();
    long endTime = System.currentTimeMillis();
    LOGGER.info("  took " + (endTime-startTime) + " milliseconds");
  }
项目:hmftools    文件:SpellCheckerTokenFilter.java   
SpellCheckerTokenFilter(@NotNull final TokenStream tokenStream, @NotNull final SpellChecker spellChecker) {
    super(tokenStream);
    this.spellChecker = spellChecker;
}
项目:document-management-system    文件:IndexHelper.java   
public void updateSpellCheckerIndex(NodeDocumentVersion nDocVer) {
    log.info("Observed Wine added/updated event for {1} from Thread {0}",
            Thread.currentThread().getName(), String.valueOf(nDocVer));
    String text = (nDocVer != null) ? nDocVer.getText() : null;

    if (text != null) {
        Dictionary dictionary = null;

        try {
            FullTextEntityManager ftEm = (FullTextEntityManager) entityManager;
            SearchFactory searchFactory = ftEm.getSearchFactory();
            dictionary = new SetDictionary(text, searchFactory.getAnalyzer("wine_en"));
        } catch (IOException ioExc) {
            log.error("Failed to analyze dictionary text {0} from Wine {1} to update spell checker due to: {2}" +
                    text + nDocVer.getUuid() + ioExc.toString());
        }

        if (dictionary != null) {
            Directory dir = null;
            // only allow one thread to update the index at a time ...
            // the Dictionary is pre-computed, so it should happen quickly
            // ...
            // this synchronized approach only works because this component
            // is application-scoped
            synchronized (this) {
                try {
                    dir = FSDirectory.open(new File("lucene_index/spellcheck"));
                    SpellChecker spell = new SpellChecker(dir);
                    spell.indexDictionary(dictionary);
                    spell.close();
                    log.info("Successfully updated the spell checker index after Document added/updated.");
                } catch (Exception exc) {
                    log.error("Failed to update the spell checker index!", exc);
                } finally {
                    if (dir != null) {
                        try {
                            dir.close();
                        } catch (Exception zzz) {
                        }
                    }
                }
            }
        }
    }
}
项目:search    文件:AbstractLuceneSpellChecker.java   
public SpellChecker getSpellChecker() {
  return spellChecker;
}
项目:search    文件:IndexBasedSpellCheckerTest.java   
@Override
public SpellChecker getSpellChecker(){
  return spellChecker;
}
项目:webdsl    文件:SearchSuggester.java   
@SuppressWarnings("deprecation")
public static ArrayList<String> findSpellSuggestionsForField(Class<?> entityClass, String baseDir,
        String suggestedField, int maxSuggestionCount, float accuracy, boolean morePopular,
        Analyzer analyzer, String toSuggestOn) {

    if (toSuggestOn == null || toSuggestOn.isEmpty())
        return new ArrayList<String>();

    SpellChecker spellChecker = null;
    IndexReader fieldIR = null;
    boolean hasSuggestions = false;

    String indexPath = baseDir+suggestedField;
    try {
        spellChecker = getSpellChecker(indexPath);

        spellChecker.setAccuracy(accuracy);

        TokenStream tokenStream = analyzer.tokenStream(suggestedField, new StringReader(
                toSuggestOn));
        CharTermAttributeImpl ta = (CharTermAttributeImpl) tokenStream
                .addAttribute(CharTermAttribute.class);

        ArrayList<String[]> allSuggestions = new ArrayList<String[]>();
        String word;
        String[] suggestions;
        while (tokenStream.incrementToken()) {
            word = ta.term();
            suggestions = null;
            if (!morePopular) {
                suggestions = spellChecker.suggestSimilar(word, maxSuggestionCount);
            } else {
                if (fieldIR == null)
                    fieldIR = getIndexReader(entityClass);
                suggestions = spellChecker.suggestSimilar(word, maxSuggestionCount, fieldIR,
                        suggestedField, true);
            }

            if (suggestions == null || suggestions.length == 0)
                suggestions = new String[] { word };
            else
                hasSuggestions = true;

            allSuggestions.add(suggestions);
        }

        if (!hasSuggestions)
            // if no suggestions were found, return empty list
            return new ArrayList<String>();
        else
            return formSuggestions(maxSuggestionCount, allSuggestions);

    } catch (Exception e) {
        org.webdsl.logging.Logger.error("EXCEPTION",e);
        //if something goes wrong, close and remove current SpellChecker instance, so it gets renewed
        try {
            spellChecker.close();
        } catch (IOException e2) {
            org.webdsl.logging.Logger.error("EXCEPTION",e2);
        }
        spellCheckMap.remove(indexPath);
    }
    finally {
        searchfactory.getReaderProvider().closeReader(fieldIR);
    }
    return new ArrayList<String>();
}
项目:Lucee4    文件:LuceneSearchCollection.java   
private SpellChecker getSpellChecker(String id) throws IOException {
    FSDirectory siDir = FSDirectory.getDirectory(FileWrapper.toFile(_getSpellDirectory(id)));
    SpellChecker spellChecker = new SpellChecker(siDir);
    return spellChecker;
}
项目:olat    文件:SearchSpellChecker.java   
/**
 * Creates a new spell-check index based on search-index
 */
public void createSpellIndex() {
    if (isSpellCheckEnabled) {
        IndexReader indexReader = null;
        try {
            log.info("Start generating Spell-Index...");
            long startSpellIndexTime = 0;
            if (log.isDebugEnabled()) {
                startSpellIndexTime = System.currentTimeMillis();
            }
            final Directory indexDir = FSDirectory.open(new File(indexPath));
            indexReader = IndexReader.open(indexDir);
            // 1. Create content spellIndex
            final File spellDictionaryFile = new File(spellDictionaryPath);
            final Directory contentSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + CONTENT_PATH));// true
            final SpellChecker contentSpellChecker = new SpellChecker(contentSpellIndexDirectory);
            final Dictionary contentDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.CONTENT_FIELD_NAME);
            contentSpellChecker.indexDictionary(contentDictionary);
            // 2. Create title spellIndex
            final Directory titleSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + TITLE_PATH));// true
            final SpellChecker titleSpellChecker = new SpellChecker(titleSpellIndexDirectory);
            final Dictionary titleDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.TITLE_FIELD_NAME);
            titleSpellChecker.indexDictionary(titleDictionary);
            // 3. Create description spellIndex
            final Directory descriptionSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + DESCRIPTION_PATH));// true
            final SpellChecker descriptionSpellChecker = new SpellChecker(descriptionSpellIndexDirectory);
            final Dictionary descriptionDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.DESCRIPTION_FIELD_NAME);
            descriptionSpellChecker.indexDictionary(descriptionDictionary);
            // 4. Create author spellIndex
            final Directory authorSpellIndexDirectory = FSDirectory.open(new File(spellDictionaryPath + AUTHOR_PATH));// true
            final SpellChecker authorSpellChecker = new SpellChecker(authorSpellIndexDirectory);
            final Dictionary authorDictionary = new LuceneDictionary(indexReader, AbstractOlatDocument.AUTHOR_FIELD_NAME);
            authorSpellChecker.indexDictionary(authorDictionary);

            // Merge all part spell indexes (content,title etc.) to one common spell index
            final Directory spellIndexDirectory = FSDirectory.open(spellDictionaryFile);// true
            final IndexWriter merger = new IndexWriter(spellIndexDirectory, new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.UNLIMITED);
            final Directory[] directories = { contentSpellIndexDirectory, titleSpellIndexDirectory, descriptionSpellIndexDirectory, authorSpellIndexDirectory };
            merger.addIndexesNoOptimize(directories);
            merger.optimize();
            merger.close();
            spellChecker = new SpellChecker(spellIndexDirectory);
            spellChecker.setAccuracy(0.7f);
            if (log.isDebugEnabled()) {
                log.debug("SpellIndex created in " + (System.currentTimeMillis() - startSpellIndexTime) + "ms");
            }
            log.info("New generated Spell-Index ready to use.");
        } catch (final IOException ioEx) {
            log.warn("Can not create SpellIndex", ioEx);
        } finally {
            if (indexReader != null) {
                try {
                    indexReader.close();
                } catch (final IOException e) {
                    log.warn("Can not close indexReader properly", e);
                }
            }
        }
    }
}
项目:NYBC    文件:AbstractLuceneSpellChecker.java   
public SpellChecker getSpellChecker() {
  return spellChecker;
}
项目:NYBC    文件:IndexBasedSpellCheckerTest.java   
@Override
public SpellChecker getSpellChecker(){
  return spellChecker;
}
项目:search-core    文件:AbstractLuceneSpellChecker.java   
public SpellChecker getSpellChecker() {
  return spellChecker;
}
项目:search-core    文件:IndexBasedSpellCheckerTest.java   
@Override
public SpellChecker getSpellChecker(){
  return spellChecker;
}
项目:CophiAlignment    文件:SimilarWordFinder.java   
public void init() throws Exception{
    indexReader=IndexReader.open(spellIndexDirectory);
    spellChecker = new SpellChecker(spellIndexDirectory);
}
项目:CophiAlignment    文件:LuceneIndexMaker.java   
public void init() throws Exception{
    NIOFSDirectory spellIndexDirectory =new NIOFSDirectory(dirDict, NoLockFactory.getNoLockFactory());
    spellchecker = new SpellChecker(spellIndexDirectory);
    iwc=new IndexWriterConfig(Version.LUCENE_36,new WhitespaceAnalyzer(Version.LUCENE_36));

}
项目:read-open-source-code    文件:AbstractLuceneSpellChecker.java   
public SpellChecker getSpellChecker() {
  return spellChecker;
}
项目:read-open-source-code    文件:AbstractLuceneSpellChecker.java   
public SpellChecker getSpellChecker() {
  return spellChecker;
}
项目:opensearchserver    文件:SpellCheck.java   
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());
    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, ' ');
}