本文介绍了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 =约翰计划在加州大学伯克利分校的电气工程专业,并与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(组织名称为:+ 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; (int j = 0; j< l; j ++)

{
fd = fd.append(a [j] +\\\
);

}
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; (int j = 0; j< l; j ++){
fd = fd.append(a [j] +\\\
);


}

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);
令牌tz = new TokenizerME(tm);
令牌= tz.tokenize(令牌);
// System.out.println(Tokens [1]);
} catch(IOException e){
e.printStackTrace();
}
}

}

我有错误与以下行

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

错误是

 类型不匹配:无法从opennlp.tools.util.Span []转换为main.java.opennlp.tools.util.Span [] 

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



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

解决方案

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


I am new to java opennlp and i am trying to implement a program that extracts city names from a file but i am testing my code on a string first and i get some errors the code is

 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();
    }
}

  }

I have errors with the following line

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

the error is

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

I don't know how to solve it in both locations

Any suggestions....??thanks in advance

解决方案

Why do you have imports of main.java.opennlp.*? Are those your classes, or do you have two separate copies of the dependency in two different places? There is something wrong with the way your project is set up.

这篇关于opennlp中的Span类不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:10