-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathskills.py
More file actions
172 lines (124 loc) · 5.25 KB
/
skills.py
File metadata and controls
172 lines (124 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from util.res import *
import importlib
from locale import getlocale
from gtts import gTTS
from random import randint
from os import remove
import playsound
from difflib import SequenceMatcher
from util.translator import translate
from multiprocessing import Process
from functools import partial
from webbrowser import open as open_url
def starred_sentences_ratio(line,voice_command,sentence):
"""
a star [*] can be placed in sentence to mark where random words can appears.
I split the setences by * and return the average ratio of matching,
the voice command is considered as matching.
"""
split = sentence.split("*")
voice_command = voice_command.lower()
ratio_l = []
for line_part in split:
if not line_part in voice_command :
r = SequenceMatcher(None, voice_command, line_part).ratio()
if r < 0.80:
return {"module" : line.split(":")[0], "ratio" : r, "match" : False}
else:
r = 1.0
ratio_l.append(r)
av_r = 0.0
av_r += sum(ratio_l)/len(ratio_l)
return {"module" : line.split(":")[0], "ratio" : av_r, "match" : True}
def full_sentence_ratio(line,voice_command, sentence):
"""
return a matching ratio between a trigger sentence from a skill and the voice_command
"""
return {"module" : line.split(":")[0], "ratio": SequenceMatcher(None, voice_command, sentence).ratio()}
def check_user_custom_commands(voice_command):
#custom websites part
custom_websites = get_custom_websites_voice_commands()
for website in custom_websites:
if voice_command == website['voice_command'] :
Process(target = open_url, args=(website['url'],) ).start()
return True
#coming soon : custom request to server part
return False
def init_skill_call(module, ratio,final_sentences,voice_command):
# correct module is now defined, display ratio in a human-friendly way
# and call the skill
ratio = round(ratio,4)
print(f"module : {module} | confidence : {ratio*100}%")
Process(target=call_skill,args=(module,voice_command,final_sentences.split("/"),)).start()
def check_skills(voice_command):
pinfo("logging voice command...")
with open("config/logs.txt","w") as f:
f.write(voice_command)
f.close()
#check if a skill is waiting user to speak or not, if not we can begin the process
if not is_waiting_user_command():
pinfo("checking user custom commands...")
if not check_user_custom_commands(voice_command):
pinfo("checking skills...")
with open("config/skills.blue","r",encoding="utf-8") as f:
lines = f.read().splitlines()
pinfo("trying starred sentences ratio...")
for line in lines:
sentences = line.split(":")[1]
for sentence in sentences.split("/"):
result = starred_sentences_ratio(line,voice_command,sentence)
if result['match']:
init_skill_call(result['module'], result['ratio'],sentences,voice_command)
return True
ratio = 0
module = None
final_sentences = ""
pinfo("trying full sentences ratio...")
for line in lines:
sentences = line.split(":")[1]
for sentence in sentences.split("/"):
final_sentences = sentences
#starred lines ratio didn't work, using full ratio to get the higher match
result = full_sentence_ratio(line,voice_command,sentence.strip("*"))
if result['ratio'] > ratio:
module = result['module']
if ratio > 0.2:
#calling module on higher ratio obtained
init_skill_call(module, ratio,final_sentences,voice_command)
else:
module = "open_website"
init_skill_call(module, ratio,"",voice_command)
return True
else:
return True
def speak(text):
try:
tts = gTTS(text,lang=get_locale())
sn = str(randint(1,100000))+".mp3"
tts.save(sn)
playsound.playsound(sn)
remove(sn)
except Exception as e:
print(e)
pass
def call_skill(module,voice_command,sentences):
try:
skill = importlib.import_module(f"skills_modules.{module}")
except Exception as e:
perror(f"Error while importing skill module : {e}")
return
ret, response = skill.initialize(voice_command,sentences)
try:
locale = get_locale()
if locale != 'fr':
response = translate(response,'fr',dest=locale)
except Exception as e:
perror(f"Error while translating Blue response to your language : {e}")
return
print(response)
try:
if response != "":
speak(response)
except Exception as e:
perror(f"Error while trying to speak : {e}")
return