【发布时间】:2020-11-30 05:05:36
【问题描述】:
编辑:错误是这一行if len(rhs) == 2 and rhs[0] in T[i][k] and rhs[1] in T[k + 1][j]:
我能够使用一小组规则、终端和非终端来实现基于 cky 解析器 wiki 的 cky 算法。但我对其进行了缩放以拥有更多的规则、单词、语法,现在它给了我
IndexError: list index out of range
有谁知道我在使用更大的语法集时做错了什么?
如果有帮助,这里是以前的较小规模的语法。
non_terminals = ["NP", "Nom", "Det", "AP",
"Adv", "A"]
terminals = ["book", "orange", "man",
"tall", "heavy",
"very", "muscular"]
# Rules of the grammar
R = {
"NP": [["Det", "Nom"]],
"Nom": [["AP", "Nom"], ["book"],
["orange"], ["man"]],
"AP": [["Adv", "A"], ["heavy"],
["orange"], ["tall"]],
"Det": [["a"]],
"Adv": [["very"], ["extremely"]],
"A": [["heavy"], ["orange"], ["tall"],
["muscular"]]
}
这是我的功能
def cykParse(w): n = 长度(w)
# Initialize the table
T = [[set([]) for j in range(n)] for i in range(n)]
# Filling in the table
for j in range(0, n):
# Iterate over the rules
for lhs, rule in R.items():
for rhs in rule:
# If a terminal is found
if len(rhs) == 1 and rhs[0] == w[j]:
T[j][j].add(lhs)
for i in range(j, -1, -1):
# Iterate over the range i to j + 1
for k in range(i, j + 1):
# Iterate over the rules
for lhs, rule in R.items():
for rhs in rule:
# If a terminal is found
if len(rhs) == 2 and rhs[0] in T[i][k] and rhs[1] in T[k + 1][j]:
T[i][j].add(lhs)
# If word can be formed by rules
# of given grammar
if len(T[0][n-1]) != 0:
print("True")
else:
print("False")
【问题讨论】:
标签: python algorithm parsing nlp automata-theory