SpaceCreator

generate

Mar 13th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. import collections
  2. import pickle
  3. import random
  4. import argparse
  5.  
  6. parser = argparse.ArgumentParser()
  7. parser.add_argument('way_to_model', '--model ', type=str,
  8.                     help='The way to directory with model')
  9. parser.add_argument('way_to_output_file', "--output", type=str,
  10.                     help='The way to output file')
  11. parser.add_argument('first_word', '--seed', type=str,
  12.                     help='The starting word')
  13. parser.add_argument('length_of_text', '--length', type=int,
  14.                     help='The length of the generated text')
  15. parser.add_argument('Help', '--help', action=store_true,
  16.                     help='Show the utility reference')
  17. args = parser.parse_args()
  18.  
  19.  
  20. def frequency_choice(frequencies):
  21.     total = 0
  22.     for key in frequencies:
  23.         total += frequencies.get(key)
  24.     r = random.uniform(0, total)
  25.     up_to = 0
  26.     for key in frequencies:
  27.         if up_to + frequencies.get(key) >= r:
  28.             return key
  29.         up_to += frequencies.get(key)
  30.  
  31.  
  32. def generate_text(way_to_model, first_word, length_of_text,
  33.                   way_to_output_file):
  34.     dictionary = collections.OrderedDict()
  35.     if (first_word is None):
  36.         list_of_keys = dictionary.keys()
  37.         num = random.randint(0, len(list_of_keys))
  38.         first_word = list_of_keys[num]
  39.     with open('{}/model.pkl'.format(way_to_output_file), 'rb') as model:
  40.         dictionary = pickle.load(model)
  41.     num_of_words = length_of_text
  42.     new_word = first_word
  43.     space = " "
  44.     # next_words_list = collections.OrderedDict()
  45.     with open('{}/generated_text.txt'.format(way_to_output_file),
  46.               'w', encoding="utf-8") as f:
  47.         for i in range(num_of_words):
  48.             next_words_dict = dictionary.get(new_word)
  49.             f.write(str(new_word) + space)
  50.             new_word = None
  51.             while (new_word is None):
  52.                 new_word = frequency_choice(next_words_dict)
  53.             next_words_dict.clear()
  54.  
  55.  
  56. if (args.Help):
  57.     print('''Help :
  58.    It is the utility for generate text on the basis of model.
  59.    You should specify the directory with model file :
  60.    --model <dir_name> .
  61.    Also you can specify a directory in which it
  62.    is required to keep generated text or use the stdout:
  63.    --model <dir_name> . Stdout use by default.
  64.    If you want to choice the seed-word, you should use --seed:
  65.    --seed <your_word>
  66.    else it will be chosen accidentally.
  67.    And you should input the lenght of generated text:
  68.    --lengs <natural_num>.
  69.    SUDDENLY use --help to see this text.''')
  70. if (args.way_to_output_file is None):
  71.     args.way_to_output_file = 'stdout_output.txt'
  72. generate_text(args.way_to_model, args.first_word,
  73.               args.length_of_text, args.way_to_output_file)
  74. if (args.way_to_output_file is None):
  75.     with open('{}/generated_text.txt'.format(args.way_to_output_file),
  76.               'r', encoding="utf-8") as f:
  77.         for line in f:
  78.             print(line)
Advertisement
Add Comment
Please, Sign In to add comment