当前位置: 首页 > 知识库问答 >
问题:

opennlp中的Span类不工作

寇甫
2023-03-14

我是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[]

我不知道如何解决这两个问题

任何建议......??提前谢谢

共有2个答案

景明诚
2023-03-14

我只改变了你的进口和工程罚款:

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

姜楷
2023-03-14

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

 类似资料:
  • 我是java opennlp新手,我正在尝试实现一个从文件中提取城市名称的程序,但我首先在字符串上测试我的代码,我得到了一些错误代码是错误的 以及我在span veriable span sp[]=nf中得到的错误。查找(cnt);错误是类型不匹配:无法从opennlp转换。工具。util。Span[]到main。JAVAopennlp。工具。util。Span[]我不知道如何解决这两个问题 有什

  • 我正在尝试从使用非特定span类来格式化/显示内容的网站中刮取数据。这些页面提供了有关化学产品的信息,并且每个产品都在一个div类中进行描述。我首先用那个div类进行解析,然后从那里提取我需要的数据。我已经能够得到许多东西,但我似乎无法提取的部分是在span类“ppisreportspan”中。如果您查看代码,您会注意到它在每个化学描述中出现多次。 这个样品包括两种化学物质。一个有“Alstar”

  • 我在Visual Studio中使用硒。编写一个测试用例,我想打开一个下拉菜单。我想使用唯一的名称来识别菜单。 代码看起来像这样: 我通常的解决方案是: 问题在于span类:“mat select占位符ng-tns-c34-170 ng star inserted”并不总是相同的。每次再次加载页面时,数字“c34-170”将更改为一组新的数字。 我的问题是:是否可以通过指定div类来识别“Uniq

  • 我正在尝试使用Apache OpenNLP API创建一个标记器。我已经从他们的网站中提取了代码,但在Tokenize类中,以下代码行出现了“不兼容类型”错误: Tokenize Tokenizer=new TokenizerME(模型); 有人知道这个错误的原因吗?因为看起来他们不应该不兼容? 这是主要类: 这是具有错误的Tokenize类:

  • 我有这个代码: 我已经尝试过这个,但不工作:

  • org.springframework.beans.factory.beanCreationException:创建名为“birthday controller”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springframework.beans.factory.beanCreationException:无法自动连接字段:private com.esri.birthd