opennlp中的Span类不起作用

我是java opennlp的新手,我正在尝试实现一个程序,从文件中提取城市名称,但我首先在字符串上测试我的代码,我得到一些错误,代码是

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import main.java.opennlp.tools.namefind.NameFinderME; import main.java.opennlp.tools.namefind.TokenNameFinderModel; import main.java.opennlp.tools.util.InvalidFormatException; import main.java.opennlp.tools.util.Span; import opennlp.tools.tokenize.Tokenizer; import opennlp.tools.tokenize.TokenizerME; import opennlp.tools.tokenize.TokenizerModel; import opennlp.tools.tokenize.SimpleTokenizer; import opennlp.tools.sentdetect.SentenceDetectorME; import opennlp.tools.sentdetect.SentenceModel; import org.xml.sax.SAXException; public class CityFinder { public String Tokens[]; public static void main(String[] args) throws IOException, SAXException { CityFinder toi = new CityFinder(); String cnt; cnt="John is planning to specialize in Electrical Engineering in UC Berkley and pursue a career with IBM."; toi.tokenization(cnt); String cities = toi.namefind(toi.Tokens); String org = toi.orgfind(toi.Tokens); System.out.println("City name is : "+cities); System.out.println("organization name is: "+org); } public String namefind(String cnt[]) { InputStream is; TokenNameFinderModel tnf; NameFinderME nf; String sd = ""; try { is = new FileInputStream("en-ner-location.bin"); tnf = new TokenNameFinderModel(is); nf = new NameFinderME(tnf); Span sp[] = nf.find(cnt); // <-- Here is the Error StringBuilder fd = new StringBuilder(); int l = a.length; for (int j = 0; j < l; j++) { fd = fd.append(a[j] + "\n"); } sd = fd.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sd; } public String orgfind(String cnt[]) { InputStream is; TokenNameFinderModel tnf; NameFinderME nf; String sd = ""; try { is = new FileInputStream("en-ner-organization.bin"); tnf = new TokenNameFinderModel(is); nf = new NameFinderME(tnf); Span sp[] = nf.find(cnt); // <-- Here is the Error String a[] = Span.spansToStrings(sp, cnt); StringBuilder fd = new StringBuilder(); int l = a.length; for (int j = 0; j < l; j++) { fd = fd.append(a[j] + "\n"); } sd = fd.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sd; } public void tokenization(String tokens) { InputStream is; TokenizerModel tm; try { is = new FileInputStream("en-token.bin"); tm = new TokenizerModel(is); Tokenizer tz = new TokenizerME(tm); Tokens = tz.tokenize(tokens); // System.out.println(Tokens[1]); } catch (IOException e) { e.printStackTrace(); } } } 

我有以下行的错误

 Span sp[] = nf.find(cnt); 

错误是

 Type mismatch: cannot convert from opennlp.tools.util.Span[] to main.java.opennlp.tools.util.Span[] 

我不知道如何在两个地方解决它

有什么建议….?? 提前致谢

为什么要导入main.java.opennlp.* ? 那些是您的课程,还是在两个不同的地方有两个独立的依赖副本? 项目设置方式有问题。

我只改变你的import并且工作正常:

 import opennlp.tools.namefind.NameFinderME; // I've import opennlp.tools.namefind.TokenNameFinderModel; // changed import opennlp.tools.util.Span; // only these import opennlp.tools.util.InvalidFormatException; // lines 

OpenNLP版本是1.5.3