如何使用OpenNLP从文本获取位置?

时间:2018-11-07 16:26:01

标签: java location stanford-nlp opennlp

我正在使用分块标记数据并最初从文本中获取位置,当我们使用还被称为名词短语的名词短语名称以致无法使用时,我尝试从下一个提取名词短语,然后我移到了位置NLP核心 我尝试运行以下代码

 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    try {

InputStream inputStreamTokenizer =新          FileInputStream(“ D:\ project \文本文档中的关系提取\ Libraray \ parsing / en-token.bin”);       TokenizerModel tokenModel =新的TokenizerModel(inputStreamTokenizer);

  //String paragraph = "Mike and Smith are classmates"; 
  String paragraph = "Tutorialspoint is located in Hyderabad"; 

  //Instantiating the TokenizerME class 
  TokenizerME tokenizer = new TokenizerME(tokenModel); 
  String tokens[] = tokenizer.tokenize(paragraph); 

  //Loading the NER-location moodel 
  InputStream inputStreamNameFinder = new 
     FileInputStream("D:\\project\\Relation Extraction in Text Document\\Libraray\\parsing/en-ner-location.bin");       
  TokenNameFinderModel location = new TokenNameFinderModel(inputStreamNameFinder); 

  //Instantiating the NameFinderME class 
  NameFinderME nameFinder;      
        nameFinder = new NameFinderME(location);

  //Finding the names of a location 
  Span nameSpans[] = nameFinder.find(tokens);        
  //Printing the spans of the locations in the sentence 
 for(Span s: nameSpans)        
     System.out.println(s.toString()+"  "+tokens[s.getStart()]);

我收到一个错误消息“ java.lang.UnsupportedOperationException:尚不支持。”

“ nameFinder = new NameFinderME(location);”处的错误符号;说“ exmp.TokenNameFinderModel无法转换为opennlp.tools.namefind.TokenNameFinderModel”是什么原因

1 个答案:

答案 0 :(得分:0)

您的导入不正确,这是一个有效的版本:

import java.io.FileInputStream;
import java.io.InputStream;

import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;

和输出:[4..5) location Hyderabad