NLTK:从树值转换为Lambda函数表示法

2024-05-16 08:49:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一个句子转换成lambda函数表示法,比如在4.2节中看到的http://www.nltk.org/book/ch10.html。使用代码,我可以将句子转换成语义值,并使用上下文无关语法分析句子,但是有哪些方法可以将句子转换成lambda符号。在

import nltk
grammar = nltk.CFG.fromstring("""
  S -> NP VP
  VP -> V NP 
  V -> "is" 
  Adj -> "Chewing"
  Coj -> "and"
  Nom -> Adj Nom | N
  NP ->  Nom Coj NP | N
  Det -> "a" | "an" | "the" | "my"
  N -> "gum" | "candy" | "thrilling"
  P -> "in" | "on" | "by" | "with"
  """)
sent = "Chewing gum and candy is thrilling".split()
rd_parser = nltk.RecursiveDescentParser(grammar, )
for tree in rd_parser.parse(sent):
    print (tree)

Tags: andlambdaisnpnom句子nltkgum
1条回答
网友
1楼 · 发布于 2024-05-16 08:49:10

简答:你必须编写自己的语义语法。要将CFG映射到lambda演算,没有简单的方法。在

较长的答案

在CFG中,最接近语义树的是为非终端指定语义特性,sqlcfg示例是如何实现这一点的一个很好的指南,请参见第1.1节。来自http://www.nltk.org/book/ch10.html

>>> nltk.data.show_cfg('grammars/book_grammars/sql0.fcfg')
% start S
S[SEM=(?np + WHERE + ?vp)] -> NP[SEM=?np] VP[SEM=?vp]
VP[SEM=(?v + ?pp)] -> IV[SEM=?v] PP[SEM=?pp]
VP[SEM=(?v + ?ap)] -> IV[SEM=?v] AP[SEM=?ap]
NP[SEM=(?det + ?n)] -> Det[SEM=?det] N[SEM=?n]
PP[SEM=(?p + ?np)] -> P[SEM=?p] NP[SEM=?np]
AP[SEM=?pp] -> A[SEM=?a] PP[SEM=?pp]
NP[SEM='Country="greece"'] -> 'Greece'
NP[SEM='Country="china"'] -> 'China'
Det[SEM='SELECT'] -> 'Which' | 'What'
N[SEM='City FROM city_table'] -> 'cities'
IV[SEM=''] -> 'are'
A[SEM=''] -> 'located'
P[SEM=''] -> 'in'

将CFG映射到lambda演算,反之亦然,或者从文本中学习两者仍然是一项值得研究的工作,因此目前还没有明确的方法来做。在

http://dl.acm.org/citation.cfm?id=2052254http://www.computer.org/csdl/proceedings/ictai/2008/3440/02/3440b135-abs.html

相关问题 更多 >