Upload english_to_hindi.py
Browse files- Task 3/english_to_hindi.py +57 -0
Task 3/english_to_hindi.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import speech_recognition as sr
|
2 |
+
from deep_translator import GoogleTranslator
|
3 |
+
from datetime import datetime, time
|
4 |
+
import pytz
|
5 |
+
|
6 |
+
def hours():
|
7 |
+
|
8 |
+
ist = pytz.timezone('Asia/Kolkata')
|
9 |
+
current_time = datetime.now(ist).time()
|
10 |
+
|
11 |
+
start_time = time(18, 0, 0) # 6 PM IST
|
12 |
+
end_time = time(23, 59, 59) # Midnight
|
13 |
+
return start_time <= current_time <= end_time
|
14 |
+
|
15 |
+
def audio():
|
16 |
+
r = sr.Recognizer()
|
17 |
+
with sr.Microphone() as source:
|
18 |
+
print("Please speak now:")
|
19 |
+
audio = r.listen(source)
|
20 |
+
try:
|
21 |
+
text = r.recognize_google(audio)
|
22 |
+
print("You said: " + text)
|
23 |
+
return text
|
24 |
+
except sr.UnknownValueError:
|
25 |
+
print("Sorry,could you Please repeat the sentence again.")
|
26 |
+
return None
|
27 |
+
except sr.RequestError:
|
28 |
+
print("Network error. Please check your connection.")
|
29 |
+
return None
|
30 |
+
|
31 |
+
def text(text):
|
32 |
+
translator = GoogleTranslator(source='en', target='hi')
|
33 |
+
words = text.split()
|
34 |
+
translated_words = []
|
35 |
+
for word in words:
|
36 |
+
if word[0].upper() not in ['M', 'O']:
|
37 |
+
translated_word = translator.translate(word)
|
38 |
+
translated_words.append(translated_word)
|
39 |
+
else:
|
40 |
+
translated_words.append(word)
|
41 |
+
return ' '.join(translated_words)
|
42 |
+
|
43 |
+
def main():
|
44 |
+
if not hours():
|
45 |
+
print("Translation feature is available only after 6 PM IST.")
|
46 |
+
return
|
47 |
+
|
48 |
+
text = None
|
49 |
+
while text is None:
|
50 |
+
text = audio()
|
51 |
+
|
52 |
+
if text:
|
53 |
+
translated_text = text(text)
|
54 |
+
print("Translated text:", translated_text)
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
main()
|