coding party! OooWee

kollector

New member
Python function that converts a string of a decimal(negative or positive) in digit form to it's word representation
Python:
import inflect

def decimal_to_words(decimal):
    p = inflect.engine()
    if '.' in decimal:
        if decimal.startswith('-'):
            integer_part, decimal_part = decimal[1:].split('.')
            words = 'negative ' + p.number_to_words(integer_part) + ' point ' + ' '.join(p.number_to_words(i) for i in decimal_part)
        else:
            integer_part, decimal_part = decimal.split('.')
            words = p.number_to_words(integer_part) + ' point ' + ' '.join(p.number_to_words(i) for i in decimal_part)
    else:
        words = p.number_to_words(decimal)
    return words

# Example usage:
print(decimal_to_words("-123.45"))
 

fukurou

the supreme coder
ADMIN
Python:
import os
import re

from pygame import mixer
import inflect

from LivinGrimoire23 import RegexUtil, enumRegexGrimoire


def validate_time(time) -> bool:
    pattern = "^([01]?[0-9]|2[0-3]):[0-5][0-9]$"
    result = re.match(pattern, time)
    return result is not None


def number_to_words(number: str):
    # num,decimal,negatives, ret 0 as default
    p = inflect.engine()
    words = p.number_to_words(number)
    words = words.replace("-", " ")
    words = words.replace(",", "")
    return words


def time_to_words(time):
    # five o'clock and nine minutes
    p = inflect.engine()
    hour, minute = map(int, time.split(':'))
    if hour <= 12:
        hour_words = p.number_to_words(hour) + ' o\'clock'
    else:
        hour_words = p.number_to_words(hour - 12) + ' o\'clock'
    if minute != 0:
        minute_words = p.number_to_words(minute)
        return hour_words + ' and ' + minute_words + ' minutes'
    else:
        return hour_words


class OGTTS:
    def __init__(self):
        mixer.init()
        self.regex: RegexUtil = RegexUtil()

    def playSoundFile(self, file: str) -> bool:
        if os.path.isfile(f'sounds/{file}.mp3'):
            mixer.music.load(f'sounds/{file}.mp3')
            mixer.music.play()
            return True
        words = file.split(' ')
        rejoin: bool = False
        for i in range(len(words)):
            if not os.path.isfile(f'sounds/{words[i]}.mp3'):
                if validate_time(words[i]):
                    words[i] = time_to_words(words[i])
                    rejoin = True
                    continue
                elif len(self.regex.extractEnumRegex(enumRegexGrimoire.integer, words[i])) > 0:
                    words[i] = number_to_words(words[i])
                    rejoin = True
                    continue
                else:
                    return False
        # rejoin
        if rejoin:
            line = ""
            for word in words:
                line = f'{line} {word}'
            rejoin = False # redundent
            line = line.strip()
            words = line.split(' ')
            # delete the loop below if all number sound file exist
            for i in range(len(words)):
                if not os.path.isfile(f'sounds/{words[i]}.mp3'):
                    return False
        # play sounds
        for i in range(len(words)):
            mixer.music.load(f'sounds/{words[i]}.mp3')
            mixer.music.play()
        return True
 

fukurou

the supreme coder
ADMIN
Python:
import os
import re

from pygame import mixer
import inflect

from LivinGrimoire23 import RegexUtil, enumRegexGrimoire


def validate_time(time) -> bool:
    pattern = "^([01]?[0-9]|2[0-3]):[0-5][0-9]$"
    result = re.match(pattern, time)
    return result is not None


def number_to_words(number: str):
    # num,decimal,negatives, ret 0 as default
    p = inflect.engine()
    words = p.number_to_words(number)
    words = words.replace("-", " ")
    words = words.replace(",", "")
    return words


def time_to_words(time):
    # five o'clock and nine minutes
    p = inflect.engine()
    hour, minute = map(int, time.split(':'))
    if hour <= 12:
        hour_words = p.number_to_words(hour) + ' o\'clock'
    else:
        hour_words = p.number_to_words(hour - 12) + ' o\'clock'
    if minute != 0:
        minute_words = p.number_to_words(minute)
        return hour_words + ' and ' + minute_words + ' minutes'
    else:
        return hour_words


def timeAdjust(time: str) -> str:
    result:str = time.replace("-", " ")
    return result.replace("o'", "o")


class OGTTS:
    def __init__(self):
        mixer.init()
        self.regex: RegexUtil = RegexUtil()

    def playSoundFile(self, file: str) -> bool:
        if os.path.isfile(f'sounds/{file}.mp3'):
            mixer.music.load(f'sounds/{file}.mp3')
            mixer.music.play()
            return True
        words = file.split(' ')
        rejoin: bool = False
        for i in range(len(words)):
            if not os.path.isfile(f'sounds/{words[i]}.mp3'):
                if validate_time(words[i]):
                    words[i] = timeAdjust(time_to_words(words[i]))
                    rejoin = True
                    continue
                elif len(self.regex.extractEnumRegex(enumRegexGrimoire.integer, words[i])) > 0:
                    words[i] = number_to_words(words[i])
                    rejoin = True
                    continue
                else:
                    return False
        # rejoin
        if rejoin:
            line = ""
            for word in words:
                line = f'{line} {word}'
            rejoin = False  # redundent
            line = line.strip()
            words = line.split(' ')
            # delete the loop below if all number sound file exist
            for i in range(len(words)):
                if not os.path.isfile(f'sounds/{words[i]}.mp3'):
                    return False
        # play sounds
        for i in range(len(words)):
            mixer.music.load(f'sounds/{words[i]}.mp3')
            mixer.music.play()
        return True
 

fukurou

the supreme coder
ADMIN
Python:
import os
import re
import time

from pygame import mixer
import inflect

from LivinGrimoire23 import RegexUtil, enumRegexGrimoire


def validate_time(time) -> bool:
    pattern = "^([01]?[0-9]|2[0-3]):[0-5][0-9]$"
    result = re.match(pattern, time)
    return result is not None


def number_to_words(number: str):
    # num,decimal,negatives, ret 0 as default
    p = inflect.engine()
    words = p.number_to_words(number)
    words = words.replace("-", " ")
    words = words.replace(",", "")
    return words


def time_to_words(time):
    # five o'clock and nine minutes
    p = inflect.engine()
    hour, minute = map(int, time.split(':'))
    if hour <= 12:
        hour_words = p.number_to_words(hour) + ' o\'clock'
    else:
        hour_words = p.number_to_words(hour - 12) + ' o\'clock'
    if minute != 0:
        minute_words = p.number_to_words(minute)
        return hour_words + ' and ' + minute_words + ' minutes'
    else:
        return hour_words


def timeAdjust(time: str) -> str:
    result:str = time.replace("-", " ")
    return result.replace("o'", "o")


class OGTTS:
    def __init__(self):
        mixer.init()
        self.regex: RegexUtil = RegexUtil()

    def playSoundFile(self, file: str) -> bool:
        if os.path.isfile(f'sounds/{file}.mp3'):
            mixer.music.load(f'sounds/{file}.mp3')
            mixer.music.play()
            return True
        words = file.split(' ')
        rejoin: bool = False
        # prep words
        for i in range(len(words)):
            if validate_time(words[i]) and (not os.path.isfile(f'sounds/{words[i]}.mp3')):
                words[i] = timeAdjust(time_to_words(words[i]))
                rejoin = True
            elif len(self.regex.extractEnumRegex(enumRegexGrimoire.integer, words[i])) > 0 and (not os.path.isfile(f'sounds/{words[i]}.mp3')):
                words[i] = number_to_words(words[i])
                rejoin = True
        if rejoin:
            line = ""
            for word in words:
                line = f'{line} {word}'
            rejoin = False  # redundent
            line = line.strip()
            words = line.split(' ')
        # delete the loop below if all number sound file exist
        for i in range(len(words)):
            if not os.path.isfile(f'sounds/{words[i]}.mp3'):
                print(words[i])
                return False
        # play sounds
        for i in range(len(words)):
            mixer.music.load(f'sounds/{words[i]}.mp3')
            mixer.music.play()
            while mixer.music.get_busy():
                time.sleep(0.1)  # wait 100
        return True
 
Top