在NLTK中parsing英文语法

有没有一个现成的英语语法,我可以加载它并在NLTK中使用? 我已经search了parsingNLTK的例子,但似乎我必须在parsing一个句子之前手动指定语法。

非常感谢!

你可以看一下pyStatParser ,一个简单的python统计分析器,它返回NLTK分析树。 它带有公共树库,只有在您第一次实例化parsing器对象(大约8秒钟)时才会生成语法模型。 它使用CKYalgorithm,并且在一秒之内parsing平均长度句子(如下所示)。

>>> 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提供了一个高性能的依赖关系parsing器。

安装:

 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是可用的最快的依赖parsing器。 它在一个线程上每秒处理超过13000个句子。 在标准的WSJ评估中,得分为92.7%,比CoreNLP的任何模型都高出1%以上。

有一个叫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()

使用MaltParser,你有一个预训练的英语语法,还有一些其他的预训练语言。 而Maltparser是一个依赖parsing器,而不是一些简单的自下而上或自上而下的parsing器。

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

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

我试过NLTK,PyStatParser,Pattern。 恕我直言模式是上述文章中介绍的最好的英语parsing器。 因为它支持pip安装和网站上有一个奇特的文件( http://www.clips.ua.ac.be/pages/pattern-en )。 我无法find合理的NLTK文件(而且它给了我默认的不准确的结果,我也找不到如何调整它)。 pyStatParser比我上面描述的要慢得多。 (初始化大约一分钟,parsing长句需要几秒钟,也许我没有正确使用它)。

你在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