graph_utils.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import pynini
  2. from fun_text_processing.text_normalization.en.graph_utils import DAMO_SIGMA, DAMO_SPACE
  3. from fun_text_processing.text_normalization.es import LOCALIZATION
  4. from fun_text_processing.text_normalization.es.utils import get_abs_path, load_labels
  5. from pynini.lib import pynutil
  6. digits = pynini.project(pynini.string_file(get_abs_path("data/numbers/digit.tsv")), "input")
  7. tens = pynini.project(pynini.string_file(get_abs_path("data/numbers/ties.tsv")), "input")
  8. teens = pynini.project(pynini.string_file(get_abs_path("data/numbers/teen.tsv")), "input")
  9. twenties = pynini.project(pynini.string_file(get_abs_path("data/numbers/twenties.tsv")), "input")
  10. hundreds = pynini.project(pynini.string_file(get_abs_path("data/numbers/hundreds.tsv")), "input")
  11. accents = pynini.string_map([("á", "a"), ("é", "e"), ("í", "i"), ("ó", "o"), ("ú", "u")])
  12. if LOCALIZATION == "am": # Setting localization for central and northern america formatting
  13. cardinal_separator = pynini.string_map([",", DAMO_SPACE])
  14. decimal_separator = pynini.accep(".")
  15. else:
  16. cardinal_separator = pynini.string_map([".", DAMO_SPACE])
  17. decimal_separator = pynini.accep(",")
  18. ones = pynini.union("un", "ún")
  19. fem_ones = pynini.union(pynini.cross("un", "una"), pynini.cross("ún", "una"), pynini.cross("uno", "una"))
  20. one_to_one_hundred = pynini.union(digits, "uno", tens, teens, twenties, tens + pynini.accep(" y ") + digits)
  21. fem_hundreds = hundreds @ pynini.cdrewrite(pynini.cross("ientos", "ientas"), "", "", DAMO_SIGMA)
  22. def strip_accent(fst: 'pynini.FstLike') -> 'pynini.FstLike':
  23. """
  24. Converts all accented vowels to non-accented equivalents
  25. Args:
  26. fst: Any fst. Composes vowel conversion onto fst's output strings
  27. """
  28. return fst @ pynini.cdrewrite(accents, "", "", DAMO_SIGMA)
  29. def shift_cardinal_gender(fst: 'pynini.FstLike') -> 'pynini.FstLike':
  30. """
  31. Applies gender conversion rules to a cardinal string. These include: rendering all masculine forms of "uno" (including apocopated forms) as "una" and
  32. Converting all gendered numbers in the hundreds series (200,300,400...) to feminine equivalent (e.g. "doscientos" -> "doscientas"). Conversion only applies
  33. to value place for <1000 and multiple of 1000. (e.g. "doscientos mil doscientos" -> "doscientas mil doscientas".) For place values greater than the thousands, there
  34. is no gender shift as the higher powers of ten ("millones", "billones") are masculine nouns and any conversion would be formally
  35. ungrammatical.
  36. e.g.
  37. "doscientos" -> "doscientas"
  38. "doscientos mil" -> "doscientas mil"
  39. "doscientos millones" -> "doscientos millones"
  40. "doscientos mil millones" -> "doscientos mil millones"
  41. "doscientos millones doscientos mil doscientos" -> "doscientos millones doscientas mil doscientas"
  42. Args:
  43. fst: Any fst. Composes conversion onto fst's output strings
  44. """
  45. before_mil = (
  46. DAMO_SPACE
  47. + (pynini.accep("mil") | pynini.accep("milésimo"))
  48. + pynini.closure(DAMO_SPACE + hundreds, 0, 1)
  49. + pynini.closure(DAMO_SPACE + one_to_one_hundred, 0, 1)
  50. + pynini.union(pynini.accep("[EOS]"), pynini.accep("\""), decimal_separator)
  51. )
  52. before_double_digits = pynini.closure(DAMO_SPACE + one_to_one_hundred, 0, 1) + pynini.union(
  53. pynini.accep("[EOS]"), pynini.accep("\"")
  54. )
  55. fem_allign = pynini.cdrewrite(fem_hundreds, "", before_mil, DAMO_SIGMA) # doscientas mil dosciento
  56. fem_allign @= pynini.cdrewrite(fem_hundreds, "", before_double_digits, DAMO_SIGMA) # doscientas mil doscienta
  57. fem_allign @= pynini.cdrewrite(
  58. fem_ones, "", pynini.union("[EOS]", "\"", decimal_separator), DAMO_SIGMA
  59. ) # If before a quote or EOS, we know it's the end of a string
  60. return fst @ fem_allign
  61. def shift_number_gender(fst: 'pynini.FstLike') -> 'pynini.FstLike':
  62. """
  63. Performs gender conversion on all verbalized numbers in output. All values in the hundreds series (200,300,400) are changed to
  64. feminine gender (e.g. "doscientos" -> "doscientas") and all forms of "uno" (including apocopated forms) are converted to "una".
  65. This has no boundary restriction and will perform shift across all values in output string.
  66. e.g.
  67. "doscientos" -> "doscientas"
  68. "doscientos millones" -> "doscientas millones"
  69. "doscientos millones doscientos" -> "doscientas millones doscientas"
  70. Args:
  71. fst: Any fst. Composes conversion onto fst's output strings
  72. """
  73. fem_allign = pynini.cdrewrite(fem_hundreds, "", "", DAMO_SIGMA)
  74. fem_allign @= pynini.cdrewrite(
  75. fem_ones, "", pynini.union(DAMO_SPACE, pynini.accep("[EOS]"), pynini.accep("\"")), DAMO_SIGMA
  76. ) # If before a quote or EOS, we know it's the end of a string
  77. return fst @ fem_allign
  78. def strip_cardinal_apocope(fst: 'pynini.FstLike') -> 'pynini.FstLike':
  79. """
  80. Reverts apocope on cardinal strings in line with formation rules. e.g. "un" -> "uno". Due to cardinal formation rules, this in effect only
  81. affects strings where the final value is a variation of "un".
  82. e.g.
  83. "un" -> "uno"
  84. "veintiún" -> "veintiuno"
  85. Args:
  86. fst: Any fst. Composes conversion onto fst's output strings
  87. """
  88. # Since cardinals use apocope by default for large values (e.g. "millón"), this only needs to act on the last instance of one
  89. strip = pynini.cross("un", "uno") | pynini.cross("ún", "uno")
  90. strip = pynini.cdrewrite(strip, "", pynini.union("[EOS]", "\""), DAMO_SIGMA)
  91. return fst @ strip
  92. def add_cardinal_apocope_fem(fst: 'pynini.FstLike') -> 'pynini.FstLike':
  93. """
  94. Adds apocope on cardinal strings in line with stressing rules. e.g. "una" -> "un". This only occurs when "una" precedes a stressed "a" sound in formal speech. This is not predictable
  95. with text string, so is included for non-deterministic cases.
  96. e.g.
  97. "una" -> "un"
  98. "veintiuna" -> "veintiun"
  99. Args:
  100. fst: Any fst. Composes conversion onto fst's output strings
  101. """
  102. # Since the stress trigger follows the cardinal string and only affects the preceding sound, this only needs to act on the last instance of one
  103. strip = pynini.cross("una", "un") | pynini.cross("veintiuna", "veintiún")
  104. strip = pynini.cdrewrite(strip, "", pynini.union("[EOS]", "\""), DAMO_SIGMA)
  105. return fst @ strip
  106. def roman_to_int(fst: 'pynini.FstLike') -> 'pynini.FstLike':
  107. """
  108. Alters given fst to convert Roman integers (lower and upper cased) into Arabic numerals. Valid for values up to 1000.
  109. e.g.
  110. "V" -> "5"
  111. "i" -> "1"
  112. Args:
  113. fst: Any fst. Composes fst onto Roman conversion outputs.
  114. """
  115. def _load_roman(file: str):
  116. roman = load_labels(get_abs_path(file))
  117. roman_numerals = [(x, y) for x, y in roman] + [(x.upper(), y) for x, y in roman]
  118. return pynini.string_map(roman_numerals)
  119. digit = _load_roman("data/roman/digit.tsv")
  120. ties = _load_roman("data/roman/ties.tsv")
  121. hundreds = _load_roman("data/roman/hundreds.tsv")
  122. graph = (
  123. digit
  124. | ties + (digit | pynutil.add_weight(pynutil.insert("0"), 0.01))
  125. | (
  126. hundreds
  127. + (ties | pynutil.add_weight(pynutil.insert("0"), 0.01))
  128. + (digit | pynutil.add_weight(pynutil.insert("0"), 0.01))
  129. )
  130. ).optimize()
  131. return graph @ fst