Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- import pickle
- import random
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument('way_to_model', '--model ', type=str,
- help='The way to directory with model')
- parser.add_argument('way_to_output_file', "--output", type=str,
- help='The way to output file')
- parser.add_argument('first_word', '--seed', type=str,
- help='The starting word')
- parser.add_argument('length_of_text', '--length', type=int,
- help='The length of the generated text')
- parser.add_argument('Help', '--help', action=store_true,
- help='Show the utility reference')
- args = parser.parse_args()
- def frequency_choice(frequencies):
- total = 0
- for key in frequencies:
- total += frequencies.get(key)
- r = random.uniform(0, total)
- up_to = 0
- for key in frequencies:
- if up_to + frequencies.get(key) >= r:
- return key
- up_to += frequencies.get(key)
- def generate_text(way_to_model, first_word, length_of_text,
- way_to_output_file):
- dictionary = collections.OrderedDict()
- if (first_word is None):
- list_of_keys = dictionary.keys()
- num = random.randint(0, len(list_of_keys))
- first_word = list_of_keys[num]
- with open('{}/model.pkl'.format(way_to_output_file), 'rb') as model:
- dictionary = pickle.load(model)
- num_of_words = length_of_text
- new_word = first_word
- space = " "
- # next_words_list = collections.OrderedDict()
- with open('{}/generated_text.txt'.format(way_to_output_file),
- 'w', encoding="utf-8") as f:
- for i in range(num_of_words):
- next_words_dict = dictionary.get(new_word)
- f.write(str(new_word) + space)
- new_word = None
- while (new_word is None):
- new_word = frequency_choice(next_words_dict)
- next_words_dict.clear()
- if (args.Help):
- print('''Help :
- It is the utility for generate text on the basis of model.
- You should specify the directory with model file :
- --model <dir_name> .
- Also you can specify a directory in which it
- is required to keep generated text or use the stdout:
- --model <dir_name> . Stdout use by default.
- If you want to choice the seed-word, you should use --seed:
- --seed <your_word>
- else it will be chosen accidentally.
- And you should input the lenght of generated text:
- --lengs <natural_num>.
- SUDDENLY use --help to see this text.''')
- if (args.way_to_output_file is None):
- args.way_to_output_file = 'stdout_output.txt'
- generate_text(args.way_to_model, args.first_word,
- args.length_of_text, args.way_to_output_file)
- if (args.way_to_output_file is None):
- with open('{}/generated_text.txt'.format(args.way_to_output_file),
- 'r', encoding="utf-8") as f:
- for line in f:
- print(line)
Advertisement
Add Comment
Please, Sign In to add comment