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, needs rewriting
Language Code | Pronounce Number | Pronounce Ordinal | Extract Number | numbers_to_digits |
---|---|---|---|---|
en (English) |
โ | โ | โ | โ |
az (Azerbaijani) |
โ | โ | โ | โ |
ca (Catalan) |
โ | โ | โ | ๐ง |
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) |
โ | โ | โ | ๐ง |
ru (Russian) |
โ | โ | โ | โ |
sv (Swedish) |
โ | โ | โ | โ |
sl (Slovenian) |
โ | โ | โ | โ |
uk (Ukrainian) |
โ | โ | โ | โ |
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
- ovos-date-parser - for handling dates and times
- ovos-lang-parser - for handling languages
- ovos-color-parser - for handling colors
License
This project is licensed under the Apache License 2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ovos-number-parser-0.0.2a1.tar.gz
.
File metadata
- Download URL: ovos-number-parser-0.0.2a1.tar.gz
- Upload date:
- Size: 110.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 671f4bfda5bf30e7ecb8afde8ff388c46c719a6090e612b69d12529e1b5acbe5 |
|
MD5 | 0fc80fc618d7640c1663f6bd9fb6f2ed |
|
BLAKE2b-256 | d16d17a3b8d42245b1c2db8e273825e467c8e04514f0fbda9eff59a652f5af98 |
File details
Details for the file ovos_number_parser-0.0.2a1-py3-none-any.whl
.
File metadata
- Download URL: ovos_number_parser-0.0.2a1-py3-none-any.whl
- Upload date:
- Size: 130.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdf75d93aadb70b5cc76b2c6f9b4e2345d98528901e261a254741d19f59911f3 |
|
MD5 | 46ebc03a86ae615c26cd00db9ce65489 |
|
BLAKE2b-256 | 97c81adefeb73996ab72e05c3a773d59d2a7431a3ad63080c12ea710db39c315 |