【问题标题】:Context-free grammar for Greek希腊语的上下文无关语法
【发布时间】:2015-01-02 10:03:38
【问题描述】:

我想使用nltk 为希腊语创建一个非常简单的上下文无关语法。我在 Windows 上运行 Python 2.7。

这是我的代码:

# -*- coding: utf-8 -*-
import nltk
grammar = nltk.CFG.fromstring("""
            S -> Verb Noun
            Verb -> a
            Noun -> b
            """)
a="κάνω"
b="ποδήλατο"

user_input = "κάνω ποδήλατο"

如何判断user_input 在语法上是否正确?我试过了:

sent =  user_input.split()
parser = nltk.ChartParser(grammar)
for tree in parser.parse(sent):
        print tree

但我收到以下错误,该错误发生在grammar.py 文件(第 632 行)中,与 nltk 一起出现:

ValueError: Grammar does not cover some of the input words: u"'\\xce\\xba\\xce\\xac\\xce\\xbd\\xcf\\x89', '\\xcf\\x80\\xce\\xbf\\xce\\xb4\\xce\\xae\\xce\\xbb\\xce\\xb1\\xcf\\x84\\xce\\xbf'".

我只有在使用 for 循环时才会收到错误消息。在那之前,我没有收到任何错误。所以我想这是某种我不知道如何克服的编码问题。

【问题讨论】:

    标签: python parsing nltk grammar


    【解决方案1】:

    首先,如果您使用nltk.CFG.fromstring,则必须将非终结符,即词典中的单词直接声明到CFG语法中:

    import nltk
    grammar = nltk.CFG.fromstring(u"""
                S -> Verb Noun
                Verb -> "κάνω"
                Noun -> "ποδήλατο"
                """)
    parser = nltk.ChartParser(grammar)
    print parser.grammar()
    

    [出]:

    Grammar with 3 productions (start state = S)
        S -> Verb Noun
        Verb -> '\u03ba\u03ac\u03bd\u03c9'
        Noun -> '\u03c0\u03bf\u03b4\u03ae\u03bb\u03b1\u03c4\u03bf'
    

    现在我们看看你的user_input

    >>> print ["κάνω ποδήλατο"]
    ['\xce\xba\xce\xac\xce\xbd\xcf\x89 \xcf\x80\xce\xbf\xce\xb4\xce\xae\xce\xbb\xce\xb1\xcf\x84\xce\xbf']
    

    您意识到字符串在 python 2.x 中被读取为字节码,但在 python 3.x 中,默认情况下它会是 utf8。现在在我们将其解码为 utf8 时查看它:

    >>> print ["κάνω ποδήλατο".decode('utf8')]
    [u'\u03ba\u03ac\u03bd\u03c9 \u03c0\u03bf\u03b4\u03ae\u03bb\u03b1\u03c4\u03bf']
    

    请注意,在硬编码某些变量时,u"κάνω ποδήλατο" 在显式解码字符串时与 "κάνω ποδήλατο".decode('utf8')` 具有相同的效果。

    现在看起来像nltk.CFG.fromstring() 读取语法的方式:

    # -*- coding: utf-8 -*-
    
    import nltk
    grammar = nltk.CFG.fromstring(u"""
                S -> Verb Noun
                Verb -> "κάνω"
                Noun -> "ποδήλατο"
                """)
    parser = nltk.ChartParser(grammar)
    
    user_input = u"κάνω ποδήλατο".split()
    sent = user_input
    parser = nltk.ChartParser(grammar)
    
    for tree in parser.parse(sent):
        print tree
    

    [出]:

    (S (Verb \u03ba\u03b1\u03bd\u03c9) (Noun \u03c0\u03bf\u03b4\u03b7\u03bb\u03b1\u03c4\u03bf))
    

    但我不确定你是否看到输出有些奇怪,它不完全是 unicode,而是 unicode 字节表示:

    >>> x = '\u03ba\u03b1\u03bd\u03c9'
    >>> print x
    \u03ba\u03b1\u03bd\u03c9
    >>> print x.decode('utf8')
    \u03ba\u03b1\u03bd\u03c9
    >>> print x.encode('utf8')
    \u03ba\u03b1\u03bd\u03c9
    >>> x = u'\u03ba\u03b1\u03bd\u03c9'
    >>> print x
    κανω
    

    您需要这样做才能检索原始 unicode(感谢@Kasra,请参阅 How to retrieve my unicode from the unicode byte representation ):

    >>> s='\u03ba\u03b1\u03bd\u03c9'
    >>> print unicode(s,'unicode_escape')
    κανω
    

    【讨论】:

    • 它一直有效,直到我必须检索原始 unicode。我使用 Sublime Text 3,我得到 UnicodeEncodeError。不过,如果我在 IDLE 中编写代码,它就可以工作。
    • 可能是因为默认的编码。看看这个:stackoverflow.com/questions/27659861/…。我假设输入数据将使用 raw_input 通过标准输入输入。建议:使用python 3。你可以从Sublime发布错误回溯吗?
    猜你喜欢
    • 2012-01-04
    • 2014-04-26
    • 2013-02-23
    • 1970-01-01
    • 2011-07-03
    • 2017-05-19
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多