【问题标题】:Span class in opennlp is not workingopennlp 中的 Span 类不起作用
【发布时间】:2014-06-30 08:21:49
【问题描述】:

我是 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.*?那些是你的类,还是你在两个不同的地方有两个独立的依赖副本?您的项目设置方式有问题。
  • 谢谢你帮我解决了
  • 你提出了很多关于 OpenNLP 的问题,这表明你应该更加努力地理解库的工作原理,而不是随机提问......至少这是我的对情况的看法。

标签: java eclipse nlp opennlp


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我只更改了您的导入并且工作正常:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2019-09-08
      相关资源
      最近更新 更多