def numbers_variations(string, language): variations = set() if not supports_num2words(language): return variations number_entities = get_builtin_entities( string, language, scope=[SNIPS_NUMBER]) number_entities = sorted(number_entities, key=lambda x: x[RES_MATCH_RANGE][START]) if not number_entities: return variations digit_values = [digit_value(e) for e in number_entities] alpha_values = [alphabetic_value(e, language) for e in number_entities] values = [(n[RES_MATCH_RANGE], (d, a)) for (n, d, a) in zip(number_entities, digit_values, alpha_values) if a is not None] combinations = itertools.product(range(2), repeat=len(values)) for c in combinations: ranges_and_utterances = [(values[i][0], values[i][1][ix]) for i, ix in enumerate(c)] variations.add(build_variated_query(string, ranges_and_utterances)) return variations
def numbers_variations(string, language, builtin_entity_parser): if not supports_num2words(language): return set() number_entities = builtin_entity_parser.parse(string, scope=[SNIPS_NUMBER], use_cache=True) number_entities = sorted(number_entities, key=lambda x: x[RES_MATCH_RANGE][START]) if not number_entities: return set() digit_values = [digit_value(e) for e in number_entities] alpha_values = [alphabetic_value(e, language) for e in number_entities] values = [(n[RES_MATCH_RANGE], (d, a)) for (n, d, a) in zip(number_entities, digit_values, alpha_values) if a is not None] n_values = len(values) if 2**n_values > MAX_ENTITY_VARIATIONS: return set() combinations = itertools.product(range(2), repeat=n_values) variations = set() for c in combinations: ranges_and_utterances = [(values[i][0], values[i][1][ix]) for i, ix in enumerate(c)] variations.add(build_variated_query(string, ranges_and_utterances)) return variations
def numbers_variations(string, language): variations = set() if not supports_num2words(language): return variations number_entities = get_builtin_entities(string, language, scope=[SNIPS_NUMBER]) number_entities = sorted(number_entities, key=lambda x: x[RES_MATCH_RANGE][START]) if not number_entities: return variations digit_values = [digit_value(e) for e in number_entities] alpha_values = [alphabetic_value(e, language) for e in number_entities] values = [(n[RES_MATCH_RANGE], (d, a)) for (n, d, a) in zip(number_entities, digit_values, alpha_values) if a is not None] combinations = itertools.product(range(2), repeat=len(values)) for c in combinations: ranges_and_utterances = [(values[i][0], values[i][1][ix]) for i, ix in enumerate(c)] variations.add(build_variated_query(string, ranges_and_utterances)) return variations