def number_transform(word): """Automatically transform numbers""" if not isinstance(word, jsgf.Word): # Skip anything besides words return match = NUMBER_PATTERN.match(word.text) or NUMBER_RANGE_PATTERN.match( word.text) if not match: return try: lower_bound = int(match.group(1)) upper_bound = lower_bound if len(match.groups()) > 1: upper_bound = int(match.group(2)) if upper_bound < lower_bound: # Swap bounds temp_bound = lower_bound lower_bound = upper_bound upper_bound = lower_bound # Create alternative with all numbers in range num_alt = jsgf.Sequence(text="", type=jsgf.SequenceType.ALTERNATIVE, converters=["int"]) for n in range(lower_bound, upper_bound + 1): # 75 -> (seventy five):75!int number_text = num2words(n, lang=language).replace("-", " ").strip() assert number_text, f"Empty num2words result for {n}" number_words = number_text.split() if len(number_words) == 1: # Easy case, single word num_alt.items.append( jsgf.Word(text=number_text, substitution=str(n))) else: # Hard case, split into mutliple Words num_alt.items.append( jsgf.Sequence( text=number_text, type=jsgf.SequenceType.GROUP, substitution=str(n), converters=["int"], items=[jsgf.Word(w) for w in number_words], )) return num_alt except ValueError: # Not a number pass
def number_transform(word): """Automatically transform numbers""" if not isinstance(word, jsgf.Word): # Skip anything besides words return try: n = int(word.text) # 75 -> (seventy five):75 number_text = num2words(n, lang=language).replace("-", " ").strip() assert number_text, f"Empty num2words result for {n}" number_words = number_text.split() if len(number_words) == 1: # Easy case, single word word.text = number_text word.substitution = str(n) else: # Hard case, split into mutliple Words return jsgf.Sequence( text=number_text, type=jsgf.SequenceType.GROUP, substitution=str(n), items=[jsgf.Word(w) for w in number_words], ) except ValueError: # Not a number pass
def number_transform(word): """Automatically transform numbers""" if not isinstance(word, jsgf.Word): # Skip anything besides words return match = NUMBER_PATTERN.match(word.text) if not match: return try: n = int(match.group(1)) # 75 -> (seventy five):75!int number_text = re.sub(r"[-,]\s*", " ", num2words(n, lang=language)).strip() assert number_text, f"Empty num2words result for {n}" number_words = number_text.split() if len(number_words) == 1: # Easy case, single word word.text = number_text word.substitution = str(n) word.converters = ["int"] return word # Hard case, split into multiple Words return jsgf.Sequence( text=number_text, type=jsgf.SequenceType.GROUP, substitution=str(n), converters=["int"], items=[jsgf.Word(w) for w in number_words], ) except ValueError: # Not a number pass except Exception: _LOGGER.exception("number_transform")