【问题标题】:Provide Opennlp with a list of names为 Opennlp 提供名称列表
【发布时间】:2014-04-10 19:08:11
【问题描述】:

我正在尝试为我们公司创建一个聊天机器人,我们可以在其中向该机器人发送消息,然后使用 opennlp 解析字符串并运行一些脚本。

例如查询是

"I'm going to work on ProjectY, can you close ProjectX?" 

这应该会触发带有参数 ProjectX 的脚本 closeRepo.sh。

我遇到的问题是它正确地将上面的句子解析为两部分:

"I'm going to work on ProjectY"

和 "你能关闭 ProjectX"

但是,并非所有可能的项目都被正确解析。我有一个项目名称,其中 opennlp 不将其视为 NP,而是将其视为 ADVB 或其他东西,我认为它将其视为句子:你能快速关闭或类似的东西。

这是我的解析代码,我放出模型加载(我使用这里提供的标准模型:http://opennlp.sourceforge.net/models-1.5/

    String sentences[] = sentenceDetector.sentDetect(input);
    for(int i = 0; i < sentences.length; i++){
        String[] tokens = tokenizer.tokenize(sentences[i]);
        StringBuffer sb = new StringBuffer();
        for(String t : tokens){
            sb.append(t);
            sb.append(' ');
        }
        sb.deleteCharAt(sb.length()-1);//remove last space

        sentences[i] = sb.toString();
    }
    ArrayList<Parse> parses = new ArrayList<Parse>();
    for(String s : sentences){
        Parse topParses[] = ParserTool.parseLine(s, parser, 1);
        if(topParses.length > 0){
            parses.add(topParses[0]);
        }
    }
    return parses;

如果这样会更容易,我愿意切换到斯坦福的 nlp。但我的问题是:

有没有办法让 opennlp 列出我的项目并将它们检测为 NP还是NN?

【问题讨论】:

    标签: java nlp opennlp


    【解决方案1】:

    使用 OpenNLP 句子分块器可能会更好,它工作得很好,并检查是否有任何名词短语包含您的项目名称之一。像这样。

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import opennlp.tools.chunker.ChunkerME;
    import opennlp.tools.chunker.ChunkerModel;
    import opennlp.tools.postag.POSModel;
    import opennlp.tools.postag.POSTaggerME;
    import opennlp.tools.tokenize.TokenizerME;
    import opennlp.tools.tokenize.TokenizerModel;
    import opennlp.tools.util.Span;
    
    /**
     *
     * Extracts noun phrases from a sentence. To create sentences using OpenNLP use
     * the SentenceDetector classes.
     */
    public class OpenNLPNounPhraseExtractor {
    
    
    
      public static void main(String[] args) {
    
        try {
    
          String modelPath = "c:\\temp\\opennlpmodels\\";
          TokenizerModel tm = new TokenizerModel(new FileInputStream(new File(modelPath + "en-token.zip")));
          TokenizerME wordBreaker = new TokenizerME(tm);
          POSModel pm = new POSModel(new FileInputStream(new File(modelPath + "en-pos-maxent.zip")));
          POSTaggerME posme = new POSTaggerME(pm);
          InputStream modelIn = new FileInputStream(modelPath + "en-chunker.zip");
          ChunkerModel chunkerModel = new ChunkerModel(modelIn);
          ChunkerME chunkerME = new ChunkerME(chunkerModel);
          //this is your sentence
          String sentence = "Barack Hussein Obama II  is the 44th President of the United States, and the first African American to hold the office.";
          //words is the tokenized sentence
          String[] words = wordBreaker.tokenize(sentence);
          //posTags are the parts of speech of every word in the sentence (The chunker needs this info of course)
          String[] posTags = posme.tag(words);
          //chunks are the start end "spans" indices to the chunks in the words array
          Span[] chunks = chunkerME.chunkAsSpans(words, posTags);
          //chunkStrings are the actual chunks
          String[] chunkStrings = Span.spansToStrings(chunks, words);
          for (int i = 0; i < chunks.length; i++) {
            String np = chunkStrings[i];
            if (np.contains("some project name")) {
              System.out.println(np);
            //do something here
            }
          }
    
    
        } catch (IOException e) {
        }
      }
    
    }
    

    顺便说一句,您正在尝试做的事情意味着对统计 NLP 方法的期望极高。句子分块是基于模型的,如果您的聊天不符合创建模型的数据的一般形状,那么无论您使用 opennlp 还是 stanford 或其他任何东西,您的结果都会有问题。听起来您还试图提取与项目名称 NP 相关的“要采取的行动”,您可能会修改动词短语提取。我不建议根据对潜在嘈杂句子的概率解析自动触发 sh 脚本!

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2012-04-28
      • 2020-10-08
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多