NLTK语法解析错误价值eRROR:无法解析行24

发布于 2025-01-25 14:15:02 字数 3507 浏览 4 评论 0原文

我正在使用Python进行自然语言处理 - 使用自然语言工具包分析文本,我正在第10章,练习1:

    >Select three or four contiguous sentences from a book for children. A possible source of examples >are the collections of stories in nltk.corpus.gutenberg: bryant-stories.txt, burgess->busterbrown.txt and edgeworth-parents.txt. Develop a grammar which will allow your sentences to be 
    >translated into first order logic, and build a model which will allow those translations to be >checked for truth or falsity.

我正在尝试与Burgess-Busterbrown.txt corpus中的以下句子一起工作

    >Little Joe Otter was in a terrible rage. It was a bad beginning for a
    >beautiful day and Little Joe knew it. But who wouldn't be in a rage if
    >his breakfast was taken from him just as he was about to eat it? Anyway,
    >that is what Little Joe told Billy Mink.

:遵循FCFG语法文件&解析代码:

    % start S
    S[SEM=(?vp + '(' + ?nps + ',' + ?npo + ')')] -> CONJ[SEM=?conj] NP[SEM=?nps] VP[SEM=?vp] NP[SEM=?npo]
    S[SEM=(?vp + '(' + ?nps + ')')] -> CONJ[SEM=?conj] NP[SEM=?nps] VP[SEM=?vp] AdvP[SEM=?advp]
    S[SEM=(?vp + '(' + ?nps + ',' + ?npo + ')')] -> NP[SEM=?nps] VP[SEM=?vp] NP[SEM=?npo]
    S[SEM=(?vp + '(' + ?nps + ',' + ?s + ')')] -> CONJ[SEM=?conj] NP[SEM=?nps] VP[SEM=?vp] S[SEM=?s]
    S[SEM=(?vp + '(' + ?nps + ',' + ?s + ')')] -> NP[SEM=?nps] VP[SEM=?vp] S[SEM=?s]
    S[SEM=(?vp + '(boy)')] -> P[SEM=?p] VP[SEM=?vp] AdvP[SEM=?advp]
    S[SEM=(?vp + '(boy,' + ?np +')')] -> P[SEM=?p] VP[SEM=?vp] NP[SEM=?np]
    S[SEM=(?s1 + ?conj + ?s2)] -> S[SEM=?s1] CONJ[SEM=?conj] S[SEM=?s2]
    S[SEM=(?vp + '(' + ?np + ')')] -> AdvP[SEM=?advp] AdvP[SEM=?advp] VP[SEM=?vp] NP[SEM=?np]
    
    VP[SEM=?v] -> V[SEM=?v]
    VP[SEM=?v] -> Aux[SEM=?aux] V[SEM=?v]
    NP[SEM=?n] -> N[SEM=?n]
    NP[SEM=?n] -> DET[SEM=?det] N[SEM=?n]
    NP[SEM=?n] -> DET[SEM=?det] Adj[SEM=?adj] N[SEM=?n]
    
    AdvP[SEM=''] -> P[SEM=?p] P[SEM=?p] NP[SEM=?np]
    AdvP[SEM=''] -> Adv[SEM=?adv]
    
    
    CONJ[SEM='&'] -> 'and'
    CONJ[SEM='->'] -> 'So'
    P[SEM=?p] -> 'a' | 'for' | 'wouldn't' | 'if' | 'from' | 'as' | 'to' |'Anyway'
    Adv[SEM=''] -> 'about'
    Adv[SEM=''] -> 'just'
    Adv[SEM=''] -> 'that'
    Adj[SEM=''] -> 'little'
    Adj[SEM=''] -> 'terrible'
    Adj[SEM=''] -> 'bad'
    Adj[SEM=''] -> 'beautiful'
    DET[SEM=''] -> 'a' | 'the'
    N[SEM='Joe'] -> 'Joe' | 'he' |'Otter' |'him' | 'his'
    N[SEM='Billy'] -> 'Bilyu' |'Mink'
    N[SEM='rage'] -> 'rage'
    N[SEM='beginning'] -> 'beginning'
    N[SEM='day'] -> 'day'
    N[SEM='breakfast'] -> 'breakfast' | 'it'
    V[SEM='be'] -> 'was' | 'be'
    V[SEM='in'] -> 'in'
    V[SEM='knew'] -> 'knew'
    V[SEM='take'] -> 'taken'
    V[SEM='eat'] -> 'eat'
    V[SEM='tell'] -> 'told'


    parser = load_parser('grammar_test.fcfg')
    sentence = 'Little Joe Otter was in a terrible rage'
    tokens = sentence.split()
    trees = list(parser.parse(tokens))
    for tree in trees:
       print (tree.label()['SEM'])

我会收到以下错误:

    > ValueError: Unable to parse line 24: P[SEM=?p] -> 'a' | 'for' | 'wouldn't' | 'if' | 'from' | >'as' | 'to' |'Anyway' Unterminated string
    

诚然,我不太擅长理解语法部分,我使用其他句子从其他人那里调整了其他问题的语法部分,以获取相同的问题。我在做什么错或失踪?我如何正确地进入一阶逻辑?我尝试了其他版本的语法,但是它们给出了“语法不涵盖某些输入单词”错误,这比上述错误更接近,但无论哪种方式都没有起作用。

感谢任何帮助。

I'm working through Natural Language Processing with Python – Analyzing Text with the Natural Language Toolkit and I'm on chapter 10, Exercise 1:

    >Select three or four contiguous sentences from a book for children. A possible source of examples >are the collections of stories in nltk.corpus.gutenberg: bryant-stories.txt, burgess->busterbrown.txt and edgeworth-parents.txt. Develop a grammar which will allow your sentences to be 
    >translated into first order logic, and build a model which will allow those translations to be >checked for truth or falsity.

I'm trying to work with the following sentences in the burgess-busterbrown.txt corpus:

    >Little Joe Otter was in a terrible rage. It was a bad beginning for a
    >beautiful day and Little Joe knew it. But who wouldn't be in a rage if
    >his breakfast was taken from him just as he was about to eat it? Anyway,
    >that is what Little Joe told Billy Mink.

with the following fcfg grammar file & parsing code:

    % start S
    S[SEM=(?vp + '(' + ?nps + ',' + ?npo + ')')] -> CONJ[SEM=?conj] NP[SEM=?nps] VP[SEM=?vp] NP[SEM=?npo]
    S[SEM=(?vp + '(' + ?nps + ')')] -> CONJ[SEM=?conj] NP[SEM=?nps] VP[SEM=?vp] AdvP[SEM=?advp]
    S[SEM=(?vp + '(' + ?nps + ',' + ?npo + ')')] -> NP[SEM=?nps] VP[SEM=?vp] NP[SEM=?npo]
    S[SEM=(?vp + '(' + ?nps + ',' + ?s + ')')] -> CONJ[SEM=?conj] NP[SEM=?nps] VP[SEM=?vp] S[SEM=?s]
    S[SEM=(?vp + '(' + ?nps + ',' + ?s + ')')] -> NP[SEM=?nps] VP[SEM=?vp] S[SEM=?s]
    S[SEM=(?vp + '(boy)')] -> P[SEM=?p] VP[SEM=?vp] AdvP[SEM=?advp]
    S[SEM=(?vp + '(boy,' + ?np +')')] -> P[SEM=?p] VP[SEM=?vp] NP[SEM=?np]
    S[SEM=(?s1 + ?conj + ?s2)] -> S[SEM=?s1] CONJ[SEM=?conj] S[SEM=?s2]
    S[SEM=(?vp + '(' + ?np + ')')] -> AdvP[SEM=?advp] AdvP[SEM=?advp] VP[SEM=?vp] NP[SEM=?np]
    
    VP[SEM=?v] -> V[SEM=?v]
    VP[SEM=?v] -> Aux[SEM=?aux] V[SEM=?v]
    NP[SEM=?n] -> N[SEM=?n]
    NP[SEM=?n] -> DET[SEM=?det] N[SEM=?n]
    NP[SEM=?n] -> DET[SEM=?det] Adj[SEM=?adj] N[SEM=?n]
    
    AdvP[SEM=''] -> P[SEM=?p] P[SEM=?p] NP[SEM=?np]
    AdvP[SEM=''] -> Adv[SEM=?adv]
    
    
    CONJ[SEM='&'] -> 'and'
    CONJ[SEM='->'] -> 'So'
    P[SEM=?p] -> 'a' | 'for' | 'wouldn't' | 'if' | 'from' | 'as' | 'to' |'Anyway'
    Adv[SEM=''] -> 'about'
    Adv[SEM=''] -> 'just'
    Adv[SEM=''] -> 'that'
    Adj[SEM=''] -> 'little'
    Adj[SEM=''] -> 'terrible'
    Adj[SEM=''] -> 'bad'
    Adj[SEM=''] -> 'beautiful'
    DET[SEM=''] -> 'a' | 'the'
    N[SEM='Joe'] -> 'Joe' | 'he' |'Otter' |'him' | 'his'
    N[SEM='Billy'] -> 'Bilyu' |'Mink'
    N[SEM='rage'] -> 'rage'
    N[SEM='beginning'] -> 'beginning'
    N[SEM='day'] -> 'day'
    N[SEM='breakfast'] -> 'breakfast' | 'it'
    V[SEM='be'] -> 'was' | 'be'
    V[SEM='in'] -> 'in'
    V[SEM='knew'] -> 'knew'
    V[SEM='take'] -> 'taken'
    V[SEM='eat'] -> 'eat'
    V[SEM='tell'] -> 'told'


    parser = load_parser('grammar_test.fcfg')
    sentence = 'Little Joe Otter was in a terrible rage'
    tokens = sentence.split()
    trees = list(parser.parse(tokens))
    for tree in trees:
       print (tree.label()['SEM'])

I get the following error:

    > ValueError: Unable to parse line 24: P[SEM=?p] -> 'a' | 'for' | 'wouldn't' | 'if' | 'from' | >'as' | 'to' |'Anyway' Unterminated string
    

Admittedly, I'm not very good at understanding the grammar portion and I adapted parts of the grammar file from others for the same question using different sentences. What am I doing wrong or missing? How do I get it do properly get into the first order logic? I've tried other versions of a grammar, but they gave "Grammar does not cover some of the input words" errors, which feels closer than the above error, but either way, neither worked.

Appreciate any help on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文