-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzorbashell.py
executable file
·162 lines (131 loc) · 4.79 KB
/
zorbashell.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
from os import path
import sys
import shutil
import os
import select
import subprocess
import re
from zorbacmd import ZorbaCMD
from zorbaspeech import ZorbaSpeech
from zorbachatter import ZorbaChatter
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
try:
import gnureadline as readline
except ImportError:
import readline
#from __future__ import braces #this makes it possible to use {} instead of indentation
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('set editing-mode vi')
DIRECTORY_TO_WATCH = os.path.abspath(os.path.dirname(sys.argv[0])) + "/watchme/"
language = ''#'it-IT'
singlerun = False
continuous = True
voice = False
bot = []
chat_id = ""
if language == '' and os.path.isfile("zorbalanguage.txt"):
text_file = open("zorbalanguage.txt", "r")
language = text_file.read().replace("\n", "")
text_file.close()
if language == '':
language = 'it-IT'
Zorba = ZorbaCMD(language)
speech = ZorbaSpeech(language)
chatter = ZorbaChatter(language)
chatter.clearTraining()
chatter.train()
class zwHandler(FileSystemEventHandler):
global bot
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
#elif event.event_type == 'created':
elif event.event_type == 'modified':
try:
Zorba.set_telegramusers(listusers())
except:
nousers = True
if os.path.isfile(event.src_path):
content = ""
text_file = open(event.src_path, "r")
content = text_file.read().replace("\n", "")
text_file.close()
if content[:4] == "CMD:":
tr_cmd = Zorba.translate(content.replace("CMD:", ""))
if "WHAT?" == tr_cmd:
answer = chatter.reply(content)
Zorba.sendMessage(chat_id, bot, speech, str(answer), voice, "")
else:
try:
cmdoutput = subprocess.check_output(tr_cmd, shell=True).decode('UTF-8')
except:
cmdoutput = ""
if cmdoutput != "":
Zorba.display_output(cmdoutput, "", bot, speech, voice)
else:
print(tr_cmd)
else:
Zorba.display_output(content, "", bot, speech, voice)
if os.path.isfile(event.src_path): os.remove(event.src_path)
for (i, item) in enumerate(sys.argv):
if item == "-h":
print("Options:\n -l specify language\n -p specify phrase to analize\n -c continuous mode, works as a shell (default)\n -v uses voice recognition and sinthesys\n -t allows you to tune the speech recognition model for your own voice")
if item == "-l":
language = sys.argv[i+1]
print("LANGUAGE: " + language)
if item == "-p":
singlerun = True
if item == "-v":
voice = True
if item == "-t":
singlerun = True
continuous = False
speech.train('')
sys.exit(0)
if item == "-c":
singlerun = False
continuous = True
observer = Observer()
event_handler = zwHandler()
observer.schedule(event_handler, DIRECTORY_TO_WATCH, recursive=True)
observer.start()
while continuous:
if voice == True:
phrase = speech.recognize()
print("You: " + phrase)
else:
phrase = input("You: ")
phrases = list(filter(None, re.split("[;.!?]", phrase))) #if we've got multiple phrases, we split them
for (p, itemp) in enumerate(phrases):
command = phrases[p]
if command != '':
tr_cmd = Zorba.translate(command)
if "SAYHELLO" == tr_cmd:
res = " ^_^\n"
res = res + "(*.*)\n"
res = res + " ---"
#sendMessage(chat_id, res)
Zorba.sendMessage(chat_id, bot, speech, res)
elif "WHAT?" == tr_cmd:
answer = chatter.reply(command)
Zorba.sendMessage(chat_id, bot, speech, str(answer), voice)
elif "SET continuous FALSE" == tr_cmd:
continuous = False
else:
try:
cmdoutput = subprocess.check_output(tr_cmd, shell=True).decode('UTF-8')
except:
cmdoutput = ""
if cmdoutput != "":
Zorba.display_output(cmdoutput, "", bot, speech, voice)
else:
print(tr_cmd)
if singlerun == True:
continuous = False
observer.stop()
observer.join()