python - NLTK:过滤具有特定结构的句子

标签 python nlp nltk

如何使用NLTK过滤特定结构的句子? 例如,我们有以下上下文无关语法的定义:

  1. S → NP VP
  2. S → Aux NP VP
  3. S → VP
  4. NP → Pronoun
  5. NP → Proper-Noun
  6. NP → Det Nominal
  7. Nominal → Noun
  8. Nominal → Nominal Noun
  9. Nominal → Nominal PP
  10.VP → Verb
  11.VP → Verb NP
  12.VP → VP PP
  13.PP → Prep NP

可以看出,定义了三种类型的句子结构:

  1. S → NP VP
  2. S → Aux NP VP
  3. S → VP

给定下面的句子,我想知道这个句子是否符合以上三个句子结构中的任何一个。

I am not much for country music but it has the potential for beauty, with its combined inclusions of comedy and sadness.

我的问题是,我应该如何使用 NLTK 来做到这一点?

最佳答案

http://www.nltk.org/book/ch05.html

应解释您为此所需的一切。基本上,你必须首先对句子进行标记(将其分解为单独的标记),然后用 nltk 识别它们的相应 PoS 来标记它们。

这会返回一个元组列表,然后有多种方法可以将这些元组与语法中的元组进行比较。

防止 future 错误链接的特定代码:

>>> 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')]

关于python - NLTK:过滤具有特定结构的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389674/

相关文章:

python - 如何从句子中提取字符ngram? - Python

Python NLTK 'LazyCorpusLoader' 对象不可调用

python - Matplotlib python 用每对 (x,y) 值的线连接两个散点图?

python - 在for循环中调用函数

python - python pg 模块导入错误

python - 拼写更正可能性

javascript - Django:防止重复对象创建的策略

machine-learning - Keras 文本预处理 - 将 Tokenizer 对象保存到文件以进行评分

python - 如何为 n-gram 训练朴素贝叶斯分类器 (movie_reviews)

python - 从 Pandas 数据框中删除停用词