Lucene的spellchecker用于纠错的问题
import java.io.File;import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.lucene.search.spell.PlainTextDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.Directory;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class check extends ActionSupport{
private String searching;
private String[] suggestions;
public String getSearching() {
return searching;
}
public void setSearching(String searching) {
this.searching = searching;
}
public String[] getSuggestions() {
return suggestions;
}
public void setSuggestions(String[] suggestions) {
this.suggestions = suggestions;
}
public String execute() throws IOException{
Directory spellIndexDirectory = null;
//实例化拼写检查器
SpellChecker sp = new SpellChecker(spellIndexDirectory);
//对词典进行索引
sp.indexDictionary(new PlainTextDictionary(new File("dictionary.txt")));
//“错误”的搜索
String search = searching;
//建议个数
int suggestionNumber = 1;
//获取建议的关键字
suggestions = sp.suggestSimilar(search, suggestionNumber);
System.out.println(suggestions[0]);
return SUCCESS;
}
}
从前台得到searching的内容,dictionary.txt已经建好。运行时抛出异常如下
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
File: org/apache/lucene/index/SegmentInfos.java
Line number: 151
--------------------------------------------------------------------------------
Stacktraces
java.lang.NullPointerException
org.apache.lucene.index.SegmentInfos.getCurrentSegmentGeneration(SegmentInfos.java:151)
org.apache.lucene.index.IndexReader.indexExists(IndexReader.java:626)
org.apache.lucene.search.spell.SpellChecker.setSpellIndex(SpellChecker.java:141)
org.apache.lucene.search.spell.SpellChecker.(SpellChecker.java:110)
org.apache.lucene.search.spell.SpellChecker.(SpellChecker.java:124)
check.execute(check.java:39)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
...
我感觉就是 Directory spellIndexDirectory = null; 这里写错了,可是该怎么改呢,我看了API没看懂,问题如下
1.这个Directory是一个索引么
2.如果是一个索引,那他已经建好了还是没有被创建
3.如何创建他
4.如果不是我想的这样,那到底是什么地方错了,如何解决呢
先谢谢大家了 --------------------编程问答-------------------- 先获取在赋值给directory,看答案 --------------------编程问答-------------------- ls写的太简洁了,木有看懂啊,求助ing --------------------编程问答-------------------- 继续顶,顶啊顶 --------------------编程问答-------------------- 空指针啊也就是说某一个对象是null然后调用了他的方法之类的打个断电不可以吗? --------------------编程问答-------------------- sp.indexDictionary(new PlainTextDictionary(new File("dictionary.txt")));
很明显这是错误的给你举个例子吧
String s=new String(null);
s.indexOf();
你可以试一下,个人见解 --------------------编程问答-------------------- spellchecker.indexDictionary(new PlainTextDictionary(new File("myfile.txt")));
上面是api里的例子,我写的没错,不是这出的错误 --------------------编程问答-------------------- Directory spellIndexDirectory = null;
//实例化拼写检查器
SpellChecker sp = new SpellChecker(spellIndexDirectory);
spellIndexDirectory本来就是null呀,给个值就可以了
//创建词典
Directory directory = FSDirectory.open(new File("E:/index2"));
//实例化拼写检查器
final SpellChecker sp = new SpellChecker(directory);
补充:Java , Web 开发