为什么我的代码通过广度优先搜索解决单词错误率不起作用?

问题描述 投票:0回答:0

这是我关于广度优先搜索计算单词错误率的代码[enter image description here](https://i.stack.imgur.com/SV1ow.png)

class FIFOQueue :
   def __init__(self) -> None:
       self.__queue = []

   def append(self,item_in) :
       self.__queue.append(item_in)
   
   def extend(self,old_queue):
       self.__queue.extend(old_queue) 

   def pop(self):
       return self.__queue.pop(0)  # It will be return node

   def is_empty(self) :
       return self.__queue ==[]

class TreeNode: 
   def __init__(self,state, step = 0 ,parent=None) -> None:
       self.state = tuple(state)
       self.step = step
       self.parent = parent
       
   def get_state(self):
       return self.state

   def get_parent(self) :
       return self.parent


class RNode(TreeNode) :
   def __init__(self, state, step = 0, parent=None) -> None:
       super().__init__(state, step, parent)
   
   def expand(self,rp) :
       child_list = []
       for new_state in rp.adjacent_states(self.state, self.step) :
           child_list.append(RNode(new_state, self.step+1, self))
           for i in child_list :
               print(i.get_state())

       return child_list 

class RoutingProb :  #change
   def __init__(self,initial,destination) -> None:
       self.initial = initial
       self.destination = destination

       des = ''
       for i in destination :
           des += i

       self.des_str = des

   def is_destination(self,state) :
       check_state = ''
       for i in state : 
           if i is None :
               continue
           else :
               check_state += i
       return self.des_str == check_state

   def adjacent_states(self,state, step) : 
       return RoutingProb.add_action(self, list(state), step, self.destination)
           
   def add_action(self, state_li, step, des) :
       print(step)
       print(state_li)
       if state_li[step] == des[step]:
           print('pass')
           print(state_li)
           return [state_li]

       if len(state_li) > len(des) : #del or sub
           
           state_li2 = state_li[:]
           state_li[step] =  None  #del
           state_li2[step] = des[step] #sub
           # print('pass2')
           return [state_li, state_li2]

       elif len(state_li) < len(des) : # ins or sub
           state_li2 = state_li[:]
           state_li.insert(step, des[step]) #ins
           state_li2[step] = des[step] #sub
           # print('pass3')
           return [state_li, state_li2]

       else : # len(state_li) = len(des) only sub
           #print('pass4')
           state_li[step] = des[step]
           return [state_li]
    
   
def breadth_first_search(prob):
   fringe = FIFOQueue()
   fringe.append(RNode(prob.initial))

   reaeched ={} 
   while not fringe.is_empty():
       node = fringe.pop()
       # print(node.state)
       if prob.is_destination(node.state) :
           print('xx')
           return node

       if node.state not in reaeched : 
           reaeched[node.state] = node
           print(reaeched)
           fringe.extend(node.expand(prob))
           print('x')

def P5_wer(ref,test):
   ref_list = [i for i in ref]
   test_list = [i for i in test]

   rPorb = RoutingProb(test_list,ref_list)
   leave_node = breadth_first_search(rPorb)
   # print('leave=',leave_node)

   x, sol_path = leave_node, [leave_node]
   # print('x=',x.get_state)
   while x.get_parent() is not None :
       sol_path.append(x.get_parent())
       x = x.get_parent()
    
if __name__ == '__main__':
   wer, n = P5_wer("grit", "greet")
   print("wer = {}, n = {}".format(wer, n))

这是我在 Introduction to machine learning 中关于搜索的作业,但这是最糟糕的课程,因为 他们没有教我数据结构和面向对象编程等基础知识。我尽力了 我不知道为什么它不起作用

python machine-learning search
© www.soinside.com 2019 - 2024. All rights reserved.