Warm tip: This article is reproduced from serverfault.com, please click

其他-CYK算法的Python实现

(其他 - Python implementation for the CYK Algorithm)

发布于 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 = len(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") 
Questioner
John Baltimore
Viewed
0
rici 2020-11-30 23:07:17

我猜(因为你没有显示表明错误发生在哪里的实际错误),它在这一行中:

if len(rhs) == 2 and rhs[0] in T[i][k] and rhs[1] in T[k + 1][j]:

k就是n-1如果前两个条件为真,则第三个条件将执行并爆炸。

我怀疑的迭代限制中存在一个错误的错误k一些代码注释会很有用,或者至少是对实现所基于的伪代码的引用。