Last active
September 20, 2017 20:04
-
-
Save marcolussetti/84867ecfa31362c72b4ad8b008166cd7 to your computer and use it in GitHub Desktop.
20170920 NLP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| # # 20170920 | |
| # ## State Machine from https://www.python-course.eu/finite_state_machine.php | |
| # In[6]: | |
| class StateMachine: | |
| def __init__(self): | |
| self.handlers = {} | |
| self.startState = None | |
| self.endStates = [] | |
| def add_state(self, name, handler, end_state=0): | |
| name = name.upper() | |
| self.handlers[name] = handler | |
| if end_state: | |
| self.endStates.append(name) | |
| def set_start(self, name): | |
| self.startState = name.upper() | |
| def run(self, cargo): | |
| try: | |
| handler = self.handlers[self.startState] | |
| except: | |
| raise InitializationError("must call .set_start() before .run()") | |
| if not self.endStates: | |
| raise InitializationError("at least one state must be an end_state") | |
| while True: | |
| (newState, cargo) = handler(cargo) | |
| if newState.upper() in self.endStates: | |
| print("reached ", newState) | |
| break | |
| else: | |
| handler = self.handlers[newState.upper()] | |
| # In[7]: | |
| positive_adjectives = ["great","super", "fun", "entertaining", "easy"] | |
| negative_adjectives = ["boring", "difficult", "ugly", "bad"] | |
| # In[8]: | |
| def start_transitions(txt): | |
| splitted_txt = txt.split(None,1) | |
| word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") | |
| if word == "Python": | |
| newState = "Python_state" | |
| else: | |
| newState = "error_state" | |
| return (newState, txt) | |
| def python_state_transitions(txt): | |
| splitted_txt = txt.split(None,1) | |
| word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") | |
| if word == "is": | |
| newState = "is_state" | |
| else: | |
| newState = "error_state" | |
| return (newState, txt) | |
| def is_state_transitions(txt): | |
| splitted_txt = txt.split(None,1) | |
| word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") | |
| if word == "not": | |
| newState = "not_state" | |
| elif word in positive_adjectives: | |
| newState = "pos_state" | |
| elif word in negative_adjectives: | |
| newState = "neg_state" | |
| else: | |
| newState = "error_state" | |
| return (newState, txt) | |
| def not_state_transitions(txt): | |
| splitted_txt = txt.split(None,1) | |
| word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"") | |
| if word in positive_adjectives: | |
| newState = "neg_state" | |
| elif word in negative_adjectives: | |
| newState = "pos_state" | |
| else: | |
| newState = "error_state" | |
| return (newState, txt) | |
| def neg_state(txt): | |
| print("Hallo") | |
| return ("neg_state", "") | |
| # In[15]: | |
| m = StateMachine() | |
| m.add_state("Start", start_transitions) | |
| m.add_state("Python_state", python_state_transitions) | |
| m.add_state("is_state", is_state_transitions) | |
| m.add_state("not_state", not_state_transitions) | |
| m.add_state("neg_state", None, end_state=1) | |
| m.add_state("pos_state", None, end_state=1) | |
| m.add_state("error_state", None, end_state=1) | |
| m.set_start("Start") | |
| # In[16]: | |
| m.run("Python is great") | |
| # In[13]: | |
| m.run("Python is difficult") | |
| # In[17]: | |
| m.run("Perl is ugly") | |
| # In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment