简体   繁体   English

在Python中使用NLTK进行绘图

[英]Chinking using NLTK in Python

I have been trying out some of the examples in the Python NLTK Book . 我一直在尝试Python NLTK Book中的一些示例。 For example, Chapter 7 talks about Chinking with this example: 例如,第7章通过以下示例讨论了Chinking:

grammar = r"""
    NP:
    {<.*>+}          # Chunk everything
    }<VBD|IN>+{      # Chink sequences of VBD and IN
  """
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"),
       ("dog", "NN"), ("barked", "VBD"), ("at", "IN"),  ("the", "DT"), ("cat", "NN")]
cp = nltk.RegexpParser(grammar)
result = cp.parse(sentence)

According to me, this supposed to excise "barked at" from the result. 据我说,这应该从结果中剔除。 But it doesn't. 但事实并非如此。 I am new to python and nltk, but what am I missing here? 我是python和nltk的新手,但是我在这里错过了什么? Is there something obvious which needs to be updated here? 是否有明显的东西需要在这里进行更新? Thanks.. 谢谢..

chunking creates chunks, while chinking breaks up those chunks. 分块会创建块,而chinking会分解这些块。

That's exactly what says "Python Text Processing with NLTK 2.0 Cookbook" by Jacob Perkins (I suggest you this book as you're new to NLTK). 这正是Jacob Perkins所说的“使用NLTK 2.0食谱进行Python文本处理”(我建议您这本书,因为您是NLTK的新手)。

That means that {} creates some chunks and }{ breaks up those chunks into smaller ones (ie separates them) but does NOT remove anything. 这意味着{}创建了一些块,} {将这些块分解为较小的块(即,将它们分开),但不删除任何内容。

According to you example, check out what shows 根据您的示例,查看显示了什么

result.draw()

or alternatively run 或者运行

from nltk.tree import Tree

Tree('S', [Tree('NP', [('the', 'DT'), ('little', 'JJ'), ('yellow', 'JJ'), ('dog', 'NN')]), ('barked', 'VBD'), ('at', 'IN'), Tree('NP', [('the', 'DT'), ('cat', 'NN')])]).draw()

(the above code samples show the same thing. the difference is that the first requires you initial example to run while the second does not require anything) (以上代码示例显示了相同的内容。不同之处在于,第一个示例要求您运行初始示例,而第二个示例则不需要任何操作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM