python 状态机语句"Python is fun"褒贬义判断的两种实现方式
来源:互联网 发布:21天学通java第7版下载 编辑:程序博客网 时间:2024/05/21 05:22
0、
背景:思考下怎么对一句话如python is bad判断出这是贬义。
版本:
pip install transitions==0.6.1
python == 2.7.10
1、
网上教程大部分如:http://www.cnblogs.com/21207-iHome/p/6085334.html 这篇博客上的写法。
# coding=utf-8############################################################### 代码来源: https://www.python-course.eu/finite_state_machine.php# 目标:输入一段str如:python is good.# 判断出这句话是褒义还是贬义的。##############################################################class StateMahine(object): def __init__(self): """ 有限状态机是一个五元组M=(Q,Σ,δ,q0,F),其中: Q = {q0,q1,...,qn}是有限状态集合。在任一确定的时刻,有限状态机只能处于一个确定的状态qiqi; Σ = {σ1,σ2,...,σn}是有限输入字符集合。在任一确定的时刻,有限状态机只能接收一个确定的输入σjσj; δ: Q×Σ→Q是状态转移函数,在某一状态下,给定输入后有限状态机将转入状态迁移函数决定的一个新状态; q0 ∈ Q是初始状态,有限状态机由此状态开始接收输入; F ⊆ Q是最终状态集合,有限状态机在达到终态后不再接收输入。 """ self.handlers = {} # 这就是状态转移函数δ self.startState = None # 初始状态q0 self.endStates = [] # 最终状态集合F 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 Exception("must call .set_start() before .run()") if not self.endStates: raise Exception("at least one state must be an end_state") while 1: newState, cargo = handler(cargo) if newState.upper() in self.endStates: print ("Reached ", newState) break else: handler = self.handlers[newState.upper()]positive_adjectives = ["great", "super", "fun", "entertraining", "easy"]negative_adjectives = ["boring", "difficult", "ugly", "bad"]def start_transitions(txt): """ s = "python is good" s.split(None, 1) ['python', 'is good'] """ 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)if __name__ == '__main__': m = StateMahine() 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") m.run("Python is great") m.run("Python is not great") m.run("Python is not fun") m.run("Python is ugly") m.run("Pythoniseasy") m.run("Python is easy1")"""结果:Reached pos_stateReached neg_stateReached neg_stateReached neg_stateReached error_stateReached error_state采用这种算法结构对每一个状态进行封装处理,逻辑清晰。"""
2、
但其实我们研究了这个transitions库,理论上也可以实现的吧。 研究的博客:点击打开链接
# coding=utf-8############################################################### 目标:判断"Python is bad"这句话的褒贬义# 环境: pip install transitions# 这个库简直碉堡了...# by ouyang 2017年10月10日 星期二##############################################################from transitions import Machineclass Matter(object): def __init__(self, words, positive_adjectives, negative_adjectives): self.original_s = self.s = words # 保留原始的字符串 self.positive_adjectives = positive_adjectives self.negative_adjectives = negative_adjectives self.result = None # 结果保存 def on_enter_Start(self): splitted_txt = self.s.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (self.s, "") if word == 'Python': self.next_state = self.to_Python else: self.next_state = self.to_error self.s = txt def on_enter_Python(self): splitted_txt = self.s.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (self.s, "") if word == 'is': self.next_state = self.to_is else: self.next_state = self.to_error self.s = txt def on_enter_is(self): splitted_txt = self.s.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (self.s, "") if word == "not": self.next_state = self.to_not elif word in self.positive_adjectives: self.next_state = self.to_positive elif word in self.negative_adjectives: self.next_state = self.to_negative else: self.next_state = self.to_error self.s = txt def on_enter_not(self): splitted_txt = self.s.split(None, 1) word, txt = splitted_txt if len(splitted_txt) > 1 else (self.s, "") if word in self.positive_adjectives: self.next_state = self.to_negative elif word in self.negative_adjectives: self.next_state = self.to_positive else: self.next_state = self.to_error self.s = txt def on_enter_negative(self): self.result = "Negative!!!" self.next_state = None def on_enter_positive(self): self.result = "Positive!!!" self.next_state = None def on_enter_error(self): self.result = "Error!!!" self.next_state = None def run(self): while self.next_state: print "now state = %s!" % self.state self.next_state()if __name__ == '__main__': words_list = ["Python is not bad", "Python is bad", "Python is fun", "Python is not fun", "Python1 is bad", "Python is1 bad", "Python is bad111"] for words in words_list: print "#" * 50 positive_adjectives = ["great", "super", "fun", "entertraining", "easy"] negative_adjectives = ["boring", "difficult", "ugly", "bad"] model = Matter(words, positive_adjectives, negative_adjectives) # 定义状态 states = ["Start", "Python", "is", "not", "positive", "negative", "error"] # 初始化状态机 machine = Machine(model=model, states=states, ordered_transitions=True) # 计算结果 model.run() print model.original_s, "=", model.result结果:
##################################################now state = initial!now state = Start!now state = Python!now state = is!now state = not!Python is not bad = Positive!!!##################################################now state = initial!now state = Start!now state = Python!now state = is!Python is bad = Negative!!!##################################################now state = initial!now state = Start!now state = Python!now state = is!Python is fun = Positive!!!##################################################now state = initial!now state = Start!now state = Python!now state = is!now state = not!Python is not fun = Negative!!!##################################################now state = initial!now state = Start!Python1 is bad = Error!!!##################################################now state = initial!now state = Start!now state = Python!Python is1 bad = Error!!!##################################################now state = initial!now state = Start!now state = Python!now state = is!Python is bad111 = Error!!!
相关的demo放到:https://github.com/emaste-r/state_demo
如果觉得可以的话,帮忙去github点个star哟~
以上
阅读全文
0 0
- python 状态机语句"Python is fun"褒贬义判断的两种实现方式
- python多线程的两种实现方式
- 【Python】python 多线程两种实现方式
- 读卡器的状态机, python实现
- 飘逸的python - 两种with语句实现方法
- 飘逸的python - 两种with语句实现方法
- Python的判断语句
- Python判断语句中‘is’和‘==’的区别
- python判断语句is与==的区别
- python中package机制的两种实现方式
- python中package机制的两种实现方式
- [转载]python中package机制的两种实现方式
- 快速排序quick_sort(python的两种实现方式)
- python 多线程两种实现方式
- Python导入模块(包)的两种方式 TypeError: 'module' object is not callable
- Python线程编程的两种方式
- Python线程编程的两种方式
- Python线程编程的两种方式
- Hive编写UDF函数
- C语言二级指针做函数参数改变该指针的指向
- Android 中的Looper如何实现阻塞与唤醒的
- memcmp的问题
- Spring Boot访问mysql(JPA方式)最简单配置
- python 状态机语句"Python is fun"褒贬义判断的两种实现方式
- Unity NGUI UITexture 图片替换
- Atlantis HDU
- axis2 webservice 接口请求(json对象组装参数请求(用于对象请求)、多个参数请求、阿里巴巴json工具使用)
- SourceTree冲突解决
- 根据城市名获取天气
- Effective Java中文第四章第13节(个人渣翻)
- sublimeText3如何配置以支持jsx语法
- 机器学习随记1-逻辑回归(Logistic Regression)-1