gpt4 book ai didi

algorithm - 从 CYK 算法(自然语言处理)生成解析树的步骤

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:42:01 25 4
gpt4 key购买 nike

我目前正在从事一个涉及 NLP 的项目。我已经实现了 Jurafsky 和 ​​Martin 中给出的 CKY 标识符(第 450 页的算法)。这样生成的表实际上将非终结符存储在表中(而不是通常的 bool 值)。但是,我遇到的唯一问题是检索解析树。

这是我的 CKY 标识符作用的说明:

这是我的语法

          S -> NP VP 
S -> VP
NP -> MODAL PRON | DET NP | NOUN VF | NOUN | DET NOUN | DET FILENAME
MODAL -> 'MD'
PRON -> 'PPSS' | 'PPO'
VP -> VERB NP
VP -> VERB VP
VP -> ADVERB VP
VP -> VF
VERB -> 'VB' | 'VBN'
NOUN -> 'NN' | 'NP'
VF -> VERB FILENAME
FILENAME -> 'NN' | 'NP'
ADVERB -> 'RB'
DET -> 'AT'

这是算法:

for j  from i to LENGTH(words) do
table[j-1,j] = A where A -> POS(word[j])
for i from j-2 downto 0
for k from i+1 to j-1
table[i,j] = Union(table[i,j], A such that A->BC)
where B is in table[i,k] and C is in table[k,j]

这是我的解析表在填充后的样子:

CKY Table filled as per algorithm mentioned

现在我知道由于 S 位于 [0,5] 中,字符串已被解析,并且对于 k = 1(根据 Martin 和 Jurafsky 中给出的算法),我们有 S -> table[0 ][2] 表[2][5] 即 S -> NP VP

我得到的唯一问题是我已经能够检索使用的规则,但是它们的格式很困惑,即不是基于它们在解析树中的出现。有人可以建议一种算法来检索正确的解析树吗?

谢谢。

最佳答案

您应该以递归方式访问表格的单元格,并以与处理 S 节点相同的方式展开它们,直到一切都成为终端(这样您就没有其他任何东西可以展开了)。在您的示例中,您首先转到单元格 [0][2];这是一个终端,你不需要做任何事情。接下来转到 [2][5],这是 [2][3] 和 [3][5] 制作的非终结符。您访问 [2][3],这是一个终端。 [3][5] 是一个非终端,由两个终端组成。你完成了。这是 Python 中的演示:

class Node:
'''Think this as a cell in your table'''
def __init__(self, left, right, type, word):
self.left = left
self.right = right
self.type = type
self.word = word

# Declare terminals
t1 = Node(None,None,'MOD','can')
t2 = Node(None,None,'PRON','you')
t3 = Node(None,None,'VERB', 'eat')
t4 = Node(None,None,'DET', 'a')
t5 = Node(None,None,'NOUN','flower')

# Declare non-terminals
nt1 = Node(t1,t2, 'NP', None)
nt2 = Node(t4,t5, 'NP', None)
nt3 = Node(t3,nt2,'VP', None)
nt4 = Node(nt1,nt3,'S', None)

def unfold(node):
# Check for a terminal
if node.left == None and node.right == None:
return node.word+"_"+node.type

return "["+unfold(node.left)+" "+unfold(node.right)+"]_"+node.type

print unfold(nt4)

输出:

[[can_MOD you_PRON]_NP [eat_VERB [a_DET flower_NOUN]_NP]_VP]_S

关于algorithm - 从 CYK 算法(自然语言处理)生成解析树的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com