SpaceCreator

train_2

Mar 14th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import re
  2. import collections
  3. import pickle
  4. import argparse
  5. import os
  6.  
  7.  
  8. def clear_the_string(source_str):
  9.     spaces_to_zero = re.compile('\s')
  10.     zero_to_spaces = re.compile('[0]')
  11.     ignored_pro = re.compile('\W')
  12.     without_nums = re.compile('\d')
  13.     source_str = spaces_to_zero.sub("0", source_str)
  14.     source_str = ignored_pro.sub("", source_str)
  15.     source_str = zero_to_spaces.sub(" ", source_str)
  16.     source_str = without_nums.sub("", source_str)
  17.     return source_str
  18.  
  19.  
  20. def stdin_to_file():
  21.     filename = 'stdin_input.txt'
  22.     open(filename, 'a').close()
  23.     with open(filename, 'w') as f:
  24.         while True:
  25.             new_line = input()
  26.             if (new_line == ""):
  27.                 break
  28.             f.write(new_line + '\n')
  29.  
  30.  
  31. parser = argparse.ArgumentParser()
  32. parser.add_argument('way_to_training_files', '--input-dir', type=str,
  33.                     help='The way to directory with training files')
  34. parser.add_argument('way_to_model_file', "--model", type=str,
  35.                     help='The way to model file')
  36. parser.add_argument('lowercase', '--lc', action='store_true',
  37.                     help='To lead to lowercase')
  38. parser.add_argument('Help', '--help', action='store_true',
  39.                     help='Show the utility reference')
  40. args = parser.parse_args()
  41.  
  42.  
  43. def make_model(way_to_train_files, way_to_model_file, lowercase):
  44.     ignored = re.compile('\w+')
  45.  
  46.     filenames_list = os.listdir(args.way_to_train_files)
  47.     dictionary = collections.OrderedDict()
  48.  
  49.     for filename in filenames_list:
  50.         with open(filename, 'r', encoding="utf-8") as f:
  51.             new_string = f.readline()
  52.             if (lowercase):
  53.                 new_string = new_string.lower()
  54.             new_string = clear_the_string(new_string)
  55.             new_list = ignored.findall(new_string)
  56.             while True:
  57.                 next_string = f.readline()
  58.                 if (next_string == ''):
  59.                     break
  60.                 if (lowercase):
  61.                     next_string = new_string.lower()
  62.                 next_string = clear_the_string(next_string)
  63.                 next_list = ignored.findall(next_string)
  64.                 while (len(next_list) == 0):
  65.                     next_string = f.readline()
  66.                     next_string = clear_the_string(next_string)
  67.                     next_list = ignored.findall(next_string)
  68.                 new_list.append(next_list[0])
  69.                 counter = 1
  70.                 for k in range(len(new_list) - 1):
  71.                     if new_list[k] not in dictionary:
  72.                         dictionary.setdefault(new_list[k],
  73.                                               collections.OrderedDict())
  74.                     if new_list[counter] in dictionary[new_list[k]]:
  75.                         dictionary[new_list[k]][new_list[counter]] += 1
  76.                     else:
  77.                         dictionary[new_list[k]].setdefault(new_list[counter], 1)
  78.                     counter += 1
  79.                 new_list = next_list
  80.     open('{}/model.pkl'.format(way_to_model_file), 'a').close()
  81.     with open('{}/model.pkl'.format(way_to_model_file), 'wb') as pickle_file:
  82.         pickle.dump(dictionary, pickle_file, 2)
  83.  
  84.  
  85. if (args.Help):
  86.     print('''Help :
  87.   It is the utility for creation of model of the text.
  88.   You can specify the directory with source files or use stdin:
  89.   --input-dir <dir_name> . stdin use by default.
  90.   Also you should specify a directory in which it
  91.   is required to keep model:
  92.   --model <dir_name> .
  93.   If you want to get model in lowercase, you should use --lc.
  94.   SUDDENLY use --help to see this text.''')
  95. if (args.way_to_training_files is None):
  96.     stdin_to_file()
  97.     args.way_to_training_files = 'stdin_input.txt'
  98. make_model(args.way_to_training_files, args.way_to_model_file, args.lowercase)
Advertisement
Add Comment
Please, Sign In to add comment