Spaces:
Sleeping
Sleeping
Upload 13 files
Browse files- Tamil_number_conversion.py +78 -0
- Text2List.py +67 -0
- applyVad.py +212 -0
- convert2list.py +55 -0
- highPassFilter.py +41 -0
- ipynb2py.py +41 -0
- isNumber.py +22 -0
- numberMapping.py +135 -0
- processDoubles.py +54 -0
- replaceWords.py +153 -0
- text2int.py +200 -0
- waveletDenoise.py +21 -0
- wienerFilter.py +22 -0
Tamil_number_conversion.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
5 |
+
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
import librosa
|
9 |
+
import numpy as np
|
10 |
+
import pywt
|
11 |
+
import nbimporter
|
12 |
+
from scipy.signal import butter, lfilter, wiener
|
13 |
+
from scipy.io.wavfile import write
|
14 |
+
from transformers import pipeline
|
15 |
+
from text2int import text_to_int
|
16 |
+
from isNumber import is_number
|
17 |
+
from Text2List import text_to_list
|
18 |
+
from convert2list import convert_to_list
|
19 |
+
from processDoubles import process_doubles
|
20 |
+
from replaceWords import replace_words
|
21 |
+
|
22 |
+
asr_model = pipeline("automatic-speech-recognition", model="cdactvm/w2v-bert-tamil_new")
|
23 |
+
|
24 |
+
# Function to apply a high-pass filter
|
25 |
+
def high_pass_filter(audio, sr, cutoff=300):
|
26 |
+
nyquist = 0.5 * sr
|
27 |
+
normal_cutoff = cutoff / nyquist
|
28 |
+
b, a = butter(1, normal_cutoff, btype='high', analog=False)
|
29 |
+
filtered_audio = lfilter(b, a, audio)
|
30 |
+
return filtered_audio
|
31 |
+
|
32 |
+
# Function to apply wavelet denoising
|
33 |
+
def wavelet_denoise(audio, wavelet='db1', level=1):
|
34 |
+
coeffs = pywt.wavedec(audio, wavelet, mode='per')
|
35 |
+
sigma = np.median(np.abs(coeffs[-level])) / 0.5
|
36 |
+
uthresh = sigma * np.sqrt(2 * np.log(len(audio)))
|
37 |
+
coeffs[1:] = [pywt.threshold(i, value=uthresh, mode='soft') for i in coeffs[1:]]
|
38 |
+
return pywt.waverec(coeffs, wavelet, mode='per')
|
39 |
+
|
40 |
+
# Function to apply a Wiener filter for noise reduction
|
41 |
+
def apply_wiener_filter(audio):
|
42 |
+
return wiener(audio)
|
43 |
+
|
44 |
+
# Function to handle speech recognition
|
45 |
+
def recognize_speech(audio_file):
|
46 |
+
audio, sr = librosa.load(audio_file, sr=16000)
|
47 |
+
audio = high_pass_filter(audio, sr)
|
48 |
+
audio = apply_wiener_filter(audio)
|
49 |
+
denoised_audio = wavelet_denoise(audio)
|
50 |
+
result = asr_model(denoised_audio)
|
51 |
+
text_value = result['text']
|
52 |
+
cleaned_text = text_value.replace("<s>", "")
|
53 |
+
print(cleaned_text)
|
54 |
+
converted_to_list = convert_to_list(cleaned_text, text_to_list())
|
55 |
+
print(converted_to_list)
|
56 |
+
processed_doubles = process_doubles(converted_to_list)
|
57 |
+
print(processed_doubles)
|
58 |
+
replaced_words = replace_words(processed_doubles)
|
59 |
+
print(replaced_words)
|
60 |
+
converted_text = text_to_int(replaced_words)
|
61 |
+
print(converted_text)
|
62 |
+
return converted_text
|
63 |
+
|
64 |
+
# Gradio Interface
|
65 |
+
gr.Interface(
|
66 |
+
fn=recognize_speech,
|
67 |
+
inputs=gr.Audio(sources=["microphone","upload"], type="filepath"),
|
68 |
+
outputs="text",
|
69 |
+
title="Speech Recognition with Advanced Noise Reduction & Hindi ASR",
|
70 |
+
description="Upload an audio file, and the system will use high-pass filtering, Wiener filtering, and wavelet-based denoising, then a Hindi ASR model will transcribe the clean audio."
|
71 |
+
).launch()
|
72 |
+
|
73 |
+
|
74 |
+
# In[ ]:
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
Text2List.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[4]:
|
5 |
+
|
6 |
+
|
7 |
+
def text_to_list():
|
8 |
+
text_list = [
|
9 |
+
# Tamil script for English numbers (11-19)
|
10 |
+
'எலெவன்', 'ட்வெல்வ்', 'திர்டீன்', 'போர்டீன்', 'ஃபிஃப்டீன்', 'சிக்ஸ்டீன்', 'சிவன்டீன்', 'எட்டீன்', 'நைன்டீன்',
|
11 |
+
# Tamil numbers (11-19)
|
12 |
+
'பதினொன்று', 'பனிரண்டு', 'பதிமூன்று', 'பதிநான்கு', 'பதினைந்து', 'பதினாறு', 'பதினேழு', 'பதினெட்டு', 'பத்தொன்பது',
|
13 |
+
# Tamil script for English multiples of ten (20, 30, ..., 90)
|
14 |
+
'ட்வெண்டி', 'திர்டி', 'போர்டி', 'ஃபிப்டி', 'சிக்ஸ்டி', 'சிவெண்டி', 'எய்ட்டி', 'நைன்டி',
|
15 |
+
# Tamil multiples of ten (20, 30, ..., 90)
|
16 |
+
'இருபது', 'முப்பது', 'நாற்பது', 'ஐம்பது', 'அறுபது', 'எழுபது', 'எண்பது', 'தொண்ணூறு',
|
17 |
+
# Tamil script for English combinations of 21-29
|
18 |
+
'ட்வெண்டி ஒன்', 'ட்வெண்டி டூ', 'ட்வெண்டி த்ரீ', 'ட்வெண்டி ஃபோர்', 'ட்வெண்டி ஃபைவு', 'ட்வெண்டி சிக்ஸ்', 'ட்வெண்டி செவன்', 'ட்வெண்டி எட்டு', 'ட்வெண்டி நைன்',
|
19 |
+
# Tamil combinations of 21-29
|
20 |
+
'இருபத்து ஒன்று', 'இருபத்து இரண்டு', 'இருபத்து மூன்று', 'இருபத்து நான்கு', 'இருபத்து ஐந்து', 'இருபத்து ஆறு', 'இருபத்து ஏழு', 'இருபத்து எட்டு', 'இருபத்து ஒன்பது',
|
21 |
+
# Tamil script for English combinations of 31-39
|
22 |
+
'திர்டி ஒன்', 'திர்டி டூ', 'திர்டி த்ரீ', 'திர்டி ஃபோர்', 'திர்டி ஃபைவு', 'திர்டி சிக்ஸ்', 'திர்டி செவன்', 'திர்டி எட்டு', 'திர்டி நைன்',
|
23 |
+
# Tamil combinations of 31-39
|
24 |
+
'முப்பத்து ஒன்று', 'முப்பத்து இரண்டு', 'முப்பத்து மூன்று', 'முப்பத்து நான்கு', 'முப்பத்து ஐந்து', 'முப்பத்து ஆறு', 'முப்பத்து ஏழு', 'முப்பத்து எட்டு', 'முப்பத்து ஒன்பது',
|
25 |
+
# Tamil script for English combinations of 41-49
|
26 |
+
'போர்டி ஒன்', 'போர்டி டூ', 'போர்டி த்ரீ', 'போர்டி ஃபோர்', 'போர்டி ஃபைவு', 'போர்டி சிக்ஸ்', 'போர்டி செவன்', 'போர்டி எட்டு', 'போர்டி நைன்',
|
27 |
+
# Tamil combinations of 41-49
|
28 |
+
'நாற்பத்து ஒன்று', 'நாற்பத்து இரண்டு', 'நாற்பத்து மூன்று', 'நாற்பத்து நான்கு', 'நாற்பத்து ஐந்து', 'நாற்பத்து ஆறு', 'நாற்பத்து ஏழு', 'நாற்பத்து எட்டு', 'நாற்பத்து ஒன்பது',
|
29 |
+
# Tamil script for English combinations of 51-59
|
30 |
+
'ஃபிப்டி ஒன்', 'ஃபிப்டி டூ', 'ஃபிப்டி த்ரீ', 'ஃபிப்டி ஃபோர்', 'ஃபிப்டி ஃபைவு', 'ஃபிப்டி சிக்ஸ்', 'ஃபிப்டி செவன்', 'ஃபிப்டி எட்டு', 'ஃபிப்டி நைன்',
|
31 |
+
# Tamil combinations of 51-59
|
32 |
+
'ஐம்பத்து ஒன்று', 'ஐம்பத்து இரண்டு', 'ஐம்பத்து மூன்று', 'ஐம்பத்து நான்கு', 'ஐம்பத்து ஐந்து', 'ஐம்பத்து ஆறு', 'ஐம்பத்து ஏழு', 'ஐம்பத்து எட்டு', 'ஐம்பத்து ஒன்பது',
|
33 |
+
# Tamil script for English combinations of 61-69
|
34 |
+
'சிக்ஸ்டி ஒன்', 'சிக்ஸ்டி டூ', 'சிக்ஸ்டி த்ரீ', 'சிக்ஸ்டி ஃபோர்', 'சிக்ஸ்டி ஃபைவு', 'சிக்ஸ்டி சிக்ஸ்', 'ச��க்ஸ்டி செவன்', 'சிக்ஸ்டி எட்டு', 'சிக்ஸ்டி நைன்',
|
35 |
+
# Tamil combinations of 61-69
|
36 |
+
'அறுபத்து ஒன்று', 'அறுபத்து இரண்டு', 'அறுபத்து மூன்று', 'அறுபத்து நான்கு', 'அறுபத்து ஐந்து', 'அறுபத்து ஆறு', 'அறுபத்து ஏழு', 'அறுபத்து எட்டு', 'அறுபத்து ஒன்பது',
|
37 |
+
# Tamil script for English combinations of 71-79
|
38 |
+
'சிவெண்டி ஒன்', 'சிவெண்டி டூ', 'சிவெண்டி த்ரீ', 'சிவெண்டி ஃபோர்', 'சிவெண்டி ஃபைவு', 'சிவெண்டி சிக்ஸ்', 'சிவெண்டி செவன்', 'சிவெண்டி எட்டு', 'சிவெண்டி நைன்',
|
39 |
+
# Tamil combinations of 71-79
|
40 |
+
'எழுபத்து ஒன்று', 'எழுபத்து இரண்டு', 'எழுபத்து மூன்று', 'எழுபத்து நான்கு', 'எழுபத்து ஐந்து', 'எழுபத்து ஆறு', 'எழுபத்து ஏழு', 'எழுபத்து எட்டு', 'எழுபத்து ஒன்பது',
|
41 |
+
# Tamil script for English combinations of 81-89
|
42 |
+
'எய்ட்டி ஒன்', 'எய்ட்டி டூ', 'எய்ட்டி த்ரீ', 'எய்ட்டி ஃபோர்', 'எய்ட்டி ஃபைவு', 'எய்ட்டி சிக்ஸ்', 'எய்ட்டி செவன்', 'எய்ட்டி எட்டு', 'எய்ட்டி நைன்',
|
43 |
+
# Tamil combinations of 81-89
|
44 |
+
'எண்பத்து ஒன்று', 'எண்பத்து இரண்டு', 'எண்பத்து மூன்று', 'எண்பத்து நான்கு', 'எண்பத்து ஐந்து', 'எண்பத்து ஆறு', 'எண்பத்து ஏழு', 'எண்பத்து எட்டு', 'எண்பத்து ஒன்பது',
|
45 |
+
# Tamil script for English combinations of 91-99
|
46 |
+
'நைன்டி ஒன்', 'நைன்டி டூ', 'நைன்டி த்ரீ', 'நைன்டி ஃபோர்', 'நைன்டி ஃபைவு', 'நைன்டி சிக்ஸ்', 'நைன்டி செவன்', 'நைன்டி எட்டு', 'நைன்டி நைன்',
|
47 |
+
# Tamil combinations of 91-99
|
48 |
+
'தொண்ணூற்று ஒன்று', 'தொண்ணூற்று இரண்டு', 'தொண்ணூற்று மூன்று', 'தொண்ணூற்று நான்கு', 'தொண்ணூற்று ஐந்து', 'தொண்ணூற்று ஆறு', 'தொண்ணூற்று ஏழு', 'தொண்ணூற்று எட்டு', 'தொண்ணூற்று ஒன்பது',
|
49 |
+
# Tamil script for English numbers (0-10)
|
50 |
+
'ஜீரோ', 'ஒன்', 'டூ', 'த்ரீ', 'போர்', 'ஃபைவ்', 'சிக்ஸ்', 'சிவன்', 'ஏட்', 'நைன்', 'டென்',
|
51 |
+
# Tamil numbers (0-10)
|
52 |
+
'பூஜ்ஜியம்', 'ஒன்று', 'இரண்டு', 'மூன்று', 'நான்கு', 'ஐந்து', 'ஆறு', 'ஏழு', 'எட்டு', 'ஒன்பது', 'பத்து',
|
53 |
+
# Tamil script for 100
|
54 |
+
'ஹண்ட்ரெட்',
|
55 |
+
# Tamil for 100
|
56 |
+
'நூறு',
|
57 |
+
# Tamil for 1000
|
58 |
+
'ஆயிரம்'
|
59 |
+
]
|
60 |
+
return text_list
|
61 |
+
|
62 |
+
|
63 |
+
# In[ ]:
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
applyVad.py
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
# import webrtcvad
|
8 |
+
# import numpy as np
|
9 |
+
# import librosa
|
10 |
+
# def apply_vad(audio, sr, frame_duration=30, aggressiveness=3):
|
11 |
+
# '''
|
12 |
+
# Voice Activity Detection (VAD): It is a technique used to determine whether a segment of audio contains speech.
|
13 |
+
# This is useful in noisy environments where you want to filter out non-speech parts of the audio.
|
14 |
+
# webrtcvad: This is a Python package based on the VAD from the WebRTC (Web Real-Time Communication) project.
|
15 |
+
# It helps detect speech in small chunks of audio.
|
16 |
+
# '''
|
17 |
+
# vad = webrtcvad.Vad()
|
18 |
+
# audio_int16 = np.int16(audio * 32767)
|
19 |
+
# frame_size = int(sr * frame_duration / 1000)
|
20 |
+
# frames = [audio_int16[i:i + frame_size] for i in range(0, len(audio_int16), frame_size)]
|
21 |
+
# voiced_audio = np.concatenate([frame for frame in frames if vad.is_speech(frame.tobytes(), sample_rate=sr)])
|
22 |
+
# voiced_audio = np.float32(voiced_audio) / 32767
|
23 |
+
# return voiced_audio
|
24 |
+
|
25 |
+
|
26 |
+
# In[1]:
|
27 |
+
|
28 |
+
|
29 |
+
# import webrtcvad
|
30 |
+
# import numpy as np
|
31 |
+
# import librosa
|
32 |
+
|
33 |
+
# def apply_vad(audio, sr):
|
34 |
+
# # Ensure that sample rate is supported by webrtcvad
|
35 |
+
# if sr not in [8000, 16000, 32000, 48000]:
|
36 |
+
# raise ValueError("Sample rate must be one of: 8000, 16000, 32000, or 48000 Hz")
|
37 |
+
|
38 |
+
# vad = webrtcvad.Vad(2) # Aggressiveness mode: 0-3
|
39 |
+
# frame_duration_ms = 30 # Use 10ms, 20ms, or 30ms frames only
|
40 |
+
|
41 |
+
# # Convert to PCM 16-bit and calculate frame length
|
42 |
+
# audio_pcm16 = (audio * 32767).astype(np.int16)
|
43 |
+
# frame_length = int(sr * frame_duration_ms / 1000) * 2 # 2 bytes per sample for 16-bit PCM
|
44 |
+
|
45 |
+
# # Create frames ensuring correct frame size
|
46 |
+
# frames = [
|
47 |
+
# audio_pcm16[i:i + frame_length].tobytes()
|
48 |
+
# for i in range(0, len(audio_pcm16) - frame_length, frame_length)
|
49 |
+
# ]
|
50 |
+
|
51 |
+
# # Apply VAD
|
52 |
+
# voiced_frames = []
|
53 |
+
# for frame in frames:
|
54 |
+
# try:
|
55 |
+
# if vad.is_speech(frame, sample_rate=sr):
|
56 |
+
# voiced_frames.append(frame)
|
57 |
+
# except Exception as e:
|
58 |
+
# print(f"Error during VAD frame processing: {e}")
|
59 |
+
|
60 |
+
# if not voiced_frames:
|
61 |
+
# raise Exception("No voiced frames detected.")
|
62 |
+
|
63 |
+
# # Concatenate voiced frames
|
64 |
+
# voiced_audio = b''.join(voiced_frames)
|
65 |
+
# return np.frombuffer(voiced_audio, dtype=np.int16) / 32767.0
|
66 |
+
|
67 |
+
|
68 |
+
# In[ ]:
|
69 |
+
|
70 |
+
|
71 |
+
# import webrtcvad
|
72 |
+
# import numpy as np
|
73 |
+
# import librosa
|
74 |
+
|
75 |
+
# def apply_vad(audio, sr, frame_duration=30, aggressiveness=3):
|
76 |
+
# '''
|
77 |
+
# Voice Activity Detection (VAD): Detects speech in audio.
|
78 |
+
# '''
|
79 |
+
# vad = webrtcvad.Vad(aggressiveness)
|
80 |
+
|
81 |
+
# # Resample to 16000 Hz if not already (recommended for better compatibility)
|
82 |
+
# if sr != 16000:
|
83 |
+
# audio = librosa.resample(audio, orig_sr=sr, target_sr=16000)
|
84 |
+
# sr = 16000
|
85 |
+
|
86 |
+
# # Convert to 16-bit PCM format expected by webrtcvad
|
87 |
+
# audio_int16 = np.int16(audio * 32767)
|
88 |
+
|
89 |
+
# # Ensure frame size matches WebRTC's expected lengths
|
90 |
+
# frame_size = int(sr * frame_duration / 1000)
|
91 |
+
# if frame_size % 2 != 0:
|
92 |
+
# frame_size -= 1 # Make sure it's even to avoid processing issues
|
93 |
+
|
94 |
+
# frames = [audio_int16[i:i + frame_size] for i in range(0, len(audio_int16), frame_size)]
|
95 |
+
|
96 |
+
# # Filter out non-speech frames
|
97 |
+
# voiced_frames = []
|
98 |
+
# for frame in frames:
|
99 |
+
# if len(frame) == frame_size and vad.is_speech(frame.tobytes(), sample_rate=sr):
|
100 |
+
# voiced_frames.append(frame)
|
101 |
+
|
102 |
+
# # Concatenate the voiced frames
|
103 |
+
# voiced_audio = np.concatenate(voiced_frames)
|
104 |
+
# voiced_audio = np.float32(voiced_audio) / 32767
|
105 |
+
|
106 |
+
# return voiced_audio
|
107 |
+
|
108 |
+
|
109 |
+
# In[3]:
|
110 |
+
|
111 |
+
|
112 |
+
# import webrtcvad
|
113 |
+
# import numpy as np
|
114 |
+
# import librosa
|
115 |
+
|
116 |
+
# def frame_generator(frame_duration_ms, audio, sample_rate):
|
117 |
+
# """
|
118 |
+
# Generates audio frames from PCM audio data.
|
119 |
+
# Takes the desired frame duration in milliseconds, the PCM data, and the sample rate.
|
120 |
+
# """
|
121 |
+
# n = int(sample_rate * (frame_duration_ms / 1000.0) * 2) # Convert to byte length
|
122 |
+
# offset = 0
|
123 |
+
# while offset + n < len(audio):
|
124 |
+
# yield audio[offset:offset + n]
|
125 |
+
# offset += n
|
126 |
+
|
127 |
+
# def apply_vad(audio, sample_rate):
|
128 |
+
# vad = webrtcvad.Vad()
|
129 |
+
# vad.set_mode(1)
|
130 |
+
# print("Applying VAD with mode:", 1)
|
131 |
+
# print("Audio length:", len(audio), "bytes")
|
132 |
+
# print("Sample rate:", sample_rate)
|
133 |
+
|
134 |
+
# # Ensure mono and correct sample rate
|
135 |
+
# if sample_rate != 16000:
|
136 |
+
# print("Sample rate issue detected.")
|
137 |
+
# raise ValueError("Sample rate must be 16000 Hz")
|
138 |
+
|
139 |
+
# frames = frame_generator(30, audio, sample_rate)
|
140 |
+
# frames = list(frames)
|
141 |
+
|
142 |
+
# print("Number of frames:", len(frames))
|
143 |
+
# try:
|
144 |
+
# segments = [frame for frame in frames if vad.is_speech(frame, sample_rate)]
|
145 |
+
|
146 |
+
# if not segments:
|
147 |
+
# raise Exception("No voiced frames detected.")
|
148 |
+
|
149 |
+
# return b''.join(segments)
|
150 |
+
|
151 |
+
# except Exception as e:
|
152 |
+
# print(f"Error during VAD frame processing: {e}")
|
153 |
+
# raise
|
154 |
+
|
155 |
+
|
156 |
+
# In[5]:
|
157 |
+
|
158 |
+
|
159 |
+
import torch
|
160 |
+
import torchaudio
|
161 |
+
from silero_vad import get_speech_timestamps, read_audio, save_audio
|
162 |
+
|
163 |
+
def apply_silero_vad(audio_file_path):
|
164 |
+
"""
|
165 |
+
Applies Silero VAD to an audio file and returns the processed audio
|
166 |
+
containing only the voiced segments.
|
167 |
+
"""
|
168 |
+
# Load the Silero VAD model
|
169 |
+
model = torch.hub.load('snakers4/silero-vad', 'silero_vad', force_reload=True)
|
170 |
+
|
171 |
+
# Define helper utilities manually
|
172 |
+
def read_audio(path, sampling_rate=16000):
|
173 |
+
wav, sr = torchaudio.load(path)
|
174 |
+
if sr != sampling_rate:
|
175 |
+
wav = torchaudio.transforms.Resample(orig_freq=sr, new_freq=sampling_rate)(wav)
|
176 |
+
return wav.squeeze(0)
|
177 |
+
|
178 |
+
def save_audio(path, tensor, sampling_rate=16000):
|
179 |
+
torchaudio.save(path, tensor.unsqueeze(0), sampling_rate)
|
180 |
+
|
181 |
+
# Read the audio file
|
182 |
+
wav = read_audio(audio_file_path, sampling_rate=16000)
|
183 |
+
|
184 |
+
# Get timestamps for speech segments
|
185 |
+
speech_timestamps = get_speech_timestamps(wav, model, sampling_rate=16000)
|
186 |
+
|
187 |
+
# If no speech detected, raise an exception
|
188 |
+
if not speech_timestamps:
|
189 |
+
raise Exception("No voiced frames detected using Silero VAD.")
|
190 |
+
|
191 |
+
# Combine the voiced segments
|
192 |
+
voiced_audio = torch.cat([wav[ts['start']:ts['end']] for ts in speech_timestamps])
|
193 |
+
|
194 |
+
# Save the processed audio if needed
|
195 |
+
save_audio('processed_voiced_audio.wav', voiced_audio, sampling_rate=16000)
|
196 |
+
|
197 |
+
# Convert to numpy bytes for further processing
|
198 |
+
return voiced_audio.numpy().tobytes()
|
199 |
+
|
200 |
+
# Example usage
|
201 |
+
try:
|
202 |
+
processed_audio = apply_silero_vad("path_to_your_audio.wav")
|
203 |
+
print("VAD completed successfully!")
|
204 |
+
except Exception as e:
|
205 |
+
print(f"Error during Silero VAD processing: {e}")
|
206 |
+
|
207 |
+
|
208 |
+
# In[ ]:
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
|
convert2list.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[2]:
|
5 |
+
|
6 |
+
|
7 |
+
# import nbimporter
|
8 |
+
import nbimporter
|
9 |
+
from Text2List import text_to_list
|
10 |
+
def convert_to_list(text, text_list):
|
11 |
+
matched_words = []
|
12 |
+
unmatched_text = '' # To accumulate unmatched characters
|
13 |
+
|
14 |
+
# Sort text_list by length in descending order to prioritize longest matches first
|
15 |
+
text_list_sorted = sorted(text_list, key=len, reverse=True)
|
16 |
+
|
17 |
+
while text:
|
18 |
+
matched = False
|
19 |
+
for word in text_list_sorted:
|
20 |
+
if text.startswith(word):
|
21 |
+
# Add any accumulated unmatched text before appending the matched word
|
22 |
+
if unmatched_text:
|
23 |
+
matched_words.append(unmatched_text)
|
24 |
+
unmatched_text = '' # Reset unmatched text accumulator
|
25 |
+
|
26 |
+
matched_words.append(word)
|
27 |
+
text = text[len(word):] # Remove the matched part from text
|
28 |
+
matched = True
|
29 |
+
break
|
30 |
+
|
31 |
+
if not matched:
|
32 |
+
# Accumulate unmatched characters
|
33 |
+
unmatched_text += text[0]
|
34 |
+
text = text[1:]
|
35 |
+
|
36 |
+
# If there's any remaining unmatched text, add it to the result
|
37 |
+
if unmatched_text:
|
38 |
+
matched_words.append(unmatched_text)
|
39 |
+
|
40 |
+
# Join matched words and unmatched text with a space
|
41 |
+
result = ' '.join(matched_words)
|
42 |
+
return result
|
43 |
+
|
44 |
+
# text = "जीरोएकदोतीनचारपांचछहसातआठनौदसजीरोएकदोतीनचारपांच"
|
45 |
+
|
46 |
+
# if __name__=="__main__":
|
47 |
+
# converted=convert_to_list(text, text_to_list())
|
48 |
+
# print(converted)
|
49 |
+
|
50 |
+
|
51 |
+
# In[ ]:
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
highPassFilter.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[2]:
|
5 |
+
|
6 |
+
|
7 |
+
# import scipy.signal
|
8 |
+
|
9 |
+
# def high_pass_filter(audio, sr, cutoff=200, order=3):
|
10 |
+
# """
|
11 |
+
# Applies a high-pass filter to an audio signal.
|
12 |
+
|
13 |
+
# Parameters:
|
14 |
+
# audio (numpy array): The input audio signal.
|
15 |
+
# sr (int): The sample rate of the audio signal.
|
16 |
+
# cutoff (float): The cutoff frequency in Hz. Default is 100 Hz.
|
17 |
+
# order (int): The order of the filter. Default is 5.
|
18 |
+
|
19 |
+
# Returns:
|
20 |
+
# numpy array: The filtered audio signal.
|
21 |
+
# """
|
22 |
+
# # Design the high-pass filter using a Butterworth filter design
|
23 |
+
# sos = scipy.signal.butter(order, cutoff, btype='highpass', fs=sr, output='sos')
|
24 |
+
|
25 |
+
# # Apply the filter using sosfilt (second-order sections filter)
|
26 |
+
# filtered_audio = scipy.signal.sosfilt(sos, audio)
|
27 |
+
|
28 |
+
# return filtered_audio
|
29 |
+
|
30 |
+
|
31 |
+
# In[ ]:
|
32 |
+
|
33 |
+
|
34 |
+
def high_pass_filter(audio, sr, cutoff=300):
|
35 |
+
# Design a Butterworth high-pass filter
|
36 |
+
nyquist = 0.5 * sr
|
37 |
+
normal_cutoff = cutoff / nyquist
|
38 |
+
b, a = butter(1, normal_cutoff, btype='high', analog=False)
|
39 |
+
filtered_audio = lfilter(b, a, audio)
|
40 |
+
return filtered_audio
|
41 |
+
|
ipynb2py.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
pip install nbconvert nbformat
|
8 |
+
|
9 |
+
|
10 |
+
# In[4]:
|
11 |
+
|
12 |
+
|
13 |
+
import nbformat
|
14 |
+
from nbconvert import PythonExporter
|
15 |
+
|
16 |
+
def convert_ipynb_to_py(ipynb_file, py_file):
|
17 |
+
# Load the notebook file
|
18 |
+
with open(ipynb_file, 'r', encoding='utf-8') as f:
|
19 |
+
notebook_content = nbformat.read(f, as_version=4)
|
20 |
+
|
21 |
+
# Create a Python exporter
|
22 |
+
python_exporter = PythonExporter()
|
23 |
+
|
24 |
+
# Convert the notebook to Python code
|
25 |
+
python_code, _ = python_exporter.from_notebook_node(notebook_content)
|
26 |
+
|
27 |
+
# Save the generated Python code to a .py file
|
28 |
+
with open(py_file, 'w', encoding='utf-8') as f:
|
29 |
+
f.write(python_code)
|
30 |
+
|
31 |
+
print(f"Conversion complete! {ipynb_file} has been converted to {py_file}.")
|
32 |
+
|
33 |
+
# Example usage:
|
34 |
+
convert_ipynb_to_py('highPassFilter.ipynb', 'huggingface/highPassFilter.py')
|
35 |
+
|
36 |
+
|
37 |
+
# In[ ]:
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
isNumber.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
5 |
+
|
6 |
+
|
7 |
+
# Function to check if the string is a number
|
8 |
+
def is_number(x):
|
9 |
+
if type(x) == str:
|
10 |
+
x = x.replace(',', '')
|
11 |
+
try:
|
12 |
+
float(x)
|
13 |
+
except:
|
14 |
+
return False
|
15 |
+
return True
|
16 |
+
|
17 |
+
|
18 |
+
# In[ ]:
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
|
numberMapping.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
replacement_map = {
|
8 |
+
'zero': ['शून्य', 'जेरो', 'शुन्ना', 'जीरो'],
|
9 |
+
'one': ['वन', 'एंक', 'इक', 'एक'],
|
10 |
+
'two': ['टू', 'दौ', 'दो'],
|
11 |
+
'three': ['थ्री', 'तीना', 'तीन', 'त्री'],
|
12 |
+
'four': ['फोर', 'फॉर', 'च्यार', 'चार'],
|
13 |
+
'five': ['फाइव', 'पाँच', 'पांच'],
|
14 |
+
'six': ['सिक्स', 'चह', 'छौ', 'छै', 'छह'],
|
15 |
+
'seven': ['सेवन', 'सात'],
|
16 |
+
'eight': ['एट', 'अट', 'आठ'],
|
17 |
+
'nine': ['नाइन', 'नौ'],
|
18 |
+
'ten': ['टेन', 'दस'],
|
19 |
+
|
20 |
+
# Numbers from 11 to 19
|
21 |
+
'eleven': ['इलेवन', 'ग्यारह'],
|
22 |
+
'twelve': ['ट्वेल्व', 'बारह'],
|
23 |
+
'thirteen': ['थर्टीन', 'तेरह'],
|
24 |
+
'fourteen': ['फोर्टीन', 'चौदह'],
|
25 |
+
'fifteen': ['फिफ्टीन', 'पंद्रह'],
|
26 |
+
'sixteen': ['सिक्स्टीन', 'सोलह'],
|
27 |
+
'seventeen': ['सेवंटीन', 'सत्रह'],
|
28 |
+
'eighteen': ['एटीन', 'अठारह'],
|
29 |
+
'nineteen': ['नाइनटीन', 'उन्नीस'],
|
30 |
+
|
31 |
+
# Multiples of ten
|
32 |
+
'twenty': ['ट्वेंटी', 'बीस'],
|
33 |
+
'thirty': ['थर्टी', 'तीस'],
|
34 |
+
'forty': ['फोर्टी', 'चालीस'],
|
35 |
+
'fifty': ['फिफ्टी', 'पचास'],
|
36 |
+
'sixty': ['सिक्स्टी', 'साठ'],
|
37 |
+
'seventy': ['सेवंटी', 'सत्तर'],
|
38 |
+
'eighty': ['एटी', 'अस्सी'],
|
39 |
+
'ninety': ['नाइंटी', 'नब्बे'],
|
40 |
+
|
41 |
+
# Numbers from 21 to 29
|
42 |
+
'twenty one': ['ट्वेंटी वन', 'इक्कीस'],
|
43 |
+
'twenty two': ['ट्वेंटी टू', 'बाईस'],
|
44 |
+
'twenty three': ['ट्वेंटी थ्री', 'तेईस'],
|
45 |
+
'twenty four': ['ट्वेंटी फोर', 'चौबीस'],
|
46 |
+
'twenty five': ['ट्वेंटी फाइव', 'पच्चीस'],
|
47 |
+
'twenty six': ['ट्वेंटी सिक्स', 'छब्बीस'],
|
48 |
+
'twenty seven': ['ट्वेंटी सेवन', 'सत्ताईस'],
|
49 |
+
'twenty eight': ['ट्वेंटी एट', 'अट्ठाईस'],
|
50 |
+
'twenty nine': ['ट्वेंटी नाइन', 'उनतीस'],
|
51 |
+
|
52 |
+
# Numbers from 31 to 39
|
53 |
+
'thirty one': ['थर्टी वन', 'इकतीस'],
|
54 |
+
'thirty two': ['थर्टी टू', 'बत्तीस'],
|
55 |
+
'thirty three': ['थर्टी थ्री', 'तेतीस'],
|
56 |
+
'thirty four': ['थर्टी फोर', 'चौंतीस'],
|
57 |
+
'thirty five': ['थर्टी फाइव', 'पैंतीस'],
|
58 |
+
'thirty six': ['थर्टी सिक्स', 'छत्तीस'],
|
59 |
+
'thirty seven': ['थर्टी सेवन', 'सैंतीस'],
|
60 |
+
'thirty eight': ['थर्टी एट', 'अड़तीस'],
|
61 |
+
'thirty nine': ['थर्टी नाइन', 'उनतालीस'],
|
62 |
+
|
63 |
+
# Numbers from 41 to 49
|
64 |
+
'forty one': ['फोर्टी वन', 'इकतालीस'],
|
65 |
+
'forty two': ['फोर्टी टू', 'बयालीस'],
|
66 |
+
'forty three': ['फोर्टी थ्री', 'तैंतालीस'],
|
67 |
+
'forty four': ['फोर्टी फोर', 'चौंतालीस'],
|
68 |
+
'forty five': ['फोर्टी फाइव', 'पैंतालीस'],
|
69 |
+
'forty six': ['फोर्टी सिक्स', 'छयालिस'],
|
70 |
+
'forty seven': ['फोर्टी सेवन', 'सैंतालीस'],
|
71 |
+
'forty eight': ['फोर्टी एट', 'अड़तालीस'],
|
72 |
+
'forty nine': ['फोर्टी नाइन', 'उनचास'],
|
73 |
+
|
74 |
+
# Numbers from 51 to 59
|
75 |
+
'fifty one': ['फिफ्टी वन', 'इक्यावन'],
|
76 |
+
'fifty two': ['फिफ्टी टू', 'बावन'],
|
77 |
+
'fifty three': ['फिफ्टी थ्री', 'तिरेपन'],
|
78 |
+
'fifty four': ['फिफ्टी फोर', 'चौवन'],
|
79 |
+
'fifty five': ['फिफ्टी फाइव', 'पचपन'],
|
80 |
+
'fifty six': ['फिफ्टी सिक्स', 'छप्पन'],
|
81 |
+
'fifty seven': ['फिफ्टी सेवन', 'सत्तावन'],
|
82 |
+
'fifty eight': ['फिफ्टी एट', 'अट्ठावन'],
|
83 |
+
'fifty nine': ['फिफ्टी नाइन', 'उनसठ'],
|
84 |
+
|
85 |
+
# Numbers from 61 to 69
|
86 |
+
'sixty one': ['सिक्स्टी वन', 'इकसठ'],
|
87 |
+
'sixty two': ['सिक्स्टी टू', 'बासठ'],
|
88 |
+
'sixty three': ['सिक्स्टी थ्री', 'तिरसठ'],
|
89 |
+
'sixty four': ['सिक्स्टी फोर', 'चौंसठ'],
|
90 |
+
'sixty five': ['सिक्स��टी फाइव', 'पैंसठ'],
|
91 |
+
'sixty six': ['सिक्स्टी सिक्स', 'छियासठ'],
|
92 |
+
'sixty seven': ['सिक्स्टी सेवन', 'सड़सठ'],
|
93 |
+
'sixty eight': ['सिक्स्टी एट', 'अड़सठ'],
|
94 |
+
'sixty nine': ['सिक्स्टी नाइन', 'उनहत्तर'],
|
95 |
+
|
96 |
+
# Numbers from 71 to 79
|
97 |
+
'seventy one': ['सेवंटी वन', 'इकहत्तर'],
|
98 |
+
'seventy two': ['सेवंटी टू', 'बहत्तर'],
|
99 |
+
'seventy three': ['सेवंटी थ्री', 'तिहत्तर'],
|
100 |
+
'seventy four': ['सेवंटी फोर', 'चौहत्तर'],
|
101 |
+
'seventy five': ['सेवंटी फाइव', 'पचहत्तर'],
|
102 |
+
'seventy six': ['सेवंटी सिक्स', 'छिहत्तर'],
|
103 |
+
'seventy seven': ['सेवंटी सेवन', 'सतहत्तर'],
|
104 |
+
'seventy eight': ['सेवंटी एट', 'अठहत्तर'],
|
105 |
+
'seventy nine': ['सेवंटी नाइन', 'उन्यासी'],
|
106 |
+
|
107 |
+
# Numbers from 81 to 89
|
108 |
+
'eighty one': ['एटी वन', 'इक्यासी'],
|
109 |
+
'eighty two': ['एटी टू', 'बयासी'],
|
110 |
+
'eighty three': ['एटी थ्री', 'तिरासी'],
|
111 |
+
'eighty four': ['एटी फोर', 'चौरासी'],
|
112 |
+
'eighty five': ['एटी फाइव', 'पचासी'],
|
113 |
+
'eighty six': ['एटी सिक्स', 'छियासी'],
|
114 |
+
'eighty seven': ['एटी सेवन', 'सतासी'],
|
115 |
+
'eighty eight': ['एटी एट', 'अठासी'],
|
116 |
+
'eighty nine': ['एटी नाइन', 'नवासी'],
|
117 |
+
|
118 |
+
# Numbers from 91 to 99
|
119 |
+
'ninety one': ['नाइंटी वन', 'इक्यानवे'],
|
120 |
+
'ninety two': ['नाइंटी टू', 'बानवे'],
|
121 |
+
'ninety three': ['नाइंटी थ्री', 'तिरानवे'],
|
122 |
+
'ninety four': ['नाइंटी फोर', 'चौरानवे'],
|
123 |
+
'ninety five': ['नाइंटी फाइव', 'पचानवे'],
|
124 |
+
'ninety six': ['नाइंटी सिक्स', 'छियानवे'],
|
125 |
+
'ninety seven': ['नाइंटी सेवन', 'सतानवे'],
|
126 |
+
'ninety eight': ['नाइंटी एट', 'अठानवे'],
|
127 |
+
'ninety nine': ['नाइंटी नाइन', 'निन्यानवे'],
|
128 |
+
|
129 |
+
# Hundred
|
130 |
+
'hundred': ['हंड्रेड', 'सौ'],
|
131 |
+
|
132 |
+
# Special for double digits
|
133 |
+
'डबल': ['दबल', 'डबल', 'दुबाल'],
|
134 |
+
}
|
135 |
+
|
processDoubles.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[2]:
|
5 |
+
|
6 |
+
|
7 |
+
# # Function to process "double" followed by a number
|
8 |
+
# def process_doubles(sentence):
|
9 |
+
# tokens = sentence.split()
|
10 |
+
# result = []
|
11 |
+
# i = 0
|
12 |
+
# while i < len(tokens):
|
13 |
+
# if tokens[i] == "डबल":
|
14 |
+
# if i + 1 < len(tokens):
|
15 |
+
# result.append(tokens[i + 1])
|
16 |
+
# result.append(tokens[i + 1])
|
17 |
+
# i += 2
|
18 |
+
# else:
|
19 |
+
# result.append(tokens[i])
|
20 |
+
# i += 1
|
21 |
+
# else:
|
22 |
+
# result.append(tokens[i])
|
23 |
+
# i += 1
|
24 |
+
# return ' '.join(result)
|
25 |
+
|
26 |
+
|
27 |
+
# In[ ]:
|
28 |
+
|
29 |
+
|
30 |
+
import re
|
31 |
+
|
32 |
+
def process_doubles(sentence):
|
33 |
+
# Use regex to split 'डबल' followed by numbers/words without space (e.g., "डबलवन" -> "डबल वन")
|
34 |
+
sentence = re.sub(r'(डबल)(\S+)', r'\1 \2', sentence)
|
35 |
+
|
36 |
+
tokens = sentence.split()
|
37 |
+
result = []
|
38 |
+
i = 0
|
39 |
+
|
40 |
+
while i < len(tokens):
|
41 |
+
if tokens[i] == "डबल":
|
42 |
+
if i + 1 < len(tokens):
|
43 |
+
result.append(tokens[i + 1]) # Append the next word/number
|
44 |
+
result.append(tokens[i + 1]) # Append the next word/number again to duplicate
|
45 |
+
i += 2 # Skip over the next word since it's already processed
|
46 |
+
else:
|
47 |
+
result.append(tokens[i])
|
48 |
+
i += 1
|
49 |
+
else:
|
50 |
+
result.append(tokens[i])
|
51 |
+
i += 1
|
52 |
+
|
53 |
+
return ' '.join(result)
|
54 |
+
|
replaceWords.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[9]:
|
5 |
+
|
6 |
+
|
7 |
+
import re
|
8 |
+
def replace_words(sentence):
|
9 |
+
replacement_map = {
|
10 |
+
# Single digits
|
11 |
+
'one': ['ஒன்று', 'ஒன்னு', 'ஒன்'],
|
12 |
+
'two': ['இரண்டு', 'ரெண்டு', 'டூ'],
|
13 |
+
'three': ['மூன்று', 'முன்னு', 'த்ரீ'],
|
14 |
+
'four': ['நான்கு', 'நாலு', 'ஃபோர்'],
|
15 |
+
'five': ['ஐந்து', 'அஞ்சு', 'ஃபைவ்'],
|
16 |
+
'six': ['ஆறு', 'ஆறு', 'சிக்ஸ்'],
|
17 |
+
'seven': ['ஏழு', 'எழு', 'செவன்'],
|
18 |
+
'eight': ['எட்டு', 'எட்டு', 'எய்ட்'],
|
19 |
+
'nine': ['ஒன்பது', 'ஒம்பது', 'நைன்'],
|
20 |
+
'ten': ['பத்து', 'பத்து', 'டென'],
|
21 |
+
|
22 |
+
# Numbers from 11 to 19
|
23 |
+
'eleven': ['பதினொன்று', 'பதினொன்னு', 'எலெவன்'],
|
24 |
+
'twelve': ['பன்னிரண்டு', 'பன்னிரண்டு', 'ட்வெல்வ்'],
|
25 |
+
'thirteen': ['பதின்மூன்று', 'பதின்முன்னு', 'தர்டீன்'],
|
26 |
+
'fourteen': ['பதினான்கு', 'பதின்நாலு', 'ஃபோர்டீன்'],
|
27 |
+
'fifteen': ['பதினைந்து', 'பதினஞ்சு', 'ஃபிப்டீன்'],
|
28 |
+
'sixteen': ['பதினாறு', 'பதினாறோ', 'சிக்ஸ்டீன்'],
|
29 |
+
'seventeen': ['பதினேழு', 'பதினேழு', 'செவன்டீன்'],
|
30 |
+
'eighteen': ['பதினெட்டு', 'பதினெட்டு', 'ஏட்டீன்'],
|
31 |
+
'nineteen': ['பத்தொன்பது', 'பத்தொம்பது', 'நைன்டீன்'],
|
32 |
+
|
33 |
+
# Multiples of ten
|
34 |
+
'twenty': ['இருபது', 'இருபத்து', 'ட்வென்டி'],
|
35 |
+
'thirty': ['முப்பது', 'முப்பத்து', 'தர்டி'],
|
36 |
+
'forty': ['நாற்பது', 'நாற்பத்து', 'ஃபோர்டி'],
|
37 |
+
'fifty': ['ஐம்பது', 'ஐம்பத்து', 'ஃபிப்டி'],
|
38 |
+
'sixty': ['அறுபது', 'அறுபத்து', 'சிக்ஸ்டி'],
|
39 |
+
'seventy': ['எழுபது', 'எழுபத்து', 'செவன்டி'],
|
40 |
+
'eighty': ['எண்பது', 'எண்பத்து', 'ஏட்டி'],
|
41 |
+
'ninety': ['தொண்ணூறு', 'தொன்னூறு', 'நைன்டி'],
|
42 |
+
|
43 |
+
# Numbers from 21 to 29
|
44 |
+
'twenty one': ['இருபத்து ஒன்று', 'இருபத்தொன்று', 'ட்வென்டி ஒன்'],
|
45 |
+
'twenty two': ['இருபத்து இரண்டு', 'இருபத்திரண்டு', 'ட்வென்டி டூ'],
|
46 |
+
'twenty three': ['இருபத்து மூன்று', 'இருபத்துமூன்று', 'ட்வென்டி த்ரீ'],
|
47 |
+
'twenty four': ['இருபத்து நான்கு', 'இருபத்துநான்கு', 'ட்வென்டி ஃபோர்'],
|
48 |
+
'twenty five': ['இருபத்து ஐந்து', 'இருபத்துஐந்து', 'ட்வென்டி ஃபைவ்'],
|
49 |
+
'twenty six': ['இருபத்து ஆறு', 'இருபத்துஆறு', 'ட்வென்டி சிக்ஸ்'],
|
50 |
+
'twenty seven': ['இருபத்து ஏழு', 'இருபத்துஏழு', 'ட்வென்டி செவன்'],
|
51 |
+
'twenty eight': ['இருபத்து எட்டு', 'இருபத்துஎட்டு', 'ட்வென்டி ஏட்'],
|
52 |
+
'twenty nine': ['இருபத்து ஒன்பது', 'இருபத்தொன்பது', 'ட்வென்டி நைன்'],
|
53 |
+
|
54 |
+
# Numbers from 31 to 39
|
55 |
+
'thirty one': ['முப்பத்து ஒன்று', 'முப்பத்தொன்று', 'தர்டி ஒன்'],
|
56 |
+
'thirty two': ['முப்பத்து இரண்டு', 'முப்பத்திரண்டு', 'தர்டி டூ'],
|
57 |
+
'thirty three': ['முப்பத்து மூன்று', 'முப்பத்துமூன்று', 'தர்டி த்ரீ'],
|
58 |
+
'thirty four': ['முப்பத்து நான்கு', 'முப்பத்துநான்கு', 'தர்டி ஃபோர்'],
|
59 |
+
'thirty five': ['முப்பத்து ஐந்து', 'முப்பத்துஐந்து', 'தர்டி ஃபைவ்'],
|
60 |
+
'thirty six': ['முப்பத்து ஆறு', 'முப்பத்துஆறு', 'தர்டி சிக்ஸ்'],
|
61 |
+
'thirty seven': ['முப்பத்து ஏழு', 'முப்பத்துஏழு', 'தர்டி செவன்'],
|
62 |
+
'thirty eight': ['முப்பத்து எட்டு', 'முப்பத்துஎட்டு', 'தர்டி ஏட்'],
|
63 |
+
'thirty nine': ['முப்பத்து ஒன்பது', 'முப்பத்தொன்பது', 'தர்டி நைன்'],
|
64 |
+
|
65 |
+
# Numbers from 41 to 49
|
66 |
+
'forty one': ['நாற்பத்து ஒன்று', 'நாற்பத்தொன்று', 'ஃபோர்டி ஒன்'],
|
67 |
+
'forty two': ['நாற்பத்து இரண்டு', 'நாற்பத்திரண்டு', 'ஃபோர்டி டூ'],
|
68 |
+
'forty three': ['நாற்பத்து மூன்று', 'நாற்பத்துமூன்று', 'ஃபோர்டி த்ரீ'],
|
69 |
+
'forty four': ['நாற்பத்து நான்கு', 'நாற்பத்துநான்கு', 'ஃபோர்டி ஃபோர்'],
|
70 |
+
'forty five': ['நாற்பத்து ஐந்து', 'நாற்பத்துஐந்து', 'ஃபோர்டி ஃபைவ்'],
|
71 |
+
'forty six': ['நாற்பத்து ஆறு', 'நாற்பத்துஆறு', 'ஃபோர்டி சிக்ஸ்'],
|
72 |
+
'forty seven': ['நாற்பத்து ஏழு', 'நாற்பத்துஏழு', 'ஃபோர்டி செவன்'],
|
73 |
+
'forty eight': ['நாற்பத்து எட்டு', 'நாற்பத்துஎட்டு', 'ஃபோர்டி ஏட்'],
|
74 |
+
'forty nine': ['நாற்பத்து ஒன்பது', 'நாற்பத்தொன்பது', 'ஃபோர்டி நைன்'],
|
75 |
+
|
76 |
+
# Numbers from 51 to 59
|
77 |
+
'fifty one': ['ஐம்பத்து ஒன்று', 'ஐம்பத்தொன்று', 'ஃபிப்டி ஒன்'],
|
78 |
+
'fifty two': ['ஐம்பத்து இரண்டு', 'ஐம்பத்திரண்டு', 'ஃபிப்டி டூ'],
|
79 |
+
'fifty three': ['ஐம்பத்து மூன்று', 'ஐம்பத்துமூன்று', 'ஃபிப்டி த்ரீ'],
|
80 |
+
'fifty four': ['ஐம்பத்து நான்கு', 'ஐம்பத்துநான்கு', 'ஃபிப்டி ஃபோர்'],
|
81 |
+
'fifty five': ['ஐம்பத்து ஐந்து', 'ஐம்பத்துஐந்து', 'ஃபிப்டி ஃபைவ்'],
|
82 |
+
'fifty six': ['ஐம்பத்து ஆறு', 'ஐம்பத்துஆறு', 'ஃபிப்டி சிக்ஸ்'],
|
83 |
+
'fifty seven': ['ஐம்பத்து ஏழு', 'ஐம்பத்துஏழு', 'ஃபிப்டி செவன்'],
|
84 |
+
'fifty eight': ['ஐம்பத்து எட்டு', 'ஐம்பத்துஎட்டு', 'ஃபிப்டி ஏட்'],
|
85 |
+
'fifty nine': ['ஐம்பத்து ஒன்பது', 'ஐம்பத்தொன்பது', 'ஃபிப்டி நைன்'],
|
86 |
+
|
87 |
+
# Numbers from 61 to 69
|
88 |
+
'sixty one': ['அறுபத்து ஒன்று', 'அறுபத்தொன்று', 'சிக்ஸ்டி ஒன்'],
|
89 |
+
'sixty two': ['அறுபத்து இரண்டு', 'அறுபத்திரண்டு', 'சிக்ஸ்டி டூ'],
|
90 |
+
'sixty three': ['அறுபத்து மூன்று', 'அறுபத்துமூன்று', 'சிக்ஸ்டி த்ரீ'],
|
91 |
+
'sixty four': ['அறுபத்து நான்கு', 'அறுபத்துநான்கு', 'சிக்ஸ்டி ஃபோர்'],
|
92 |
+
'sixty five': ['அறுபத்து ஐந்து', 'அறுபத்துஐந்து', 'சிக்ஸ்டி ஃபைவ்'],
|
93 |
+
'sixty six': ['அறுபத்து ஆறு', 'அறுபத்துஆறு', 'சிக்ஸ்டி சிக்ஸ்'],
|
94 |
+
'sixty seven': ['அறுபத்து ஏழு', 'அறுபத்துஏழு', 'சிக்ஸ்டி செவன்'],
|
95 |
+
'sixty eight': ['அறுபத்து எட்டு', 'அறுபத்துஎட்டு', 'சிக்ஸ்டி ஏட்'],
|
96 |
+
'sixty nine': ['அறுபத்து ஒன்பது', 'அறுபத்தொன்பது', 'சிக்ஸ்டி நைன்'],
|
97 |
+
|
98 |
+
# Numbers from 71 to 79
|
99 |
+
'seventy one': ['எழுபத்து ஒன்று', 'எழுபத்தொன்று', 'செவன்டி ஒன்'],
|
100 |
+
'seventy two': ['எழுபத்து இரண்டு', 'எழுபத்திரண்டு', 'செவன்டி டூ'],
|
101 |
+
'seventy three': ['எழுபத்து மூன்று', 'எழுபத்துமூன்று', 'செவன்டி த்ரீ'],
|
102 |
+
'seventy four': ['எழுபத்து நான்கு', 'எழுபத்துநான்கு', 'செவன்டி ஃபோர்'],
|
103 |
+
'seventy five': ['எழுபத்து ஐந���து', 'எழுபத்துஐந்து', 'செவன்டி ஃபைவ்'],
|
104 |
+
'seventy six': ['எழுபத்து ஆறு', 'எழுபத்துஆறு', 'செவன்டி சிக்ஸ்'],
|
105 |
+
'seventy seven': ['எழுபத்து ஏழு', 'எழுபத்துஏழு', 'செவன்டி செவன்'],
|
106 |
+
'seventy eight': ['எழுபத்து எட்டு', 'எழுபத்துஎட்டு', 'செவன்டி ஏட்'],
|
107 |
+
'seventy nine': ['எழுபத்து ஒன்பது', 'எழுபத்தொன்பது', 'செவன்டி நைன்'],
|
108 |
+
|
109 |
+
# Numbers from 81 to 89
|
110 |
+
'eighty one': ['எண்பத்து ஒன்று', 'எண்பத்தொன்று', 'ஏட்டி ஒன்'],
|
111 |
+
'eighty two': ['எண்பத்து இரண்டு', 'எண்பத்திரண்டு', 'ஏட்டி டூ'],
|
112 |
+
'eighty three': ['எண்பத்து மூன்று', 'எண்பத்துமூன்று', 'ஏட்டி த்ரீ'],
|
113 |
+
'eighty four': ['எண்பத்து நான்கு', 'எண்பத்துநான்கு', 'ஏட்டி ஃபோர்'],
|
114 |
+
'eighty five': ['எண்பத்து ஐந்து', 'எண்பத்துஐந்து', 'ஏட்டி ஃபைவ்'],
|
115 |
+
'eighty six': ['எண்பத்து ஆறு', 'எண்பத்துஆறு', 'ஏட்டி சிக்ஸ்'],
|
116 |
+
'eighty seven': ['எண்பத்து ஏழு', 'எண்பத்துஏழு', 'ஏட்டி செவன்'],
|
117 |
+
'eighty eight': ['எண்பத்து எட்டு', 'எண்பத்துஎட்டு', 'ஏட்டி ஏட்'],
|
118 |
+
'eighty nine': ['எண்பத்து ஒன்பது', 'எண்பத்தொன்பது', 'ஏட்டி நைன்'],
|
119 |
+
|
120 |
+
# Numbers from 91 to 99
|
121 |
+
'ninety one': ['தொண்ணூற்று ஒன்று', 'தொண்ணூற்றொன்று', 'நைன்டி ஒன்'],
|
122 |
+
'ninety two': ['தொண்ணூற்று இரண்டு', 'தொண்ணூற்றிரண்டு', 'நைன்டி டூ'],
|
123 |
+
'ninety three': ['தொண்ணூற்று மூன்று', 'தொண்ணூற்றுமூன்று', 'நைன்டி த்ரீ'],
|
124 |
+
'ninety four': ['தொண்ணூற்று நான்கு', 'தொண்ணூற்றுநான்கு', 'நைன்டி ஃபோர்'],
|
125 |
+
'ninety five': ['தொண்ணூற்று ஐந்து', 'தொண்ணூற்றுஐந்து', 'நைன்டி ஃபைவ்'],
|
126 |
+
'ninety six': ['தொண்ணூற்று ஆறு', 'தொண்ணூற்றுஆறு', 'நைன்டி சிக்ஸ்'],
|
127 |
+
'ninety seven': ['தொண்ணூற்று ஏழு', 'தொண்ணூற்றுஏழு', 'நைன்டி செவன்'],
|
128 |
+
'ninety eight': ['தொண்ணூற்று எட்டு', 'தொண்ணூற்றுஎட்டு', 'நைன்டி ஏட்'],
|
129 |
+
'ninety nine': ['தொண்ணூற்று ஒன்பது', 'தொண்ணூற்றொன்பது', 'நைன்டி நைன்'],
|
130 |
+
|
131 |
+
# Hundred
|
132 |
+
'hundred': ['நூறு', 'நூறை', 'ஹண்ட்ரெட்'],
|
133 |
+
# Thousand
|
134 |
+
'thousand': ['ஆயிரம்'],
|
135 |
+
}
|
136 |
+
|
137 |
+
words = sentence.split() # Split the sentence by spaces
|
138 |
+
|
139 |
+
# Replace words using the mapping
|
140 |
+
for i, word in enumerate(words):
|
141 |
+
for replacement, patterns in replacement_map.items():
|
142 |
+
if word in patterns:
|
143 |
+
words[i] = replacement # Replace the word if it's fully matched
|
144 |
+
|
145 |
+
# Join the processed words back into a sentence
|
146 |
+
return ' '.join(words)
|
147 |
+
|
148 |
+
|
149 |
+
# In[ ]:
|
150 |
+
|
151 |
+
|
152 |
+
|
153 |
+
|
text2int.py
ADDED
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
# # Function to convert Hindi text to numerical representation
|
8 |
+
# from isNumber import is_number
|
9 |
+
|
10 |
+
# def text_to_int (textnum, numwords={}):
|
11 |
+
# units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
|
12 |
+
# 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
|
13 |
+
# 'sixteen', 'seventeen', 'eighteen', 'nineteen']
|
14 |
+
# tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
|
15 |
+
# scales = ['hundred', 'thousand', 'lac','million', 'billion', 'trillion']
|
16 |
+
# ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
|
17 |
+
# ordinal_endings = [('ieth', 'y'), ('th', '')]
|
18 |
+
|
19 |
+
# if not numwords:
|
20 |
+
# numwords['and'] = (1, 0)
|
21 |
+
# for idx, word in enumerate(units): numwords[word] = (1, idx)
|
22 |
+
# for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
|
23 |
+
# for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
|
24 |
+
|
25 |
+
# textnum = textnum.replace('-', ' ')
|
26 |
+
|
27 |
+
# current = result = 0
|
28 |
+
# curstring = ''
|
29 |
+
# onnumber = False
|
30 |
+
# lastunit = False
|
31 |
+
# lastscale = False
|
32 |
+
|
33 |
+
# def is_numword(x):
|
34 |
+
# if is_number(x):
|
35 |
+
# return True
|
36 |
+
# if word in numwords:
|
37 |
+
# return True
|
38 |
+
# return False
|
39 |
+
|
40 |
+
# def from_numword(x):
|
41 |
+
# if is_number(x):
|
42 |
+
# scale = 0
|
43 |
+
# increment = int(x.replace(',', ''))
|
44 |
+
# return scale, increment
|
45 |
+
# return numwords[x]
|
46 |
+
|
47 |
+
# for word in textnum.split():
|
48 |
+
# if word in ordinal_words:
|
49 |
+
# scale, increment = (1, ordinal_words[word])
|
50 |
+
# current = current * scale + increment
|
51 |
+
# if scale > 100:
|
52 |
+
# result += current
|
53 |
+
# current = 0
|
54 |
+
# onnumber = True
|
55 |
+
# lastunit = False
|
56 |
+
# lastscale = False
|
57 |
+
# else:
|
58 |
+
# for ending, replacement in ordinal_endings:
|
59 |
+
# if word.endswith(ending):
|
60 |
+
# word = "%s%s" % (word[:-len(ending)], replacement)
|
61 |
+
|
62 |
+
# if (not is_numword(word)) or (word == 'and' and not lastscale):
|
63 |
+
# if onnumber:
|
64 |
+
# # Flush the current number we are building
|
65 |
+
# curstring += repr(result + current) + " "
|
66 |
+
# curstring += word + " "
|
67 |
+
# result = current = 0
|
68 |
+
# onnumber = False
|
69 |
+
# lastunit = False
|
70 |
+
# lastscale = False
|
71 |
+
# else:
|
72 |
+
# scale, increment = from_numword(word)
|
73 |
+
# onnumber = True
|
74 |
+
|
75 |
+
# if lastunit and (word not in scales):
|
76 |
+
# # Assume this is part of a string of individual numbers to
|
77 |
+
# # be flushed, such as a zipcode "one two three four five"
|
78 |
+
# curstring += repr(result + current)
|
79 |
+
# result = current = 0
|
80 |
+
|
81 |
+
# if scale > 1:
|
82 |
+
# current = max(1, current)
|
83 |
+
|
84 |
+
# current = current * scale + increment
|
85 |
+
# if scale > 100:
|
86 |
+
# result += current
|
87 |
+
# current = 0
|
88 |
+
|
89 |
+
# lastscale = False
|
90 |
+
# lastunit = False
|
91 |
+
# if word in scales:
|
92 |
+
# lastscale = True
|
93 |
+
# elif word in units:
|
94 |
+
# lastunit = True
|
95 |
+
|
96 |
+
# if onnumber:
|
97 |
+
# curstring += repr(result + current)
|
98 |
+
|
99 |
+
# return curstring
|
100 |
+
|
101 |
+
|
102 |
+
# In[3]:
|
103 |
+
|
104 |
+
|
105 |
+
import nbimporter
|
106 |
+
from isNumber import is_number # Remove or replace this if unnecessary
|
107 |
+
|
108 |
+
def text_to_int(textnum, numwords={}):
|
109 |
+
# Define units, tens, and scales including "lac"
|
110 |
+
units = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
|
111 |
+
'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
|
112 |
+
'sixteen', 'seventeen', 'eighteen', 'nineteen']
|
113 |
+
tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
|
114 |
+
scales = ['hundred', 'thousand', 'lac', 'million', 'billion', 'trillion'] # "lac" added
|
115 |
+
ordinal_words = {'first': 1, 'second': 2, 'third': 3, 'fifth': 5, 'eighth': 8, 'ninth': 9, 'twelfth': 12}
|
116 |
+
ordinal_endings = [('ieth', 'y'), ('th', '')]
|
117 |
+
|
118 |
+
if not numwords:
|
119 |
+
numwords['and'] = (1, 0) # Handle "one hundred and twenty"
|
120 |
+
|
121 |
+
# Add units, tens, and scales to numwords
|
122 |
+
for idx, word in enumerate(units):
|
123 |
+
numwords[word] = (1, idx)
|
124 |
+
for idx, word in enumerate(tens):
|
125 |
+
numwords[word] = (1, idx * 10)
|
126 |
+
|
127 |
+
for idx, word in enumerate(scales):
|
128 |
+
numwords[word] = (10 ** (5 if word == 'lac' else idx * 3 or 2), 0) # Handle "lac" as 10^5
|
129 |
+
|
130 |
+
# Remove hyphens and normalize input
|
131 |
+
textnum = textnum.replace('-', ' ')
|
132 |
+
|
133 |
+
current = result = 0
|
134 |
+
curstring = ''
|
135 |
+
onnumber = False
|
136 |
+
lastunit = False
|
137 |
+
lastscale = False
|
138 |
+
|
139 |
+
def is_numword(x):
|
140 |
+
return is_number(x) or x in numwords
|
141 |
+
|
142 |
+
def from_numword(x):
|
143 |
+
if is_number(x):
|
144 |
+
return 0, int(x.replace(',', ''))
|
145 |
+
return numwords[x]
|
146 |
+
|
147 |
+
for word in textnum.split():
|
148 |
+
if word in ordinal_words:
|
149 |
+
scale, increment = (1, ordinal_words[word])
|
150 |
+
current = current * scale + increment
|
151 |
+
if scale > 100:
|
152 |
+
result += current
|
153 |
+
current = 0
|
154 |
+
onnumber = True
|
155 |
+
lastunit = False
|
156 |
+
lastscale = False
|
157 |
+
else:
|
158 |
+
for ending, replacement in ordinal_endings:
|
159 |
+
if word.endswith(ending):
|
160 |
+
word = f"{word[:-len(ending)]}{replacement}"
|
161 |
+
|
162 |
+
if not is_numword(word) or (word == 'and' and not lastscale):
|
163 |
+
if onnumber:
|
164 |
+
curstring += repr(result + current) + " "
|
165 |
+
curstring += word + " "
|
166 |
+
result = current = 0
|
167 |
+
onnumber = False
|
168 |
+
lastunit = False
|
169 |
+
lastscale = False
|
170 |
+
else:
|
171 |
+
scale, increment = from_numword(word)
|
172 |
+
onnumber = True
|
173 |
+
|
174 |
+
if lastunit and word not in scales:
|
175 |
+
curstring += repr(result + current) + " "
|
176 |
+
result = current = 0
|
177 |
+
|
178 |
+
if scale > 1:
|
179 |
+
current = max(1, current)
|
180 |
+
|
181 |
+
current = current * scale + increment
|
182 |
+
|
183 |
+
if scale >= 100:
|
184 |
+
result += current
|
185 |
+
current = 0
|
186 |
+
|
187 |
+
lastscale = word in scales
|
188 |
+
lastunit = word in units
|
189 |
+
|
190 |
+
if onnumber:
|
191 |
+
curstring += repr(result + current)
|
192 |
+
|
193 |
+
return curstring.strip()
|
194 |
+
|
195 |
+
|
196 |
+
# In[ ]:
|
197 |
+
|
198 |
+
|
199 |
+
|
200 |
+
|
waveletDenoise.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[1]:
|
5 |
+
|
6 |
+
|
7 |
+
# Function to apply wavelet denoising
|
8 |
+
def wavelet_denoise(audio, wavelet='db1', level=1):
|
9 |
+
coeffs = pywt.wavedec(audio, wavelet, mode='per')
|
10 |
+
# Thresholding detail coefficients
|
11 |
+
sigma = np.median(np.abs(coeffs[-level])) / 0.6745
|
12 |
+
uthresh = sigma * np.sqrt(2 * np.log(len(audio)))
|
13 |
+
coeffs[1:] = [pywt.threshold(i, value=uthresh, mode='soft') for i in coeffs[1:]]
|
14 |
+
return pywt.waverec(coeffs, wavelet, mode='per')
|
15 |
+
|
16 |
+
|
17 |
+
# In[ ]:
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
wienerFilter.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[2]:
|
5 |
+
|
6 |
+
|
7 |
+
import scipy.signal
|
8 |
+
def wiener_filter(audio):
|
9 |
+
|
10 |
+
'''
|
11 |
+
The Wiener filter is designed to minimize the impact of noise by applying an adaptive filtering process.
|
12 |
+
It tries to estimate the original, clean signal by taking into account both the noisy signal and the statistical properties of the noise.
|
13 |
+
The Wiener filter is particularly useful when dealing with stationary noise (constant background noise, like white noise).
|
14 |
+
'''
|
15 |
+
return scipy.signal.wiener(audio)
|
16 |
+
|
17 |
+
|
18 |
+
# In[ ]:
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
|