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

为Opennlp提供一个名称列表

戚浩淼
2023-03-14

我正在尝试为我们的公司创建一个聊天机器人,我们可以向这个机器人发送html" target="_blank">消息,然后它使用opennlp解析字符串并运行一些脚本。

例如,一个查询是

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

这应该会触发closeRepo的脚本。用ProjectX参数sh。

我的问题是,它正确地将上述句子分为两部分:

"I'm going to work on ProjectY"

还有“你能关闭ProjectX吗”

然而,并非所有可能的项目都被正确解析。我有一个projectname,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?

共有1个答案

楮阳
2023-03-14

您可能最好使用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脚本!

 类似资料:
  • 我正在寻找使用我的应用程序在我的设备上安装的不同应用程序的第一次安装时间。非常基本的PackageManager-

  • 我正在使用OpenNLP的NameFinder API示例文档。初始化名称查找器后,文档使用以下代码作为输入文本: 然而,当我将其引入eclipse时,“documents”(而不是“document”)变量给了我一个错误,表示变量documents无法解析。“documents”数组变量所指的文档是什么?我是否需要初始化一个名为“documents”的数组,该数组包含txt文件,以消除此错误?

  • 问题内容: 我在HTML页面上有几个具有相同类的元素-但是它们是不同的元素类型。我想在遍历元素时找出元素的标签名称-但是.attr不会使用“标签”或“标签名称”。 这就是我的意思。考虑页面上的以下元素: 现在,我想运行类似的代码,以确保所有元素都具有一个ID(如果尚未定义): 我想要的结果是H2和H4元素的ID为 分别。 关于如何发现“ this”表示的元素的标签名称的任何想法? 问题答案: 应该

  • 所以我有一个叫做PlayingCard的类,它创建了一个包含int秩和int花色(模拟扑克牌)的对象。 然后我需要创建一个名为PackBuilder的程序,该程序应该模拟使用我的类构建一副卡片。 问题是我不知道如何给每个对象起一个新名字。我想到了类似这样的数组: 但是它说cardDeck已经定义好了(我不确定我是否做得不对,或者使用数组是否不起作用) 我想要的命名方案类似于“card1”“card

  • 我刚开始使用openNLP来识别名字。我使用的是open NLP附带的模型(en-ner-person.bin)。我注意到,虽然它可以识别美国、英国和欧洲的名字,但它不能识别印度或日本的名字。我的问题是(1)是否已经有模型,我可以用来识别外国名字(2)如果没有,那么我相信我将需要生成新的模型。在这种情况下,是否有一个可供我使用的copora?

  • 我正在写一个实用程序之间的集成内部系统和第三方产品。我试图生成一个可以由第三方产品加载的xml文件,但我很难按照他们的要求生成xml。我已经创建了一个简化的版本,只是为了测试。 预期产出应如下: 我的代码如下: 主要: 课程类别: