Advertisement
Guest User

Text generator

a guest
May 23rd, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. """Case-study #7 Генерация предложений
  2. Разработчики:
  3. Дарбаа Ч.Ю.
  4.  
  5. """
  6. import random
  7. import re
  8. def text_generator(filename, n_senstences = 20):
  9.     input_file = open(filename, 'r')
  10.     text = input_file.read()
  11.     text = text.replace("\n", " ")
  12.     text = re.sub('[^a-zA-Zа-яА-Я" "0-9 !.,?]+', '', text)
  13.     text = re.sub(' +', ' ', text)
  14.     text = text.replace(' ?', '?').replace(' !', '!').replace(' .', '.').replace(' ,', ',')
  15.     words = text.split()
  16.     chain = {}
  17.     for i in range(len(words) - 1):
  18.         if words[i] in chain.keys():
  19.             chain[words[i]].append(words[i + 1])
  20.         else:
  21.             chain[words[i]] = [words[i + 1]]
  22.     capital = 'ЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNM'
  23.     start_words = [word for word in words if word[0] in capital]
  24.     start_words = list(set(start_words))  #optional
  25.     generated_text = ''
  26.     sentence_end = '.!?'
  27.     for i in range(n_senstences):
  28.         chain_word = random.choice(start_words)
  29.         generated_text += chain_word + ' '
  30.         num_words = random.randint(4, 19)
  31.         for j in range(num_words):
  32.             if chain_word in chain.keys():
  33.                 chain_word = random.choice(chain[chain_word])
  34.             else:
  35.                 chain_word = random.choice(words)
  36.             generated_text += chain_word + ' '
  37.         if generated_text[-2] == ',':
  38.             generated_text = generated_text[:-2] + random.choice(sentence_end) + " "
  39.         elif generated_text[-2] not in sentence_end:
  40.             generated_text = generated_text[:-1] + random.choice(sentence_end) + " "
  41.     return generated_text
  42.  
  43. print(text_generator('text.txt', 5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement