【发布时间】:2015-10-25 17:38:49
【问题描述】:
我是 NLP 的新手,目前正在尝试使用 Python NLTK。 NLTK 中更令人困惑的事情之一是语法构造。 在 NLTK 书中提供的示例中,语法是专门针对分析中的每个句子编写的。
grammar1 = nltk.CFG.fromstring("""
S -> NP VP
VP -> V NP | V NP PP
PP -> P NP
V -> "saw" | "ate" | "walked"
NP -> "John" | "Mary" | "Bob" | Det N | Det N PP
Det -> "a" | "an" | "the" | "my"
N -> "man" | "dog" | "cat" | "telescope" | "park"
P -> "in" | "on" | "by" | "with"
""")
sent = "Mary saw Bob".split()
rd_parser = nltk.RecursiveDescentParser(grammar1)
for tree in rd_parser.parse(sent):
print(tree)
(S (NP Mary) (VP (V saw) (NP Bob)))
我想分析大量的报纸文章,显然为每个句子编写专门的语法并不是一项可行的任务。具体来说,我需要知道每个句子的子句数。这样的任务是否已经存在一种语法,或者如果没有,人们将如何解决?
我的所有句子都经过解析和 POS 标记——例如,
[(u'Her', 'PRP$'),
(u'first', 'JJ'),
(u'term', 'NN'),
(u'followed', 'VBN'),
(u'a', 'DT'),
(u'string', 'NN'),
(u'of', 'IN'),
(u'high', 'JJ'),
(u'profile', 'NN'),
(u'police', 'NNS'),
(u'abuse', 'VBP'),
(u'cases', 'NNS'),
(u'including', 'VBG'),
(u'the', 'DT'),
(u'choking', 'NN'),
(u'death', 'NN'),
(u'of', 'IN'),
(u'a', 'DT'),
(u'Hispanic', 'NNP'),
(u'man', 'NN'),
(u'in', 'IN'),
(u'1994', 'CD'),
(u'the', 'DT'),
(u'Louima', 'NNP'),
(u'case', 'NN'),
(u'in', 'IN'),
(u'1997', 'CD'),
(u'and', 'CC'),
(u'the', 'DT'),
(u'shooting', 'NN'),
(u'deaths', 'NNS'),
(u'of', 'IN'),
(u'a', 'DT'),
(u'West', 'NNP'),
(u'African', 'NNP'),
(u'immigrant', 'NN'),
(u'in', 'IN'),
(u'1999', 'CD'),
(u'and', 'CC'),
(u'a', 'DT'),
(u'black', 'JJ'),
(u'security', 'NN'),
(u'guard', 'NN'),
(u'in', 'IN'),
(u'early', 'JJ'),
(u'2000', 'CD')]
【问题讨论】:
-
你的意思是所有句子都标记和POS标记?
标签: python regex parsing nlp nltk