NLTK中用于解析的英语语法

76

有没有一个现成的英语语法可以直接加载到NLTK中使用?我在NLTK的解析示例中搜索了一下,但似乎在句子解析之前必须手动指定语法。

非常感谢!

8个回答

33
你可以查看 pyStatParser,这是一个简单的Python统计分析器,可以返回NLTK解析树。它附带公共树库,并且只有在实例化解析器对象时(大约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))))
  (. ?))

对于Python 3用户,这里有一个拉取请求来添加Python 3支持:https://github.com/emilmont/pyStatParser/pull/7。在使用“2to3”工具将所有文件从Python 2转换为Python 3之后,我才发现了该拉取请求。 - VinceFior
构建语法模型并运行示例:使用默认文本硬编码的 python example.py。非常易于使用和嵌入。 - loretoparisi
我已经执行了以下命令,以便使用pyStatParser: 2to3 --output-dir=stat_parser3 -W -n stat_parser rm star_parser mv stat_parser3 stat_parser setup.py build setup.py install 它起作用了,谢谢@emilmont。 - Mancy
该库将解析"The Sun rises from the East"为 - (SINV (NP (NP (DT the) (NNP Sun) (NNP rises)) (PP (IN from) (NP (DT the) (NNP East)))) (. .)) “rises”不应该是一个VP吗?我们如何避免将“rises”解释为专有名词? - argmin

26

我的库,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%以上。


1
谢谢您提供这个信息,我很兴奋地想要尝试一下spaCy。有没有办法只选择性地导入最少量的数据来解析您的示例句子?每当我运行 spacy.en.download all 时,它都会启动一个似乎超过600MB的下载! - wil3
此外,我的空白1GB RAM虚拟机似乎无法处理spaCy所需的内存,并显示MemoryError错误。我猜测它是将整个数据集加载到内存中了? - Xeoncross
你不能仅加载解析一句话所需的数据,假设的使用情况是您会解析任意文本。每个进程需要2-3GB的内存。我们希望在完成切换到神经网络后内存要求会降低。同时,我们已经添加了多线程支持,因此您可以将内存要求分摊到多个CPU上。 - syllogism_
2
请注意,正确的用法现在是 for sent in doc.sents: - Phylliida
@JamesKo API已更改,请使用:import spacy,然后nlp = spacy.load('en'),最后将您的句子处理为:doc = nlp(u'在此处输入未经处理的文档') - Carlo Mazzaferro
现在可以使用命令 python -m spacy download en 进行下载。 - Sam Redway

6

有一个名为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' ... 

1
这是浅层解析输出(也称为分块)。我不确定这是否符合OP的要求。 - Nikana Reklawyks

6
nltk_data分发中有几个语法。在您的Python解释器中,输入nltk.download()即可下载。

1
是的,但对于任意句子来说还不够。当我尝试一些随机的句子时,它会显示“语法不包括某些输入词:...”。我做错了吗?我想要得到一个句子的解析树。这是正确的方法吗?谢谢! - roboren
6
你可以在nltk_data中取出Penn Treebank部分,并将树状片段(一个节点及其直接子节点)转换成规则,从而导出CFG。但是,如果你想找到一个“真正”的语法,除非你探索统计解析,否则你可能找不到;现在没有人再建立非随机文法了,因为它们根本行不通,除非用于特定领域的应用。 - Fred Foo
2
nltk 是否提供统计分析?否则,我可能想切换到斯坦福解析器。再次感谢 =) - roboren
是的:http://nltk.googlecode.com/svn-history/r7492/trunk/doc/api/nltk.parse.ViterbiParse-class.html。不确定您是否需要自己推导语法。 - Fred Foo

5

使用MaltParser,你可以得到一个预先训练好的英语语法和其他一些预先训练好的语言。而且MaltParser是一个依存分析器,而不是一些简单的自底向上或自顶向下的分析器。

只需从http://www.maltparser.org/index.html下载MaltParser并像这样使用NLTK:

import nltk
parser = nltk.parse.malt.MaltParser()

4
MaltParser看起来很不错,但是我无法通过nltk使其工作(它一直以失败的消息“找不到MaltParser配置文件:malt_temp.mco”而结束)。关于MaltParser本身,我已经成功地运行了它。 - Nathaniel Waisbrot

4
你有没有在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')]

Got this example from here NLTK_chapter03


4

我尝试过NLTK、PyStatParser和Pattern。在我看来,Pattern是上述文章中介绍的最好的英语解析器。因为它支持pip安装,并且网站上有一个漂亮的文档(http://www.clips.ua.ac.be/pages/pattern-en)。我找不到合理的NLTK文档(它默认给我不准确的结果,我也找不到如何调整它)。在我的环境中,pyStatParser比上面描述的要慢得多。(大约需要一分钟进行初始化,而对长句子进行解析需要几秒钟。也许我没有正确使用它)。


1
模式似乎并没有进行解析(如依存句法分析),只有词性标注和可能的分块。对于长句子,分析器需要一些时间是相当正常的。 - Nikana Reklawyks
1
@NikanaReklawyks,确切地说,在这里正确的nltk工具就像是PyStatParser,它构建了一个语法,即PCFG语法,即概率上下文无关文法 - http://www.cs.columbia.edu/~mcollins/courses/nlp2011/notes/pcfgs.pdf - loretoparisi

1
我发现nltk和由斯坦福开发的解析语法很搭配。 使用Stanford CoreNLP和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."))

准备工作:

  1. 下载Java Stanford模型
  2. 运行CoreNLPServer

您可以使用以下代码来运行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() 来停止服务器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接