Ejemplo n.º 1
0
 def create_from_save_state_dict(
     cls,
     save_state: dict,
     new_type_context: TypeContext
 ) -> 'TypeContextWrapperVocab':
     instance = cls.__new__(cls)
     instance.itos = np.array([
         new_type_context.get_type_by_name(name) if is_type
         else new_type_context.get_object_by_name(name)
         for is_type, name in save_state['itos']
     ])
     instance._finish_init()
     return instance
Ejemplo n.º 2
0
def _create_all_word_parts(tc: TypeContext, word_part_strs: List[Tuple[str,
                                                                       bool]]):
    symb_trie = pygtrie.CharTrie()
    for symb, allow_mod in word_part_strs:
        new_part = _create_word_part_obj(tc, symb, allow_mod)
        symb_trie[symb] = new_part
        if allow_mod:
            symb_trie[symb.upper()] = new_part
            first_letter_upper_version = symb[0].upper() + symb[1:]
            symb_trie[first_letter_upper_version] = new_part
    symb_trie[""] = tc.get_object_by_name(WORD_PART_TERMINAL_NAME)

    def word_parser_func(run: parse_primitives.TypeParserRun, string: str,
                         result: parse_primitives.TypeParserResult):
        result.set_valid_implementation(symb_trie.longest_prefix(string).value)
        result.set_next_slice(0, len(string))

    TypeParser(tc, WORD_PART_TYPE_PARSER_NAME, word_parser_func)