【问题标题】:opennlp Span class in eclipse java [duplicate]eclipse java中的opennlp Span类[重复]
【发布时间】:2014-05-13 14:59:13
【问题描述】:

我是 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);
        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);
        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 verialble 时遇到的错误 跨度 sp[] = nf.find(cnt); 错误是类型不匹配:无法从 opennlp.tools.util.Span[] 转换为 main.java.opennlp.tools.util.Span[] 我不知道如何在这两个位置解决它

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

【问题讨论】:

    标签: java eclipse nlp opennlp


    【解决方案1】:

    您应该从 opennlp.tools 包中的包中导入所有类。

     import opennlp.tools.namefind.NameFinderME;
     import opennlp.tools.namefind.TokenNameFinderModel;
     import opennlp.tools.util.InvalidFormatException;
     import opennlp.tools.util.Span;
    

    【讨论】:

      猜你喜欢
      • 2014-06-30
      • 1970-01-01
      • 2021-09-07
      • 2012-06-17
      • 2018-03-12
      • 2011-01-30
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多