-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from rhasspy/synesthesiam-20241118-fix-loading
Ensure all languages load
- Loading branch information
Showing
11 changed files
with
95 additions
and
804 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from unicode_rbnf.decimal_format import format_decimal | ||
|
||
|
||
def test_format_decimal() -> None: | ||
assert format_decimal(12345.6789, "#,##0.00") == "12,345.68" | ||
assert format_decimal(5, "0000.00") == "0005.00" | ||
assert format_decimal(12345.6, "#,##0.0#") == "12,345.6" | ||
assert format_decimal(0.1, "#,##0.00") == "0.10" | ||
assert format_decimal(12345, "#,##0") == "12,345" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from unicode_rbnf import RbnfEngine | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("language", RbnfEngine.get_supported_languages()) | ||
def test_load_language(language: str): | ||
engine = RbnfEngine.for_language(language) | ||
assert engine.format_number(0).text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.0.0 | ||
2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Handle decimal formatting. | ||
See: https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1DecimalFormat.html | ||
""" | ||
from decimal import Decimal | ||
from typing import Union | ||
|
||
|
||
def format_decimal(value: Union[int, float, str, Decimal], pattern: str) -> str: | ||
"""Format a number according to a simplified ICU DecimalFormat pattern.""" | ||
# Split the pattern into integer and fractional parts | ||
if "." in pattern: | ||
integer_part, fractional_part = pattern.split(".") | ||
else: | ||
integer_part, fractional_part = pattern, "" | ||
|
||
# Determine grouping (e.g., thousands separator) | ||
grouping = "," in integer_part | ||
min_integer_digits = integer_part.replace(",", "").count("0") | ||
|
||
# Determine the number of decimal places | ||
min_fraction_digits = fractional_part.count("0") | ||
max_fraction_digits = len(fractional_part) | ||
|
||
# Round the number to the maximum fractional digits | ||
format_str = f"{{:.{max_fraction_digits}f}}" | ||
rounded_value = format_str.format(value) | ||
|
||
# Split the rounded value into integer and fractional parts | ||
if fractional_part: | ||
integer_value, fractional_value = rounded_value.split(".") | ||
fractional_value = fractional_value[:max_fraction_digits].rstrip("0") | ||
else: | ||
integer_value, fractional_value = rounded_value, "" | ||
|
||
# Apply integer padding | ||
if len(integer_value) < min_integer_digits: | ||
integer_value = integer_value.zfill(min_integer_digits) | ||
|
||
# Apply grouping | ||
if grouping: | ||
# pylint: disable=consider-using-f-string | ||
integer_value = "{:,}".format(int(integer_value)) | ||
|
||
# Combine integer and fractional parts | ||
if min_fraction_digits > 0: | ||
fractional_value = fractional_value.ljust(min_fraction_digits, "0") | ||
formatted_number = f"{integer_value}.{fractional_value}" | ||
else: | ||
formatted_number = integer_value | ||
|
||
return formatted_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.