neuralworm's picture
initial commit
1032a12
raw
history blame
6.17 kB
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
import inflect
from datetime import datetime, date
from deep_translator import GoogleTranslator
# Custom function to convert number to ordinal words
def number_to_ordinal_word(number):
ordinal_dict = {
1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth",
6: "sixth", 7: "seventh", 8: "eighth", 9: "ninth", 10: "tenth",
11: "eleventh", 12: "twelfth", 13: "thirteenth", 14: "fourteenth",
15: "fifteenth", 16: "sixteenth", 17: "seventeenth", 18: "eighteenth",
19: "nineteenth", 20: "twentieth", 21: "twentyfirst", 22: "twentysecond",
23: "twentythird", 24: "twentyfourth", 25: "twentyfifth",
26: "twentysixth", 27: "twentyseventh", 28: "twentyeighth",
29: "twentyninth", 30: "thirtieth", 31: "thirtyfirst"
}
return ordinal_dict.get(number, "")
def custom_normalize(text):
mappings = {
'ü': 'ue', 'ö': 'oe', 'ä': 'ae', 'ß': 'ss', 'Ü': 'Ue', 'Ö': 'Oe', 'Ä': 'Ae',
'á': 'a', 'à': 'a', 'â': 'a', 'ã': 'a', 'å': 'aa', 'ā': 'a', 'ă': 'a', 'ą': 'a',
'Á': 'A', 'À': 'A', 'Â': 'A', 'Ã': 'A', 'Å': 'Aa', 'Ā': 'A', 'Ă': 'A', 'Ą': 'A',
'é': 'e', 'è': 'e', 'ê': 'e', 'ë': 'e', 'ē': 'e', 'ĕ': 'e', 'ė': 'e', 'ę': 'e', 'ě': 'e',
'É': 'E', 'È': 'E', 'Ê': 'E', 'Ë': 'E', 'Ē': 'E', 'Ĕ': 'E', 'Ė': 'E', 'Ę': 'E', 'Ě': 'E',
'í': 'i', 'ì': 'i', 'î': 'i', 'ï': 'i', 'ī': 'i', 'ĭ': 'i', 'į': 'i', 'ı': 'i',
'Í': 'I', 'Ì': 'I', 'Î': 'I', 'Ï': 'I', 'Ī': 'I', 'Ĭ': 'I', 'Į': 'I', 'I': 'I',
'ó': 'o', 'ò': 'o', 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'ē': 'e', 'Ē': 'E',
'ň': 'n', 'Ň': 'N',
'ğ': 'g', 'Ğ': 'G',
'ġ': 'g', 'Ġ': 'G',
'ħ': 'h', 'Ħ': 'H',
'ı': 'i', 'İ': 'I',
'ĵ': 'j', 'Ĵ': 'J',
'ķ': 'k', 'Ķ': 'K',
'ļ': 'l', 'Ļ': 'L',
'ņ': 'n', 'Ņ': 'N',
'ŧ': 't', 'Ŧ': 'T',
'ŭ': 'u', 'Ŭ': 'U'
}
for key, value in mappings.items():
text = text.replace(key, value)
return text
# Convert a date to words with an ordinal day
def date_to_words(date_obj):
inf_engine = inflect.engine()
if isinstance(date_obj, (date, datetime)):
year = date_obj.year
month = date_obj.strftime("%B") if hasattr(date_obj, 'month') else None
day_ordinal = number_to_ordinal_word(date_obj.day) if hasattr(date_obj, 'day') else None
elif isinstance(date_obj, str):
try:
# Attempt to parse full date first
date_obj = datetime.strptime(date_obj, "%Y-%m-%d")
year = date_obj.year
month = date_obj.strftime("%B")
day_ordinal = number_to_ordinal_word(date_obj.day)
except ValueError:
try:
# Try year-month
date_obj = datetime.strptime(date_obj, "%Y-%m")
year = date_obj.year
month = date_obj.strftime("%B")
day_ordinal = None
except ValueError:
try:
# Try just year
date_obj = datetime.strptime(date_obj, "%Y")
year = date_obj.year
month = None
day_ordinal = None
except ValueError:
raise ValueError("Invalid date format. Use YYYY-MM-DD, YYYY-MM, or YYYY.")
else:
raise TypeError("date_obj must be a datetime, date, or string object.")
if 1900 <= year <= 1999:
year_words = f"{inf_engine.number_to_words(year // 100, andword='')} hundred"
if year % 100 != 0:
year_words += f" {inf_engine.number_to_words(year % 100, andword='')}"
else:
year_words = inf_engine.number_to_words(year, andword='')
year_formatted = year_words.replace(',', '')
parts = []
if day_ordinal:
parts.append(day_ordinal)
if month:
parts.append(month)
parts.append(year_formatted)
return " ".join(parts)
def translate_date_to_words(date, lang='en'):
"""Converts a date to words in the specified language."""
if date is None:
return "No date selected"
date_string = date.strftime("%Y-%m-%d")
logger.info(f"Date string: {date_string}")
date_in_words = date_to_words(date_string)
logger.info(f"Date in words: {date_in_words}")
translator = GoogleTranslator(source='auto', target=lang)
translated_date_words = translator.translate(date_in_words)
logger.info(f"Translated date words: {translated_date_words}")
# Normalize the text if it contains any special characters
translated_date_words = custom_normalize(translated_date_words)
logger.info(f"Normalized date words: {translated_date_words}")
return translated_date_words