【发布时间】:2011-09-01 05:19:21
【问题描述】:
是否有可以直接加载并在 NLTK 中使用的即用型英语语法?我搜索了使用 NLTK 进行解析的示例,但似乎我必须在解析句子之前手动指定语法。
非常感谢!
【问题讨论】:
是否有可以直接加载并在 NLTK 中使用的即用型英语语法?我搜索了使用 NLTK 进行解析的示例,但似乎我必须在解析句子之前手动指定语法。
非常感谢!
【问题讨论】:
你可以看看pyStatParser,一个简单的python统计解析器,它返回NLTK解析树。它带有公共树库,并且仅在您第一次实例化 Parser 对象时(大约 8 秒)生成语法模型。它使用 CKY 算法,可以在一秒钟内解析平均长度的句子(如下所示)。
>>> from stat_parser import Parser
>>> parser = Parser()
>>> print parser.parse("How can the net amount of entropy of the universe be massively decreased?")
(SBARQ
(WHADVP (WRB how))
(SQ
(MD can)
(NP
(NP (DT the) (JJ net) (NN amount))
(PP
(IN of)
(NP
(NP (NNS entropy))
(PP (IN of) (NP (DT the) (NN universe))))))
(VP (VB be) (ADJP (RB massively) (VBN decreased))))
(. ?))
【讨论】:
2to3 工具“手动”转换所有文件后才发现该拉取请求从 Python 2 到 Python 3。
python example.py 使用硬编码的默认文本。非常易于使用和嵌入。
2to3 --output-dir=stat_parser3 -W -n stat_parser rm star_parser mv stat_parser3 stat_parser setup.py build setup.py install 并且它有效,谢谢@emilmont
(SINV (NP (NP (DT the) (NNP Sun) (NNP rises)) (PP (IN from) (NP (DT the) (NNP East)))) (. .)) “升起”不应该是 VP 吗?我们如何避免将“上升”解释为专有名词?
我的库 spaCy 提供了一个高性能的依赖解析器。
安装:
pip install spacy
python -m spacy.en.download all
用法:
from spacy.en import English
nlp = English()
doc = nlp(u'A whole document.\nNo preprocessing require. Robust to arbitrary formating.')
for sent in doc:
for token in sent:
if token.is_alpha:
print token.orth_, token.tag_, token.head.lemma_
Choi et al. (2015) 发现 spaCy 是可用的最快的依赖解析器。它在一个线程上每秒处理超过 13,000 个句子。在标准的 WSJ 评估中,它的得分为 92.7%,比任何 CoreNLP 模型的准确率都高出 1% 以上。
【讨论】:
spacy.en.download all 时,它都会启动似乎超过 600 MB 的下载!
for sent in doc.sents:
import spacy,然后是nlp = spacy.load('en'),然后将您的句子处理为:doc = nlp(u'Your unprocessed document here)
nltk_data 发行版中有一些语法。在您的 Python 解释器中,发出 nltk.download()。
【讨论】:
nltk_data 中的 Penn 树库部分,并通过简单地将树片段(节点及其直接子节点)转换为规则来从中派生 CFG。但是除非您研究统计解析,否则您可能不会找到“真正的”语法;没有人构建非随机语法了,因为它们根本不起作用,除了非常特定领域的应用程序。
有一个名为Pattern 的库。它非常快速且易于使用。
>>> from pattern.en import parse
>>>
>>> s = 'The mobile web is more important than mobile apps.'
>>> s = parse(s, relations=True, lemmata=True)
>>> print s
'The/DT/B-NP/O/NP-SBJ-1/the mobile/JJ/I-NP/O/NP-SBJ-1/mobile' ...
【讨论】:
使用 MaltParser,你有一个预训练的英语语法,还有一些其他预训练的语言。 而且 Maltparser 是一个依赖解析器,而不是一些简单的自下而上或自上而下的解析器。
只需从 http://www.maltparser.org/index.html 下载 MaltParser 并像这样使用 NLTK:
import nltk
parser = nltk.parse.malt.MaltParser()
【讨论】:
我尝试过 NLTK、PyStatParser、Pattern。恕我直言,Pattern 是上面文章中介绍的最好的英文解析器。因为它支持 pip install 并且网站上有一个花哨的文档(http://www.clips.ua.ac.be/pages/pattern-en)。我找不到合理的 NLTK 文档(默认情况下它给了我不准确的结果。而且我找不到如何调整它)。 pyStatParser 在我的环境中比上面描述的要慢得多。 (初始化大约一分钟,解析长句子需要几秒钟。也许我没有正确使用它)。
【讨论】:
nltk 工具就像 PyStatParser 构建的语法是 PCFG 语法,即概率上下文无关语法 - cs.columbia.edu/~mcollins/courses/nlp2011/notes/pcfgs.pdf
您是否尝试过在 NLTK 中进行 POS 标记?
text = word_tokenize("And now for something completely different")
nltk.pos_tag(text)
答案是这样的
[('And', 'CC'), ('now', 'RB'), ('for', 'IN'), ('something', 'NN'),('completely', 'RB'), ('different', 'JJ')]
从这里得到这个例子NLTK_chapter03
【讨论】:
我发现 nltk 与斯坦福开发的解析器语法配合得很好。
Syntax Parsing with Stanford CoreNLP and NLTK
使用 Stanford CoreNLP 和 NLTK 非常容易上手。您只需要做一些小准备,然后您就可以使用以下代码解析句子:
from nltk.parse.corenlp import CoreNLPParser
parser = CoreNLPParser()
parse = next(parser.raw_parse("I put the book in the box on the table."))
准备工作:
您可以使用以下代码运行 CoreNLPServer:
import os
from nltk.parse.corenlp import CoreNLPServer
# The server needs to know the location of the following files:
# - stanford-corenlp-X.X.X.jar
# - stanford-corenlp-X.X.X-models.jar
STANFORD = os.path.join("models", "stanford-corenlp-full-2018-02-27")
# Create the server
server = CoreNLPServer(
os.path.join(STANFORD, "stanford-corenlp-3.9.1.jar"),
os.path.join(STANFORD, "stanford-corenlp-3.9.1-models.jar"),
)
# Start the server in the background
server.start()
不要忘记通过执行 server.stop() 来停止服务器
【讨论】: