搜索引擎智能提示的实现-基于Lucene拼音检查库
2015-02-01 21:47
531 查看
package lia.tools; /** * Copyright Manning Publications Co. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific lan */ import java.io.IOException; import java.io.File; import org.apache.lucene.search.spell.SpellChecker; import org.apache.lucene.search.spell.LuceneDictionary; import org.apache.lucene.index.IndexReader; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.Directory; //创建拼音检查库 // From chapter 8 public class CreateSpellCheckerIndex { public static void main(String[] args) throws IOException { if (args.length != 3) { System.out.println("Usage: java lia.tools.SpellCheckerTest SpellCheckerIndexDir IndexDir IndexField"); System.exit(1); } String spellCheckDir = args[0]; String indexDir = args[1]; String indexField = args[2]; System.out.println("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 = IndexReader.open(dir2); //#B try { spell.indexDictionary(new LuceneDictionary(r, indexField)); //#C } finally { r.close(); } dir.close(); dir2.close(); long endTime = System.currentTimeMillis(); System.out.println(" took " + (endTime-startTime) + " milliseconds"); } } /* #A Create SpellChecker on its directory 创建检查库 #B Open IndexReader containing words to add to spell dictionary 添加到拼音检查库 #C Add all words from the specified fields into the spell checker index 添加索引指定域单词到拼音检查库 */
package lia.tools; /** * Copyright Manning Publications Co. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific lan */ import java.io.IOException; import java.io.File; import org.apache.lucene.search.spell.SpellChecker; import org.apache.lucene.search.spell.JaroWinklerDistance; import org.apache.lucene.search.spell.LevensteinDistance; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.Directory; import org.apache.lucene.index.IndexReader; //搜索关键字拼音智能提示的实现,通过拼音检查库 // From chapter 8 public class SpellCheckerExample { 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); } } /* #A Create SpellCheck from existing spell check index #B Sets the string distance metric used to rank the suggestions #C Generate respelled candidates */
相关文章推荐
- 搜索引擎关键字智能提示的一种实现
- 搜索引擎设计实用教程(2)-以百度为例 之二:Spelling Checker拼写检查错误提示、拼音提示功能
- [原创]一种基于Python爬虫和Lucene检索的垂直搜索引擎的实现方法介绍
- 搜索引擎之全文搜索算法功能实现(基于Lucene)
- 百度搜狗等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)
- 搜索引擎关键字智能提示的一种实现
- 搜索引擎关键字智能提示的一种实现
- 基于Scrapy和Lucene实现一个深圳大学校内搜索引擎
- 各大搜索引擎智能提示API (JSONP跨域实现自动补全搜索建议)
- 百度搜狗等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)
- 美团搜索-搜索引擎关键字智能提示的一种实现[转]
- 基于 jQuery & CSS3 实现智能提示输入框光标位置
- 搜索引擎关键字智能提示的一种实现
- 搜索引擎关键字智能提示的一种实现
- 各大搜索引擎智能提示API(jsonp实现跨域自动补全建议)
- 搜索引擎关键字智能提示的一种实现
- 基于jquery实现智能提示控件intellSeach.js
- 基于jquery实现智能提示控件intellSeach.js
- 百度等搜索引擎智能提示(JSONP跨域实现自动补全搜索建议)
- 毕业设计——基于lucene的智能搜索引擎