gpt4 book ai didi

java - 向 Opennlp 提供名称列表

转载 作者:行者123 更新时间:2023-12-01 13:10:01 28 4
gpt4 key购买 nike

我正在尝试为我们公司创建一个聊天机器人,我们可以向该机器人发送消息,然后该机器人使用 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?

最佳答案

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

关于java - 向 Opennlp 提供名称列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22956143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com