2013-02-21 55 views
-4

我正在尝试使用NLTK来检查给定的句子是否是语法使用NLTK中的POS标签的CFG

例:

OK:鲸鱼舔悲伤

不正常:最好的我曾经

我知道我可以做词性标注,然后用CFG解析器和检查方式,但我还没有找到使用POS标记而不是实际词作为终端分支的CFG。

是否有任何人可以推荐的CFG?我认为制作我自己的作品是很愚蠢的,因为我不是语言学家,可能会遗漏重要的结构。

此外,我的应用程序是这样的系统将理想地拒绝许多句子,只批准句子,它是非常确定的。

感谢:d

+0

您是否看到过这个相关的StackOverflow讨论? http://stackoverflow.com/questions/10252448/how-to-check-whether-a-sentence-is-correct-simple-grammar-check-in-python – stepthom 2013-02-21 18:33:28

回答

3

的CFG的终端节点可以是任何东西,甚至POS标签。只要你的短语规则识别POS而不是单词作为输入,用POS来声明语法应该没有问题。

import nltk 
# Define the cfg grammar. 
grammar = nltk.parse_cfg(""" 
S -> NP VP 
NP -> 'DT' 'NN' 
VP -> 'VB' 
VP -> 'VB' 'NN' 
""") 


# Make your POS sentence into a list of tokens. 
sentence = "DT NN VB NN".split(" ") 

# Load the grammar into the ChartParser. 
cp = nltk.ChartParser(grammar) 

# Generate and print the nbest_parse from the grammar given the sentence tokens. 
for tree in cp.nbest_parse(sentence): 
    print tree 
+0

要从句子中获取POS标签,英语有很多POS标签。例如http://code.google.com/p/hunpos/ – alvas 2013-02-23 02:03:29

+0

我知道如何获得POS标签,但是如何获得使用POS标签作为终端的英语语言的CFG? – Sam 2013-02-23 21:31:12

+0

POS标签语料库中的每个句子,取出最频繁发生的POS模式。 – alvas 2013-02-24 01:38:15