Coverage for src/botadi/mokadi/mokadi_action_conversation.py: 73%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Defines an action for Mokadi.
5"""
6import os
7import random
8from .mokadi_action import MokadiAction
9from .mokadi_info import MokadiInfo
10from .mokadi_exceptions import MokadiException
11from .mokadi_wikipedia import definition_wikipedia, synonyms_wiktionary
14class MokadiActionConversation(MokadiAction):
15 """
16 Action. Conversation.
17 """
19 _sounds = {"hello": os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "hello.wav"),
20 "faux_rire": os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "faux_rire.wav"),
21 "applaudit": os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "applaus.wav"),
22 "toilette": os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "toilet_flush_2.wav")
23 }
25 _messages = {"hello": ["hello", "bonjour", "salut"],
26 "bye": ["au revoir", "salut", "adieu", "à la prochaine", "à la revoyure"]}
28 @staticmethod
29 def peak_random(mes: list):
30 """
31 Pick a random message
33 @param mes list of strings
34 @return one string
35 """
36 i = random.randint(0, len(mes) - 1)
37 return mes[i]
39 def __init__(self, fLOG=None):
40 """
41 Constructor.
43 @param folder folder where to look for presentation
44 @param fLOG logging function
45 """
46 MokadiAction.__init__(self, fLOG=fLOG)
48 def can_do(self, interpreted, message):
49 """
50 Tells if the class can process the message.
52 @param interpreted interpreted message
53 @param message message
54 @return true if the class can process the message
56 The function should be optimized for a subset of answers
57 quite long to build.
58 """
59 if len(interpreted) <= 1:
60 return False
61 res = self.process_interpreted_message(interpreted, message)
62 try:
63 for _ in res:
64 return True
65 except MokadiException:
66 return False
67 return False
69 def process_interpreted_message(self, interpretation, message):
70 """
71 Process the interpreted message.
73 @param interpretation interpretation
74 @param message original message
75 @return iterator on Info
76 """
77 sentance = [w[0].lower() for w in interpretation[1:]]
78 joined = " ".join(sentance).lower().replace("d'", "").replace("l'", "")
79 if sentance[0] in {"hello", "bonjour", "salut"}:
80 options = MokadiActionConversation._messages["hello"]
81 yield MokadiInfo("ok", MokadiActionConversation.peak_random(options))
82 elif sentance[0] in {"bye", "au revoir", "salut"}:
83 options = MokadiActionConversation._messages["bye"]
84 yield MokadiInfo("ok", MokadiActionConversation.peak_random(options))
85 elif sentance[0] in {"papa", "papounet"}:
86 if "ordinateur" in joined:
87 yield MokadiInfo("ok", "", sound=MokadiActionConversation._sounds["faux_rire"])
88 else:
89 raise MokadiException(
90 "Unable to answer to '{0}'.".format(sentance))
91 elif sentance[0] in {"bruit", "son"}:
92 if "toilette" in joined:
93 yield MokadiInfo("ok", "", sound=MokadiActionConversation._sounds["toilette"])
94 elif "applaudissement" in joined:
95 yield MokadiInfo("ok", "", sound=MokadiActionConversation._sounds["applaudit"])
96 else:
97 raise MokadiException(
98 "Unable to answer to '{0}'.".format(sentance))
99 elif sentance[0] in {"quel", "quelle", "comment", "pourquoi"}:
100 yield MokadiInfo("ok", "Je ne sais pas répondre à la question: {0}".format(sentance))
101 elif joined.startswith("c'est quoi") or interpretation[1][1] in {":definition:"}:
102 if "intelligence" in joined and "artificielle" in joined:
103 yield MokadiInfo("ok", "C'est moi.")
104 else:
105 query = " ".join(_[0] for _ in interpretation[
106 2:-1] if _[1] not in {":stopword:"})
107 self.fLOG(
108 "[MokadiActionConversation.process_interpreted_message] definition of '{0}'".format(query))
109 res = definition_wikipedia(query)
110 if res is None or len(res) == 0:
111 yield MokadiInfo("ok", "Je n'ai pas trouvé de définition pour {0}.".format(query))
112 else:
113 yield MokadiInfo("ok", res)
114 elif interpretation[1][1] in {":synonym:"}:
115 query = " ".join(_[0] for _ in interpretation[
116 2:-1] if _[1] not in {":stopword:"})
117 self.fLOG(
118 "[MokadiActionConversation.process_interpreted_message] synonym of '{0}'".format(query))
119 res = synonyms_wiktionary(query)
120 if res is None or len(res) == 0:
121 yield MokadiInfo("ok", "Je n'ai pas trouvé de synonyme.")
122 else:
123 yield MokadiInfo("ok", " ".join(res))
124 else:
125 raise MokadiException(
126 "Unable to answer to '{0}'.".format(sentance))