有没有一个现成的英语语法可以直接加载到NLTK中使用?我在NLTK的解析示例中搜索了一下,但似乎在句子解析之前必须手动指定语法。
非常感谢!
>>> 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))))
(. ?))
我的库,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等人(2015)发现spaCy是可用的最快的依存关系分析器。 它在单个线程上每秒处理超过13,000句话。 在标准的WSJ评估中,它得分92.7%,比CoreNLP的任何模型都要准确高出1%以上。
spacy.en.download all
时,它都会启动一个似乎超过600MB的下载! - wil3for sent in doc.sents:
。 - Phylliidaimport spacy
,然后nlp = spacy.load('en')
,最后将您的句子处理为:doc = nlp(u'在此处输入未经处理的文档')
。 - Carlo Mazzaferropython -m spacy download en
进行下载。 - Sam Redway有一个名为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' ...
nltk_data
分发中有几个语法。在您的Python解释器中,输入nltk.download()
即可下载。nltk_data
中取出Penn Treebank部分,并将树状片段(一个节点及其直接子节点)转换成规则,从而导出CFG。但是,如果你想找到一个“真正”的语法,除非你探索统计解析,否则你可能找不到;现在没有人再建立非随机文法了,因为它们根本行不通,除非用于特定领域的应用。 - Fred Foo使用MaltParser,你可以得到一个预先训练好的英语语法和其他一些预先训练好的语言。而且MaltParser是一个依存分析器,而不是一些简单的自底向上或自顶向下的分析器。
只需从http://www.maltparser.org/index.html下载MaltParser并像这样使用NLTK:
import nltk
parser = nltk.parse.malt.MaltParser()
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')]
Got this example from here NLTK_chapter03
我尝试过NLTK、PyStatParser和Pattern。在我看来,Pattern是上述文章中介绍的最好的英语解析器。因为它支持pip安装,并且网站上有一个漂亮的文档(http://www.clips.ua.ac.be/pages/pattern-en)。我找不到合理的NLTK文档(它默认给我不准确的结果,我也找不到如何调整它)。在我的环境中,pyStatParser比上面描述的要慢得多。(大约需要一分钟进行初始化,而对长句子进行解析需要几秒钟。也许我没有正确使用它)。
nltk
工具就像是PyStatParser
,它构建了一个语法,即PCFG
语法,即概率上下文无关文法 - http://www.cs.columbia.edu/~mcollins/courses/nlp2011/notes/pcfgs.pdf - loretoparisifrom 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()
python example.py
。非常易于使用和嵌入。 - loretoparisi2to3 --output-dir=stat_parser3 -W -n stat_parser
rm star_parser
mv stat_parser3 stat_parser
setup.py build
setup.py install
它起作用了,谢谢@emilmont。 - Mancy(SINV (NP (NP (DT the) (NNP Sun) (NNP rises)) (PP (IN from) (NP (DT the) (NNP East)))) (. .))
“rises”不应该是一个VP
吗?我们如何避免将“rises”解释为专有名词? - argmin