Ver Mensaje Individual
  #3 (permalink)  
Antiguo 16/04/2010, 10:02
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 8 meses
Puntos: 1360
Respuesta: [Aporte] Autómatas en python

Código Python:
Ver original
  1. #coding: utf-8
  2.  
  3. class FSM:
  4.     def __init__(self):
  5.         self.__table = {}
  6.         self.__initial_states = set()
  7.         self.__final_states = set()
  8.  
  9.     def add_transition(self, state, symbol, new_states):
  10.         """
  11.            state = string
  12.            symbol = string, len = 1
  13.            new_states = list
  14.        """
  15.         try:
  16.             self.__table[state]
  17.         except:
  18.             self.__table[state] = {}
  19.            
  20.         if type(new_states) == str:
  21.             self.__table[state][symbol] = set([new_states])
  22.         else:
  23.             self.__table[state][symbol] = set(new_states)
  24.  
  25.     def add_initial_states(self, states):
  26.         """
  27.            state = list or str
  28.        """
  29.         if type(states) == str:
  30.             self.__initial_states.update([states])
  31.         else:
  32.             self.__initial_states.update(states)
  33.  
  34.     def add_final_states(self, states):
  35.         """
  36.            states = list or str
  37.        """
  38.         if type(states) == str:
  39.             self.__final_states.update([states])
  40.         else:
  41.             self.__final_states.update(states)
  42.  
  43.     def __get_new_states_e(self, states):
  44.         visited_states_a = states.copy()
  45.         visited_states_b = set()
  46.         current_states = visited_states_a.difference(visited_states_b)
  47.         while current_states:
  48.             visited_states_b.update(visited_states_a)
  49.             for state in current_states:
  50.                 try:
  51.                     self.__table[state][""]
  52.                 except KeyError:
  53.                     pass
  54.                 else:
  55.                     visited_states_a.update(self.__table[state][""])
  56.             current_states = visited_states_a.difference(visited_states_b)
  57.         states.update(visited_states_a)
  58.  
  59.     def __get_new_states(self, states, symbol):
  60.         new_states = set()
  61.         for state in states:
  62.             try:
  63.                 self.__table[state][symbol]
  64.             except KeyError:
  65.                 pass
  66.             else:
  67.                 new_states.update(self.__table[state][symbol])
  68.         return new_states
  69.                
  70.        
  71.     def evaluate(self, string):
  72.         """
  73.            returns:
  74.                0 -> Match
  75.                1 -> No match
  76.        """
  77.         states = self.__initial_states.copy()
  78.         self.__get_new_states_e(states)
  79.         for c in string:
  80.             new_states = self.__get_new_states(states, c)
  81.             self.__get_new_states_e(new_states)
  82.             states = new_states
  83.         return bool(states.intersection(self.__final_states))
  84.  
  85.     def print_fsm(self):
  86.         print "Table:"
  87.         for state in self.__table:
  88.             for symbol in self.__table[state]:
  89.                 print "(%s, '%s') -> %s" % (state, symbol, self.__table[state][symbol])
  90.                
  91.         print "\nFinal States:"
  92.         print self.__final_states
  93.         print "\nInitial_states"
  94.         print self.__initial_states
  95.         print ""
  96.  
  97. fsm = FSM()
  98.  
  99. fsm.add_initial_states("q0")
  100.  
  101. """
  102. #Language a+b*
  103. fsm.add_transition("q0", "a", "q1")
  104. fsm.add_transition("q1", "a", "q1")
  105. fsm.add_transition("q2", "b", "q2")
  106. """
  107.  
  108. #(a* | b*)
  109. fsm.add_transition("q0", "", ["q1", "q2"])
  110. fsm.add_transition("q1", "a", "q1")
  111. fsm.add_transition("q2", "b", "q2")
  112.  
  113.  
  114. fsm.add_final_states(("q1", "q2"))
  115.  
  116. fsm.print_fsm()
  117.  
  118. print fsm.evaluate("") #True
  119. print fsm.evaluate("a") #True
  120. print fsm.evaluate("aa") #True
  121. print fsm.evaluate("aaa") #True
  122. print fsm.evaluate("aaaa") #True
  123. print fsm.evaluate("aaaaa") #True
  124. print fsm.evaluate("aaaaaa") #True
  125. print fsm.evaluate("ba") #False

Acabo de corregir las transiciones epsilon que no funcionaban :P

Última edición por razpeitia; 16/04/2010 a las 11:53