Upload NumberToText.py
#3
by Aditya02 - opened
- NumberToText.py +96 -0
NumberToText.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
|
| 4 |
+
class NumberDictionary:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
directoryPath = "numToText"
|
| 7 |
+
# print(directoryPath)
|
| 8 |
+
languages = self.get_filenames_in_folder(directoryPath)
|
| 9 |
+
# print(languages, directoryPath)
|
| 10 |
+
self.lang_num_dictionary = self.load_language_dictionary(directoryPath, languages)
|
| 11 |
+
# print(self.lang_num_dictionary)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_filenames_in_folder(self,folder_path):
|
| 16 |
+
file_list = []
|
| 17 |
+
|
| 18 |
+
# Loop through the files in the directory
|
| 19 |
+
for filename in os.listdir(folder_path):
|
| 20 |
+
# Check if it's a file (not a subdirectory)
|
| 21 |
+
if os.path.isfile(os.path.join(folder_path, filename)):
|
| 22 |
+
file_list.append(filename[:-4])
|
| 23 |
+
|
| 24 |
+
return file_list
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def load_language_dictionary(self, directory_path, file_names):
|
| 28 |
+
lang_num_dictionary = {}
|
| 29 |
+
|
| 30 |
+
for file_name in file_names:
|
| 31 |
+
language = os.path.splitext(file_name)[0]
|
| 32 |
+
file_path = os.path.join(directory_path, f"{file_name}.csv")
|
| 33 |
+
if not os.path.exists(file_path):
|
| 34 |
+
# print(f"File '{file_path}' not found. Skipping...")
|
| 35 |
+
continue
|
| 36 |
+
|
| 37 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 38 |
+
reader = csv.reader(file)
|
| 39 |
+
language_map = {row[0].strip(): row[1].strip() for row in reader}
|
| 40 |
+
|
| 41 |
+
lang_num_dictionary[language] = language_map
|
| 42 |
+
|
| 43 |
+
return lang_num_dictionary
|
| 44 |
+
|
| 45 |
+
def num2text(self, input_str, language):
|
| 46 |
+
if language not in self.lang_num_dictionary:
|
| 47 |
+
return "Language not supported."
|
| 48 |
+
|
| 49 |
+
integer_part, *decimal_part = input_str.split('.')
|
| 50 |
+
try:
|
| 51 |
+
int_part = int(integer_part)
|
| 52 |
+
except ValueError:
|
| 53 |
+
return "Invalid input. Please provide a valid number."
|
| 54 |
+
|
| 55 |
+
if int_part < 0 or int_part > 999999999999999:
|
| 56 |
+
return "Number out of range (0-999999999999999)"
|
| 57 |
+
|
| 58 |
+
lang_map = self.lang_num_dictionary[language]
|
| 59 |
+
integer_text = self.convert_to_indian_number(int_part, lang_map)
|
| 60 |
+
|
| 61 |
+
if decimal_part:
|
| 62 |
+
decimal_text = lang_map.get('.', '') + ' '
|
| 63 |
+
for digit in decimal_part[0]:
|
| 64 |
+
decimal_text += lang_map.get(digit, '') + ' '
|
| 65 |
+
return (integer_text + ' ' + decimal_text).strip()
|
| 66 |
+
else:
|
| 67 |
+
return integer_text.strip()
|
| 68 |
+
|
| 69 |
+
def convert_to_indian_number(self, n, lang_map):
|
| 70 |
+
numeric_keys = [key for key in lang_map.keys() if key.isdigit()] # Filter numeric keys
|
| 71 |
+
if n <= 20 or (n <= 100 and str(n) in lang_map):
|
| 72 |
+
return lang_map.get(str(n), '')
|
| 73 |
+
elif n < 1000:
|
| 74 |
+
result = f"{lang_map.get(str(n // 100), '')} {lang_map.get('100', '')}"
|
| 75 |
+
if n % 100 != 0:
|
| 76 |
+
result += f" {self.convert_to_indian_number(n % 100, lang_map)}"
|
| 77 |
+
return result.strip()
|
| 78 |
+
else:
|
| 79 |
+
base, term = 0, ''
|
| 80 |
+
for key in sorted(numeric_keys, key=int, reverse=True): # Sort only numeric keys
|
| 81 |
+
if n >= int(key):
|
| 82 |
+
base = int(key)
|
| 83 |
+
term = lang_map[key]
|
| 84 |
+
break
|
| 85 |
+
|
| 86 |
+
if n % base == 0:
|
| 87 |
+
return f"{self.convert_to_indian_number(n // base, lang_map)} {term}"
|
| 88 |
+
else:
|
| 89 |
+
return f"{self.convert_to_indian_number(n // base, lang_map)} {term} {self.convert_to_indian_number(n % base, lang_map)}"
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# number_dict = NumberDictionary()
|
| 95 |
+
# result = number_dict.num2text("2000048.145", "gujarati")
|
| 96 |
+
# print(result)
|