我目前正在制作一个使用python的交互式系统,它能够理解和回复。因此,对于机器分析和处理有很多条件。例如。采取下面的代码(仅供参考):有没有办法删除太多的其他条件?
if ('goodbye') in message:
rand = ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0']
speekmodule.speek(rand,n,mixer)
break
if ('hello') in message or ('hi') in message:
rand = ['Wellcome to Jarvis virtual intelligence project. At your service sir.']
speekmodule.speek(rand,n,mixer)
if ('thanks') in message or ('tanks') in message or ('thank you') in message:
rand = ['You are wellcome', 'no problem']
speekmodule.speek(rand,n,mixer)
if message == ('jarvis'):
rand = ['Yes Sir?', 'What can I doo for you sir?']
speekmodule.speek(rand,n,mixer)
if ('how are you') in message or ('and you') in message or ('are you okay') in message:
rand = ['Fine thank you']
speekmodule.speek(rand,n,mixer)
if ('*') in message:
rand = ['Be polite please']
speekmodule.speek(rand,n,mixer)
if ('your name') in message:
rand = ['My name is Jarvis, at your service sir']
speekmodule.speek(rand,n,mixer)
那么,有没有一种方式,我可以取代所有这些,如果其他条件?因为条件会更多,并且会使执行速度变慢。
第一步是删除不必要的parens,即只写'if'hello''。 – skovorodkin
这是一个使用字典的解决方案:http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python – imant
是的,你可以使用建议的字典来实现一种“切换...案例...” ”。但用RegEx为了实现“消息中的单词”。但是,它不会降低算法的复杂性:您必须检查所有情况。 –