-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathessayW.py
218 lines (193 loc) · 6.91 KB
/
essayW.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Created by AlPHA or some people call me coach AlPHA
# This is a essay writer for making online schools much easier and it will correct your grammar if there is any
# Please don't share this with your teacher:)
# And remember to only say correct commends or you have to start over
# Enjoy
# 1.3.4
# AlPHA
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import speech_recognition as sr
import pyttsx3
import pyaudio
from time import sleep
import sys
from textblob import TextBlob
from gingerit.gingerit import GingerIt
# starting works #
SCOPES = ['https://www.googleapis.com/auth/drive.file']
creds = None
r = sr.Recognizer()
mic = sr.Microphone()
text = ""
docId = ""
text5 = ""
# Starting The speech engine
engine = pyttsx3.init("sapi5")
voices = engine.getProperty('voices')
engine.setProperty("voice", voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', 155)
volume = engine.getProperty('volume')
engine.setProperty('volume', 1.0)
engine.runAndWait()
# starting functions
# speaking function
def speak(text):
engine.say(text)
return engine.runAndWait()
# tokenizing google doc
def start():
global creds
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
# speech to text function
def speech_te():
global text
with sr.Microphone() as source:
print("Listening ...")
r.adjust_for_ambient_noise(source)
audio_data = r.listen(source)
text = r.recognize_google(audio_data)
print("Recognizing...")
# text = r.recognize_google(audio_data)
print(text)
# speech to text function number 2 for defining the document text
def speech_2():
global text5
with sr.Microphone() as source:
print("Listening ...")
r.adjust_for_ambient_noise(source)
audio_data = r.listen(source)
text5 = r.recognize_google(audio_data)
print("Recognizing...")
print(text5)
# start of program
if __name__ == '__main__':
speak("Hey, I'm essay Writer, I can help you with writing essay for you, Let's start")
sleep(0.1)
start()
service = build('docs', 'v1', credentials=creds)
# Creating the Document of google drive
speak("do you want to make a doc")
speech_te()
if text == "yes" or text == "yeah" or text == "yes please":
text = ""
speak("what do you want to call your doc?")
speech_te()
title = text
body = {'title': title}
doc = service.documents().create(body=body).execute()
docId = doc.get('documentId')
print('Created document with title: {0}'.format(doc.get('title')))
speak('Created document with title: {0}'.format(doc.get('title')))
elif text == "No" or text == "I have a document":
text = ""
speak("Sorry this option is not available right now")
print("Sorry this option is not available right now")
else:
text = ""
speak("Sorry This command is not available yet")
print("Sorry This command is not available yet")
exit()
# Starting the program to listen to essay
text = ""
speak("recording is ready, Please speak!")
# I did this because some time weird thing weird things happened to speech to text engine so it give gives chance
# to user to say the essay two times if needed
try:
speech_te()
except:
speech_te()
print("Please repeat")
speak("please repeat")
sleep(1)
speak("you can see your document in few seconds")
# finding the length of essay words
lent_text = len(text) + 1
speak(text)
# correcting the text and checking ofr grammar issue
result5 = GingerIt().parse(text)
text2 = TextBlob(text)
text2 = text2.correct()
text2 = str(text2)
speak("We corrected Your speaking and we founded some grammar and spelling issue, do you want us to insert your "
"speech or our corrected sentence to your document?")
speech_2()
# Asking user which speech they want to insert to document, there own or corrected with different engines
if text5 == "let me see" or text5 == "yes" or text5 == "yes show me":
print("1)", result5['result'], "\n2)", text2, "\n3) or Original version)", text)
speak("Please say the number you want")
sleep(2)
text5 = ""
speech_2()
if text5 == "1" or text5 == "number 1":
requests = [
{
'insertText': {
'location': {
'index': 1,
},
'text': result5
}
}
]
result = service.documents().batchUpdate(documentId=docId, body={'requests': requests}).execute()
elif text5 == "2" or text5 == "number 2":
requests = [
{
'insertText': {
'location': {
'index': 1,
},
'text': text2
}
}
]
result = service.documents().batchUpdate(documentId=docId, body={'requests': requests}).execute()
elif text5 == "3" or text5 == "number 3":
requests = [
{
'insertText': {
'location': {
'index': 1,
},
'text': text
}
}
]
result = service.documents().batchUpdate(documentId=docId, body={'requests': requests}).execute()
else:
speak("Please enter a valid number")
print("Please enter a valid number")
exit()
else:
speak("I will write your speech in the document")
requests = [
{
'insertText': {
'location': {
'index': 1,
},
'text': text
}
}
]
result = service.documents().batchUpdate(documentId=docId, body={'requests': requests}).execute()
# giving the link of google doc to the user so they can visit it
speak("We entered your speech into text and it's available in below link")
print("https://docs.google.com/document/d/" + docId + "/edit")
exit()