有没有办法消除过多的if-else条件?

2024-10-03 06:20:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用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)

那么,有没有一种方法可以替换所有这些if-else条件??因为会有更多的条件,这会使执行变慢。你知道吗


Tags: orinyoumessageyourif条件are
3条回答

使用字典:

someCollections = {
    'goodbye': "Something1",
    'hello': "Somthing2",
    ...
}

speekmodule(someCollections [SomeKey],...)

排他性的“如果”:

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)

使用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):
        break
else:
    raise NotImplementedError("What to do?")
speekmodule.speek(rand, n, mixer)

由你来决定。你知道吗

首先创建一个dict对象,其键是tuple字符串,您希望在message中匹配该字符串,并将其与Jarvis应该响应的值string相关联。例如:

jarvis_dict = {
    ('goodbye',)  : ['Goodbye Sir', 'Jarvis powering off in 3, 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 for you 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']
}

现在迭代每个键dict,检查是否有任何子字符串是消息的一部分,如果有匹配项,则调用speekmodule.speek(rand,n,mixer)函数,如下所示:

for key, value in jarvis_dict.items():
    if any(item in message for item in key):
        speekmodule.speek(value, n, mixer)

注意:这里我假设代码中的speekmodule.speek(value, n, mixer)正在工作,因为您的代码中没有关于这些声明的可用信息。我刚刚用value替换了您的rand,因为它与代码中使用的dict返回的str列表相同。你知道吗

相关问题 更多 >