从java中的词干文本中提取专有名词

时间:2016-03-14 05:45:28

标签: java nlp opennlp porter-stemmer

我正在使用OpenNLP从句子中提取专有名词。这是我的代码:

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;

public class ParserTest {

    static Set<String> nounPhrases = new HashSet<>();

    private static String line = "iran india pai oil due euro delhi iran ask indian refin essar oil mangalor refineri petrochem mrpl clear oil due amount billion euro month lift sanction iran told indian author three year mechan pai cent oil import bill rupe keep remain cent pend payment channel clear end.";

    public void getNounPhrases(Parse p) {
        if (p.getType().equals("NNP") || p.getType().equals("NNPS")) {
             nounPhrases.add(p.getCoveredText());
             System.out.println(p.getCoveredText());
        }

        for (Parse child : p.getChildren()) {
             getNounPhrases(child);
        }
    }


    public void parserAction() throws Exception {
        InputStream is = new FileInputStream("C:\\Users\\asus\\Downloads\\en-parser-chunking.bin");
        ParserModel model = new ParserModel(is);
        Parser parser = ParserFactory.create(model);
        Parse topParses[] = ParserTool.parseLine(line, parser, 1);
        for (Parse p : topParses){
            //p.show();
            getNounPhrases(p);
        }
    }
    public static void main(String[] args) throws Exception {
        new ParserTest().parserAction();
        System.out.println("List of Noun Parse : "+nounPhrases);
    }
}

问题在于它是一个词干文本(我使用的是Porter Stemming算法)所以每个单词都是小写的。因此,专有名词不会被提取。我上面提取专有名词的方法是否正确?如果是,那么我必须在代码中进行哪些更改才能使其正常工作?如果不是,那么建议我一个新的方法以及一个示例代码将帮助我做到这一点。

谢谢。

1 个答案:

答案 0 :(得分:0)

我认为除非你在词干文本上训练名词短语的chunker模型,否则你必须在词干完成之前在你的处理管道的上游运行名词短语提取。另外,在我看来,根据您输入的字符串,在您的过程中,您正在消除停止词和词干,这也会导致问题,因为您将无法正确对齐下游的两组跨度。 因此,如果您可以控制提供词干文本的过程,则应该将名词短语向上移动,并将原始令牌,词干标记,NNP和任何其他元数据(如NER结果)传递到您现在的位置...然后你就可以有所作为了。 HTH

相关问题