Is There A Way To Remove Too Many If Else Conditions?
Solution 1:
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"\bgoodbye\b": ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0'],
r"\bhello\b": ['Wellcome to Jarvis virtual intelligence project. At your service sir.'],
...}
for regex, rand in mapping.items():
if re.search(message, flags=re.I):
breakelse:
raise NotImplementedError("What to do?")
speekmodule.speek(rand, n, mixer)
It's up to you to decide.
Solution 2:
if/elif/else
is a natural way to structure this kind of code in Python. As @imant noted, you may use dict-based approach in case of simple branching, but I see some mildly complex logic in your if
predicates, so you'll have to check all predicates in any case and you won't have any performance gains with another code structure.
Though it may be a little bit more readable and easier to maintain if you factor out your predicates and actions like this:
from collections import OrderedDict
defgoodbye_p(message):
return'goodbye'in message
defgoodbye_a():
rand = ['Goodbye Sir', 'Jarvis powering off in 3, 2, 1, 0']
# As @Moinuddin Quadri I also assume that your `speek` method# says random message from a list.# Otherwise you can use `random.choice` method # to get a random message out of a list: `random.choice(messages)`.
speekmodule.speek(rand, n, mixer)
defhello_p(message):
return'hello'in message or'hi'in message
defhello_a():
rand = ['Wellcome to Jarvis virtual intelligence project. At your service sir.']
speekmodule.speek(rand, n, mixer)
# Use `OrderedDict` instead of `dict` to control order# of checks and actions.
branches = OrderedDict([
# (predicate as key, action as value)
(goodbye_p, goodbye_a),
(hello_p, hello_a),
])
for predicate, action in branches.items():
if predicate(message):
action_result = action()
# You can add some logic here based on action results.# E.g. you can return some special object from `goodbye_a`# and then shut down Jarvis here.# Or if your actions are exclusive, you can add `break` here.
If all your predicates are the same and contain only substring checks, then it may be more performant to have tuples (e.g. ('hello', 'hi')
) as dict keys. Then you can iterate over those keys like this:
for words, action in branches.items():
if any(word in message for word in words):
action()
Solution 3:
Use dictionary:
someCollections = {
'goodbye': "Something1",
'hello': "Somthing2",
...
}
speekmodule(someCollections [SomeKey],...)
Solution 4:
Firstly create a dict
object with the key as tuple
of string you want to match in your message
and associate it with the value string
which your Jarvis is suppose to respond. For example:
jarvis_dict = {
('goodbye',) : ['Goodbye Sir', 'Jarvis powering off in3, 2, 1, 0'],
('hello',
'hi') : ['Wellcome to Jarvis virtual intelligence project. At your service sir.'],
('thanks',
'tanks',
'thank you') : ['You are wellcome', 'no problem'],
('jarvis',) : ['Yes Sir?', 'What can I doo foryou sir?'],
('how are you',
'and you',
'are you okay'): ['Fine thank you'],
('*',) : ['Be polite please'],
('your name',): ['My name is Jarvis, at your service sir']
}
Now iterate each key of you dict
to check whether any sub-string is the part of the message and if there is any match, call the speekmodule.speek(rand,n,mixer)
function as:
for key, value in jarvis_dict.items():
if any(item in message for item in key):
speekmodule.speek(value, n, mixer)
Note: Here I am assuming that speekmodule.speek(value, n, mixer)
in your code is working as there is no information available in your code regarding there declaration. I just replaced your rand
with value
as it the same list of str
returned by the dict
which is used in your code.
Post a Comment for "Is There A Way To Remove Too Many If Else Conditions?"