def load_model(path_to_saved_model: str) -> NGramTextGenerator: if not isinstance(path_to_saved_model, str): raise ValueError try: with open(path_to_saved_model, 'r') as file: model = json.load(file) word_storage = WordStorage() word_storage.storage = model['word_storage'] trie = NGramTrie(n_gram_size=int(model['n_gram_trie_size']), encoded_text=('he', ) * int(model['n_gram_trie_size'])) trie.n_grams = tuple([tuple(n_gram) for n_gram in model['n_grams']]) trie.n_gram_frequencies = { tuple(map(int, key.split(', '))): value for key, value in model['n_gram_trie_frequencies'].items() } trie.uni_grams = {(int(key), ): value for key, value in model['uni_grams'].items()} model_generator = NGramTextGenerator(word_storage, trie) return model_generator except FileNotFoundError as error: raise FileNotFoundError from error
def load_model(path_to_saved_model: str) -> NGramTextGenerator: if not isinstance(path_to_saved_model, str): raise ValueError with open(path_to_saved_model + '.json', 'r') as json_file: generator_json = json.load(json_file) words = WordStorage() words.storage = generator_json['_word_storage']['storage'] trie = NGramTrie(generator_json['_n_gram_trie']['size'], (0, 1)) trie.encoded_text = generator_json['_n_gram_trie']['encoded_text'] trie.n_grams = tuple( tuple(gram) for gram in generator_json['_n_gram_trie']['n_grams']) trie.n_gram_frequencies = { eval(key): value for key, value in generator_json['_n_gram_trie'] ['n_gram_frequencies'].items() } trie.uni_grams = { eval(key): value for key, value in generator_json['_n_gram_trie'] ['uni_grams'].items() } return NGramTextGenerator(words, trie)