Skip to content

Commit

Permalink
Make numbers more consistent (#1592)
Browse files Browse the repository at this point in the history
Makes numbers more consistent between prose formatters and the global
number command.

Increase consistency and additional delimiters for different locales

Fixes #1245

---------

Co-authored-by: Jeff Knaus <[email protected]>
  • Loading branch information
AndreasArvidsson and knausj85 authored Nov 24, 2024
1 parent c2365bb commit f27cb01
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
50 changes: 39 additions & 11 deletions core/numbers/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,58 @@ def number_string(m) -> str:
return parse_number(list(m))


@mod.capture(rule="<user.number_string> ((point | dot) <user.number_string>)+")
def number_decimal_string(m) -> str:
"""Parses a decimal number phrase, returning that number as a string."""
return ".".join(m.number_string_list)


@ctx.capture("number", rule="<user.number_string>")
def number(m) -> int:
"""Parses a number phrase, returning it as an integer."""
return int(m.number_string)


@ctx.capture("number_signed", rule=f"[negative|minus] <number>")
def number_signed(m):
number = m[-1]
return -number if (m[0] in ["negative", "minus"]) else number
@mod.capture(rule="[negative | minus] <user.number_string>")
def number_signed_string(m) -> str:
"""Parses a (possibly negative) number phrase, returning that number as a string."""
number = m.number_string
return f"-{number}" if (m[0] in ["negative", "minus"]) else number


@ctx.capture("number_signed", rule="<user.number_signed_string>")
def number_signed(m) -> int:
"""Parses a (possibly negative) number phrase, returning that number as a integer."""
return int(m.number_signed_string)


@mod.capture(rule="<user.number_string> ((dot | point) <user.number_string>)+")
def number_prose_with_dot(m) -> str:
return ".".join(m.number_string_list)


@mod.capture(rule="<user.number_string> (comma <user.number_string>)+")
def number_prose_with_comma(m) -> str:
return ",".join(m.number_string_list)


@mod.capture(rule="<user.number_string> (colon <user.number_string>)+")
def number_prose_with_colon(m) -> str:
return ":".join(m.number_string_list)


@mod.capture(
rule="<user.number_signed_string> | <user.number_prose_with_dot> | <user.number_prose_with_comma> | <user.number_prose_with_colon>"
)
def number_prose_unprefixed(m) -> str:
return m[0]


@mod.capture(rule="(numb | numeral) <user.number_prose_unprefixed>")
def number_prose_prefixed(m) -> str:
return m.number_prose_unprefixed


@ctx.capture("number_small", rule="{user.number_small}")
def number_small(m) -> int:
return int(m.number_small)


@mod.capture(rule=f"[negative|minus] <number_small>")
@mod.capture(rule="[negative | minus] <number_small>")
def number_signed_small(m) -> int:
"""Parses an integer between -99 and 99."""
number = m[-1]
Expand Down
2 changes: 0 additions & 2 deletions core/numbers/numbers.talon

This file was deleted.

1 change: 1 addition & 0 deletions core/numbers/numbers_prefixed.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<user.number_prose_prefixed>: "{number_prose_prefixed}"
3 changes: 1 addition & 2 deletions core/numbers/numbers_unprefixed.talon
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tag: user.unprefixed_numbers
-

<user.number_string>: "{number_string}"
<user.number_decimal_string>: "{number_decimal_string}"
<user.number_prose_unprefixed>: "{number_prose_unprefixed}"
26 changes: 2 additions & 24 deletions core/text/text_and_dictation.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,6 @@ def prose_modifier(m) -> Callable:
return getattr(DictationFormat, m.prose_modifiers)


@mod.capture(rule="(numb | numeral) <user.number_string>")
def prose_simple_number(m) -> str:
return m.number_string


@mod.capture(rule="(numb | numeral) <user.number_string> (dot | point) <digit_string>")
def prose_number_with_dot(m) -> str:
return m.number_string + "." + m.digit_string


@mod.capture(rule="(numb | numeral) <user.number_string> colon <user.number_string>")
def prose_number_with_colon(m) -> str:
return m.number_string_1 + ":" + m.number_string_2


@mod.capture(
rule="<user.prose_simple_number> | <user.prose_number_with_dot> | <user.prose_number_with_colon>"
)
def prose_number(m) -> str:
return str(m)


@mod.capture(
rule="<user.number_string> [(dot | point) <digit_string>] percent [sign|sine]"
)
Expand Down Expand Up @@ -168,7 +146,7 @@ def text(m) -> str:


@mod.capture(
rule="(<phrase> | {user.vocabulary} | {user.punctuation} | {user.prose_snippets} | <user.prose_currency> | <user.prose_time> | <user.prose_number> | <user.prose_percent> | <user.prose_modifier> | <user.abbreviation>)+"
rule="({user.vocabulary} | {user.punctuation} | {user.prose_snippets} | <user.prose_currency> | <user.prose_time> | <user.number_prose_prefixed> | <user.prose_percent> | <user.prose_modifier> | <user.abbreviation> | <phrase>)+"
)
def prose(m) -> str:
"""Mixed words and punctuation, auto-spaced & capitalized."""
Expand All @@ -177,7 +155,7 @@ def prose(m) -> str:


@mod.capture(
rule="(<phrase> | {user.vocabulary} | {user.punctuation} | {user.prose_snippets} | <user.prose_currency> | <user.prose_time> | <user.prose_number> | <user.prose_percent> | <user.abbreviation>)+"
rule="({user.vocabulary} | {user.punctuation} | {user.prose_snippets} | <user.prose_currency> | <user.prose_time> | <user.number_prose_prefixed> | <user.prose_percent> | <user.abbreviation> | <phrase>)+"
)
def raw_prose(m) -> str:
"""Mixed words and punctuation, auto-spaced & capitalized, without quote straightening and commands (for use in dictation mode)."""
Expand Down

0 comments on commit f27cb01

Please sign in to comment.