Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

python - Is there a way to remove too many if else conditions?

I am currently making an interactive system using python, that is able to understand and reply. Hence for this there are lots of conditions for machine to analyze and process. For eg. take the following code(for reference only):

    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)

So, is there a way in which I can replace all these if else conditions?? Because there are much more conditions going to be, and it will make the execution slower.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make a exclusive "if":

if 'goodbye' in message:                          
    rand = ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0']

elif 'hello' in message or 'hi' in message:
    rand = ['Wellcome to Jarvis virtual intelligence project. At your service sir.']

elif 'thanks' in message or 'tanks' in message or ('thank you') in message:
    rand = ['You are wellcome', 'no problem']

elif message == 'jarvis':
    rand = ['Yes Sir?', 'What can I doo for you sir?']

elif  'how are you' in message or 'and you' in message or ('are you okay') in message:
    rand = ['Fine thank you']

elif  '*' in message:
    rand = ['Be polite please']

elif 'your name' in message:
    rand = ['My name is Jarvis, at your service sir']

else:
    raise NotImplementedError("What to do?")

speekmodule.speek(rand, n, mixer)

With a mapping of RegEx:

mapping = {
    r"goodbye": ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0'],
    r"hello": ['Wellcome to Jarvis virtual intelligence project. At your service sir.'],
    ...}

for regex, rand in mapping.items():
    if re.search(message, flags=re.I):
        break
else:
    raise NotImplementedError("What to do?")
speekmodule.speek(rand, n, mixer)

It's up to you to decide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...