-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
125 lines (98 loc) · 2.84 KB
/
functions.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
from googletrans import Translator
from gtts import gTTS
import pygame
from io import BytesIO
def translate_to_marathi(text):
translator = Translator()
try:
translation = translator.translate(text, src='en', dest='mr')
return translation.text
except Exception as e:
print("Translation error:", e)
return None
def translate_to_english(text):
translator = Translator()
try:
translation = translator.translate(text, src='mr', dest='en')
return translation.text
except Exception as e:
print("Translation error:", e)
return None
from langdetect import detect
def detect_language(text):
try:
language = detect(text)
return language
except Exception as e:
print("Error during language detection:", str(e))
return None
'''
o="how are u"
f=detect_language(text=o)
print(f)
o="माझा पाय खूप वेदना होत आहे"
f=detect_language(text=o)
print(f)
'''
def play_marathi_text(text):
# Create a gTTS object
tts = gTTS(text=text, lang='mr')
# Create an in-memory file object
audio_file = BytesIO()
tts.write_to_fp(audio_file)
audio_file.seek(0)
# Initialize Pygame mixer
pygame.mixer.init()
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
def play_english_text(text):
# Create a gTTS object
tts = gTTS(text=text, lang='en')
# Create an in-memory file object
audio_file = BytesIO()
tts.write_to_fp(audio_file)
audio_file.seek(0)
# Initialize Pygame mixer
pygame.mixer.init()
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
def return_order(query,dictionary):
d_k=list(dictionary.keys())
query=query.split(' ')
order=[]
for i in range(len(query)):
for j in range(len(d_k)):
if d_k[j].lower()==query[i].lower():
order.append(j)
return order
def words_to_numbers(sentence:str):
unique_words=set()
sentence=sentence.split(' ')
for i in range(len(sentence)):
unique_words.add(sentence[i])
return list(unique_words)
def return_dict(unique_words:list):
dictionary={}
for i in range(len(unique_words)):
dictionary[unique_words[i]]=i
return dictionary
def create_batches(list1:list):
return list1
def split_list(lst,per):
len_75=int(len(lst)*per)
first_list=lst[:len_75]
second_list=lst[len_75:]
return first_list,second_list
def number_to_words(lst, dictionary):
d_k = list(dictionary.keys())
d_v = list(dictionary.values())
o = []
for i in range(len(lst)):
for j in range(len(d_v)):
if d_v[j] == lst[i]:
o.append(d_k[j])
return ' '.join(o)