Skip to main content

OpenVoiceOS's multilingual text parsing and formatting library

Project description

OVOS Number Parser

OVOS Number Parser is a tool for extracting, pronouncing, and detecting numbers from text across multiple languages. It supports functionalities like converting numbers to their spoken forms, extracting numbers from text, identifying fractional and ordinal numbers, and more.

Features

  • Pronounce Numbers: Converts numerical values to their spoken forms.
  • Pronounce Ordinals: Converts numbers to their ordinal forms.
  • Extract Numbers: Extracts numbers from textual inputs.
  • Detect Fractions: Identifies fractional expressions.
  • Detect Ordinals: Checks if a text input contains an ordinal number.

Supported Languages

  • โœ… - supported
  • โŒ - not supported
  • ๐Ÿšง - imperfect placeholder, usually a language agnostic implementation or external library
Language Code Pronounce Number Pronounce Ordinal Extract Number numbers_to_digits
en (English) โœ… ๐Ÿšง โœ… โœ…
az (Azerbaijani) โœ… ๐Ÿšง โœ… โœ…
ca (Catalan) โœ… ๐Ÿšง โœ… ๐Ÿšง
gl (Galician) โœ… โŒ โœ… ๐Ÿšง
cs (Czech) โœ… ๐Ÿšง โœ… โœ…
da (Danish) โœ… โœ… โœ… ๐Ÿšง
de (German) โœ… โœ… โœ… โœ…
es (Spanish) โœ… ๐Ÿšง โœ… ๐Ÿšง
eu (Euskara / Basque) โœ… โŒ โœ… โŒ
fa (Farsi / Persian) โœ… ๐Ÿšง โœ… โŒ
fr (French) โœ… ๐Ÿšง โœ… โŒ
hu (Hungarian) โœ… โœ… โŒ โŒ
it (Italian) โœ… ๐Ÿšง โœ… โŒ
nl (Dutch) โœ… โœ… โœ… โœ…
pl (Polish) โœ… ๐Ÿšง โœ… โœ…
pt (Portuguese) โœ… โœ… โœ… โœ…
mwl (Mirandese) โœ… โœ… โœ… โœ…
ru (Russian) โœ… ๐Ÿšง โœ… โœ…
sv (Swedish) โœ… โœ… โœ… โŒ
sl (Slovenian) โœ… ๐Ÿšง โŒ โŒ
uk (Ukrainian) โœ… ๐Ÿšง โœ… โœ…

๐Ÿ’ก If a language is not implemented for pronounce_number or pronounce_ordinal then unicode-rbnf will be used as a fallback

Installation

To install OVOS Number Parser, use:

pip install ovos-number-parser

Usage

Pronounce a Number

Convert a number to its spoken equivalent.

def pronounce_number(number: Union[int, float], lang: str, places: int = 2, short_scale: bool = True,
                     scientific: bool = False, ordinals: bool = False) -> str:
    """
    Convert a number to its spoken equivalent.

    Args:
        number: The number to pronounce.
        lang (str): A BCP-47 language code.
        places (int): Number of decimal places to express. Default is 2.
        short_scale (bool): Use short (True) or long scale (False) for large numbers.
        scientific (bool): Pronounce in scientific notation if True.
        ordinals (bool): Pronounce as an ordinal if True.

    Returns:
        str: The pronounced number.
    """

Example Usage:

from ovos_number_parser import pronounce_number

# Example
result = pronounce_number(123, "en")
print(result)  # "one hundred and twenty-three"

Pronounce an Ordinal

Convert a number to its ordinal spoken equivalent.

def pronounce_ordinal(number: Union[int, float], lang: str, short_scale: bool = True) -> str:
    """
    Convert an ordinal number to its spoken equivalent.

    Args:
        number: The number to pronounce.
        lang (str): A BCP-47 language code.
        short_scale (bool): Use short (True) or long scale (False) for large numbers.

    Returns:
        str: The pronounced ordinal number.
    """

Example Usage:

from ovos_number_parser import pronounce_ordinal

# Example
result = pronounce_ordinal(5, "en")
print(result)  # "fifth"

Extract a Number

Extract a number from a given text string.

def extract_number(text: str, lang: str, short_scale: bool = True, ordinals: bool = False) -> Union[int, float, bool]:
    """
    Extract a number from text.

    Args:
        text (str): The string to extract a number from.
        lang (str): A BCP-47 language code.
        short_scale (bool): Use short scale if True, long scale if False.
        ordinals (bool): Consider ordinal numbers.

    Returns:
        int, float, or False: The extracted number, or False if no number found.
    """

Example Usage:

from ovos_number_parser import extract_number

# Example
result = extract_number("I have twenty apples", "en")
print(result)  # 20

Check for Fractional Numbers

Identify if the text contains a fractional number.

def is_fractional(input_str: str, lang: str, short_scale: bool = True) -> Union[bool, float]:
    """
    Check if the text is a fraction.

    Args:
        input_str (str): The string to check if fractional.
        lang (str): A BCP-47 language code.
        short_scale (bool): Use short scale if True, long scale if False.

    Returns:
        bool or float: False if not a fraction, otherwise the fraction as a float.
    """

Example Usage:

from ovos_number_parser import is_fractional

# Example
result = is_fractional("half", "en")
print(result)  # 0.5

Check for Ordinals

Determine if the text contains an ordinal number.

def is_ordinal(input_str: str, lang: str) -> Union[bool, float]:
    """
    Check if the text is an ordinal number.

    Args:
        input_str (str): The string to check if ordinal.
        lang (str): A BCP-47 language code.

    Returns:
        bool or float: False if not an ordinal, otherwise the ordinal as a float.
    """

Example Usage:

from ovos_number_parser import is_ordinal

# Example
result = is_ordinal("third", "en")
print(result)  # 3

Related Projects

License

This project is licensed under the Apache License 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ovos-number-parser-0.5.0a1.tar.gz (126.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ovos_number_parser-0.5.0a1-py3-none-any.whl (149.2 kB view details)

Uploaded Python 3

File details

Details for the file ovos-number-parser-0.5.0a1.tar.gz.

File metadata

  • Download URL: ovos-number-parser-0.5.0a1.tar.gz
  • Upload date:
  • Size: 126.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for ovos-number-parser-0.5.0a1.tar.gz
Algorithm Hash digest
SHA256 ce2cda7f996b592f754b8f44b225499b4e14096f690b9f3f0c1525b4c3498e8f
MD5 1232777ea34e8307cf61c30e2c0d6f30
BLAKE2b-256 c3b197016067b369194bd6f9159e1369d03f33b9c0c4b0c8aa1d430ccc2bfd2f

See more details on using hashes here.

File details

Details for the file ovos_number_parser-0.5.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for ovos_number_parser-0.5.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 03ffe0e822ec4f637ed215540a3ed73d6e5361584f7d23c49e77aec3bf059e9b
MD5 80230467218c531556ea83e705dd9d20
BLAKE2b-256 6839af287ec9d43234f7c7492cdf62655ac6083ffcd4c4778bfbb33c9f3ab771

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page