- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想对一个英文句子进行 POSTtag 并进行一些处理。我想使用openNLP。我已经安装了
当我执行命令时
I:\Workshop\Programming\nlp\opennlp-tools-1.5.0-bin\opennlp-tools-1.5.0>java -jar opennlp-tools-1.5.0.jar POSTagger models\en-pos-maxent.bin < Text.txt
它给出输出POSTagging Text.txt中的输入
Loading POS Tagger model ... done (4.009s)
My_PRP$ name_NN is_VBZ Shabab_NNP i_FW am_VBP 22_CD years_NNS old._.
Average: 66.7 sent/s
Total: 1 sent
Runtime: 0.015s
我希望它安装正确?
现在我如何从 Java 应用程序内部进行 POSTtagging?我已将 openNLPtools、jwnl、maxent jar 添加到项目中,但如何调用 POSTtagging?
最佳答案
这是我拼凑起来的一些(旧)示例代码,后面还有现代化的代码:
package opennlp;
import opennlp.tools.cmdline.PerformanceMonitor;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
public class OpenNlpTest {
public static void main(String[] args) throws IOException {
POSModel model = new POSModelLoader().load(new File("en-pos-maxent.bin"));
PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
POSTaggerME tagger = new POSTaggerME(model);
String input = "Can anyone help me dig through OpenNLP's horrible documentation?";
ObjectStream<String> lineStream =
new PlainTextByLineStream(new StringReader(input));
perfMon.start();
String line;
while ((line = lineStream.read()) != null) {
String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line);
String[] tags = tagger.tag(whitespaceTokenizerLine);
POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
System.out.println(sample.toString());
perfMon.incrementCounter();
}
perfMon.stopAndPrintFinalResult();
}
}
输出是:
Loading POS Tagger model ... done (2.045s)
Can_MD anyone_NN help_VB me_PRP dig_VB through_IN OpenNLP's_NNP horrible_JJ documentation?_NN
Average: 76.9 sent/s
Total: 1 sent
Runtime: 0.013s
这基本上是在作为 OpenNLP 一部分包含的 POSTaggerTool 类中工作的。 sample.getTags()
是一个 String
数组,其中包含标签类型本身。
这需要直接访问训练数据的文件,这真的很糟糕。
为此更新的代码库略有不同(并且可能更有用。)
首先,一个 Maven POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.javachannel</groupId>
<artifactId>opennlp-example</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>[6.8.21,)</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是作为测试编写的代码,因此位于 ./src/test/java/org/javachannel/opennlp/example
:
package org.javachannel.opennlp.example;
import opennlp.tools.cmdline.PerformanceMonitor;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.stream.Stream;
public class POSTest {
private void download(String url, File destination) throws IOException {
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
@DataProvider
Object[][] getCorpusData() {
return new Object[][][]{{{
"Can anyone help me dig through OpenNLP's horrible documentation?"
}}};
}
@Test(dataProvider = "getCorpusData")
public void showPOS(Object[] input) throws IOException {
File modelFile = new File("en-pos-maxent.bin");
if (!modelFile.exists()) {
System.out.println("Downloading model.");
download("http://opennlp.sourceforge.net/models-1.5/en-pos-maxent.bin", modelFile);
}
POSModel model = new POSModel(modelFile);
PerformanceMonitor perfMon = new PerformanceMonitor(System.err, "sent");
POSTaggerME tagger = new POSTaggerME(model);
perfMon.start();
Stream.of(input).map(line -> {
String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line.toString());
String[] tags = tagger.tag(whitespaceTokenizerLine);
POSSample sample = new POSSample(whitespaceTokenizerLine, tags);
perfMon.incrementCounter();
return sample.toString();
}).forEach(System.out::println);
perfMon.stopAndPrintFinalResult();
}
}
这段代码实际上并没有测试任何东西——它是一个冒烟测试,如果有的话——但它应该作为一个起点。另一个(可能)好的事情是,如果您还没有下载模型,它会为您下载模型。
关于java - 如何在 Java 中使用 OpenNLP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836148/
我是 opennlp 的新手,需要帮助来自定义解析器 我使用了带有预训练模型 en-pos-maxtent.bin 的 opennlp 解析器,用相应的语音部分标记新的原始英语句子,现在我想自定义标签
谁能指出 openNLP NameFinder 模块使用的算法? 代码很复杂,而且文档很少,并且作为一个黑盒(提供默认模型)使用它给我的印象是它主要是启发式的。 以下是输入和输出的一些示例: 输入:
我收到无效格式异常。我看到有人建议从 en-pos-maxent.bin 文件中删除 tags.tagdict 文件,但我不知道该怎么做。谁能给我解释一下 ava.io.FileInputSt
我收到无效格式异常。我看到有人建议从 en-pos-maxent.bin 文件中删除 tags.tagdict 文件,但我不知道该怎么做。谁能给我解释一下吗 ava.io.FileInputS
我正在尝试使用 Apache OpenNLP 1.7 构建自定义 NER。来自可用文档 Here ,我开发了如下代码 import java.io.BufferedOutputStream; impo
我们有组织名称同义词的数据库(例如 BT 是 British Telecom。我们使用 OpenNLP 从文本块中提取实体和关键字。有没有办法告诉 OpenNLP 使用我们的数据库数据(例如,如果它找
我刚开始使用 openNLP 来识别名称。我正在使用开放 NLP 附带的模型 (en-ner-person.bin)。我注意到虽然它可以识别我们、英国和欧洲的名字,但它无法识别印度或日本的名字。我的问
有谁知道我在哪里可以找到如何在 OpenNLP 库中使用 SimpleLemmatizer() 类的示例,以及在哪里可以找到示例英语词典?文档中似乎缺少它。 最佳答案 您可以从这里下载字典 - en-
有没有关于 OpenNLP 中解析器标签含义的文档?我知道 POS 标签类型遵循 TreeBank 约定,但不幸的是我没有找到有关解析器标签的任何信息,例如“SBAR”等。 该文档是否存在于某处还是我
我以前用过Stanford CoreNLP,这次想研究一下OpenNLP。是否可以创建自己的注释器?例如,我想分析文本并仅挑选颜色或飞机名称。 斯坦福 NER 让我创建自己的 NER 模型来实现此目的
您好,已经引用过 this , this , this和 this但仍然发现构建自定义名称查找器模型很困难..这是代码: public class CustomClassifierTrainer {
我目前正在浏览 opennlp 源代码,试图找到/理解它们用于分块的语法。这不是最简单的任务之一。我开始研究 chunkermodel 和相关的类,但还没有走得太远...... 有人搜过这个吗?如果有
我正在尝试使用 OpenNLP 标记器对文本文件进行标记。我所做的,我读取 .txt 文件并将其存储在列表中,想要迭代每一行,标记该行并将标记化的行写入新文件。 行中: tokens[i] = tok
嘿,我正在尝试为 opennlp 制作训练数据来检测句子中的位置名称。我陷入了这样的困境: North Manchester Hospital 我确实需要检测这两个对象,医院名称和城市名称。我
我正在尝试使用官方 OpenNLP 网站手册示例来训练新模型,示例如下: Charset charset = Charset.forName("UTF-8"); ObjectStrea
我一直在尝试使用命令行界面来训练我的模型,如下所示: opennlp TokenNameFinderTrainer -model en-ner-pincode.bin -iterations 500
我正在使用 OpenNLP 的模型构建器插件来创建更好的 NER 模型。据此post ,我使用了markg发布的代码: public class ModelBuilderAddonUse { pr
我正在做一个项目,我正在使用 OpenNLP 的一些功能。我需要的一个功能是词干分析器。我用谷歌搜索了一下,发现据说它在 opennlp.tools.stemmer 包中有一个 Porter 词干分析
我有两个模型文件:1)en-politicians-ner.bin 2)en-engineers-ner.bin 现在,有没有办法将这两个模型添加到一个单个 NameFinderME 对象中。使用这两
我正在使用 Java OpenNLP 进行 NER,但我不确定如何使用我训练过的自定义模型来检测多个单词(例如 New York、Bruno Mars、Hong Kong)。 我的训练数据确实涵盖了多
我是一名优秀的程序员,十分优秀!