electronic.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright NeMo (https://github.com/NVIDIA/NeMo). All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import pynini
  15. from fun_text_processing.text_normalization.de.utils import get_abs_path
  16. from fun_text_processing.text_normalization.en.graph_utils import (
  17. DAMO_NOT_QUOTE,
  18. DAMO_SIGMA,
  19. GraphFst,
  20. delete_preserve_order,
  21. insert_space,
  22. )
  23. from pynini.lib import pynutil
  24. class ElectronicFst(GraphFst):
  25. """
  26. Finite state transducer for verbalizing electronic
  27. e.g. electronic { username: "abc" domain: "hotmail.com" } -> "a b c at hotmail punkt com"
  28. -> "a b c at h o t m a i l punkt c o m"
  29. -> "a b c at hotmail punkt c o m"
  30. -> "a b c at h o t m a i l punkt com"
  31. Args:
  32. deterministic: if True will provide a single transduction option,
  33. for False multiple transduction are generated (used for audio-based normalization)
  34. """
  35. def __init__(self, deterministic: bool = True):
  36. super().__init__(name="electronic", kind="verbalize", deterministic=deterministic)
  37. graph_digit_no_zero = pynini.invert(
  38. pynini.string_file(get_abs_path("data/numbers/digit.tsv"))
  39. ).optimize() | pynini.cross("1", "eins")
  40. graph_zero = pynini.invert(pynini.string_file(get_abs_path("data/numbers/zero.tsv"))).optimize()
  41. graph_digit = graph_digit_no_zero | graph_zero
  42. graph_symbols = pynini.string_file(get_abs_path("data/electronic/symbols.tsv")).optimize()
  43. server_common = pynini.string_file(get_abs_path("data/electronic/server_name.tsv"))
  44. domain_common = pynini.string_file(get_abs_path("data/electronic/domain.tsv"))
  45. def add_space_after_char():
  46. return pynini.closure(DAMO_NOT_QUOTE - pynini.accep(" ") + insert_space) + (
  47. DAMO_NOT_QUOTE - pynini.accep(" ")
  48. )
  49. verbalize_characters = pynini.cdrewrite(graph_symbols | graph_digit, "", "", DAMO_SIGMA)
  50. user_name = pynutil.delete("username: \"") + add_space_after_char() + pynutil.delete("\"")
  51. user_name @= verbalize_characters
  52. convert_defaults = pynutil.add_weight(DAMO_NOT_QUOTE, weight=0.0001) | domain_common | server_common
  53. domain = convert_defaults + pynini.closure(insert_space + convert_defaults)
  54. domain @= verbalize_characters
  55. domain = pynutil.delete("domain: \"") + domain + pynutil.delete("\"")
  56. protocol = (
  57. pynutil.delete("protocol: \"")
  58. + add_space_after_char() @ pynini.cdrewrite(graph_symbols, "", "", DAMO_SIGMA)
  59. + pynutil.delete("\"")
  60. )
  61. self.graph = (pynini.closure(protocol + pynini.accep(" "), 0, 1) + domain) | (
  62. user_name + pynini.accep(" ") + pynutil.insert("at ") + domain
  63. )
  64. delete_tokens = self.delete_tokens(self.graph + delete_preserve_order)
  65. self.fst = delete_tokens.optimize()