decimals.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import pynini
  2. from fun_text_processing.text_normalization.en.graph_utils import (
  3. DAMO_NOT_QUOTE,
  4. GraphFst,
  5. delete_preserve_order,
  6. delete_space,
  7. insert_space,
  8. )
  9. from fun_text_processing.text_normalization.es import LOCALIZATION
  10. from fun_text_processing.text_normalization.es.graph_utils import (
  11. add_cardinal_apocope_fem,
  12. shift_cardinal_gender,
  13. shift_number_gender,
  14. strip_cardinal_apocope,
  15. )
  16. from pynini.lib import pynutil
  17. class DecimalFst(GraphFst):
  18. """
  19. Finite state transducer for classifying decimal, e.g.
  20. decimal { negative: "true" integer_part: "dos" fractional_part: "cuatro cero" quantity: "billones" } -> menos dos coma quatro cero billones
  21. decimal { integer_part: "un" quantity: "billón" } -> un billón
  22. Args:
  23. deterministic: if True will provide a single transduction option,
  24. for False multiple transduction are generated (used for audio-based normalization)
  25. """
  26. def __init__(self, deterministic: bool = True):
  27. super().__init__(name="decimal", kind="classify", deterministic=deterministic)
  28. optional_sign = pynini.closure(pynini.cross("negative: \"true\"", "menos ") + delete_space, 0, 1)
  29. integer = pynutil.delete("integer_part: \"") + pynini.closure(DAMO_NOT_QUOTE, 1) + pynutil.delete("\"")
  30. fractional_default = (
  31. pynutil.delete("fractional_part: \"") + pynini.closure(DAMO_NOT_QUOTE, 1) + pynutil.delete("\"")
  32. )
  33. conjunction = pynutil.insert(" punto ") if LOCALIZATION == "am" else pynutil.insert(" coma ")
  34. if not deterministic:
  35. conjunction |= pynutil.insert(pynini.union(" con ", " y "))
  36. fractional_default |= strip_cardinal_apocope(fractional_default)
  37. fractional = conjunction + fractional_default
  38. quantity = (
  39. delete_space
  40. + insert_space
  41. + pynutil.delete("quantity: \"")
  42. + pynini.closure(DAMO_NOT_QUOTE, 1)
  43. + pynutil.delete("\"")
  44. )
  45. optional_quantity = pynini.closure(quantity, 0, 1)
  46. graph_masc = optional_sign + pynini.union(
  47. (integer + quantity), (integer + delete_space + fractional + optional_quantity)
  48. )
  49. # Allowing permutation for fem gender, don't include quantity since "million","billion", etc.. are masculine
  50. graph_fem = optional_sign + (shift_cardinal_gender(integer) + delete_space + shift_number_gender(fractional))
  51. if not deterministic: # "una" will drop to "un" in certain cases
  52. graph_fem |= add_cardinal_apocope_fem(graph_fem)
  53. self.numbers_only_quantity = (
  54. optional_sign
  55. + pynini.union((integer + quantity), (integer + delete_space + fractional + quantity)).optimize()
  56. )
  57. self.graph_masc = (graph_masc + delete_preserve_order).optimize()
  58. self.graph_fem = (graph_fem + delete_preserve_order).optimize()
  59. graph = graph_masc | graph_fem
  60. graph += delete_preserve_order
  61. delete_tokens = self.delete_tokens(graph)
  62. self.fst = delete_tokens.optimize()