代码之家  ›  专栏  ›  技术社区  ›  multigoodverse

如何有效地评估一系列方法,并调用那些不是错误的方法?

  •  0
  • multigoodverse  · 技术社区  · 6 年前

    下面的类获取一个字符串作为输入,并通过 answer() 方法。

    class Question:
    
        a = ["hello", "hi"]
        b = ["how are you", "how do you do"]
        c = ["how is the weather", "is it cold today"]
    
        def __init__(self, question):
            self.question = question
    
        def get_greeting(self):
            if self.question in Question.a:
                return "Hi There!"
    
        def get_health(self):
            if self.question in Question.b:
                return "I am fine"
    
        def get_weather(self):
            if self.question in Question.c:
                return "It is warm"
    
        def answer(self):
    
            if self.get_greeting():
                return self.get_greeting()
            elif self.get_health():
                return self.get_health()
            elif self.get_weather():
                return self.get_weather()
            else:
                return "I don't understand"
    
    question = Question("how is the weather") 
    print(question.answer()) # getting the output
    

    对我来说,上面是坏的,因为里面的代码 答案() 它很长,每个方法调用两次。

    因此,我想出了一个“更好的” 答案() 方法,但仍然有很多if条件。

    def answer(self):
    
        result = self.get_greeting()
        if result:
            return result
    
        result = self.get_health()
        if result:
            return result
    
        result = self.get_weather()
        if result:
            return result
    
        return "I don't understand"
    

    我想这里可能还缺少其他一些技巧。有人能提出什么建议吗?

    2 回复  |  直到 6 年前
        1
  •  3
  •   molbdnilo    6 年前

    结果 or 如果不是“false-y”,则为左侧操作数,否则为右侧操作数。
    它还仅在需要时计算右操作数。

    def answer(self):
        return self.get_greeting() \
            or self.get_health() \
            or self.get_weather() \
            or "I don't understand"
    
        2
  •  2
  •   Felix    6 年前

    您可以创建所有方法的元组,然后调用它们,直到有一个方法返回:

    def answer(self):
        methods = (self.get_greeting, self.get_health, self.get_weather)
        for m in methods:
            res = m()
            if res:
                return res
        return "I don't understand"
    

    编辑

    如果你真的想创建很多方法 answer() 函数在不显式告知的情况下尝试所有函数,您可以使用以下代码:

    def answer(self):
            getters = (v for k, v in self.__class__.__dict__.items() if k.startswith("get_"))
            for method in getters:
                res = method(self)
                if res:
                    return res
            return "I don't understand"
    

    编辑2

    如果您的系统只是获取一个字符串作为输入,并从中生成预定义的输出,那么您可以将其简化很多:

    knowledge = [
        (["hello", "hi"], "Hi There"),
        (["how are you", "how do you do"], "I am fine"),
        (["how is the weather", "is it cold today"], "It is warm")
    ]
    
    def answer(question):
        for inputs, answer in knowledge:
            if question in inputs:
                return answer
        return "I don't understand"
    
    print(answer("hello"))
    

    使用这种方法,向chatbot添加一个新短语就像向知识数据结构添加一行一样简单。