AleksanderObuchowski
3/2/2020 - 11:22 PM

answer.py

    @staticmethod
    def answer(message,embeddings_file,anwsers_file, verbose = False):
        start = time.time()
        global embeddings
        with open(embeddings_file, 'rb') as file:
            embedded_dict = pickle.load(file)
        # if(verbose== True):
        #     print("embedding loaded successfully")
        message_sentence = Sentence(message)
        embeddings.embed(message_sentence)
        message_vector = message_sentence.embedding.detach().numpy()
        # if(verbose== True):
        #     print("message encoded successfully")
        best_intent = ""
        best_score = 1
        for intent, examples in embedded_dict.items():
            for example in examples:
                score = cosine(message_vector, example)
                if(score<best_score):
                    best_score = score
                    best_intent = intent
        if(verbose== True):
            print(f"best inent: {best_intent}")
        with open(anwsers_file) as file:
            anwsers_dict = json.load(file)
        if(best_intent in anwsers_dict):
            if(verbose == True):
                print(f'answer time: {time.time()-start}')
            return random.choice(anwsers_dict[best_intent])
        else:
            return "Error intent not in dict"