Advertisement
jimMoonrock

task1

Jun 18th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. import re
  2. from statistics import median
  3.  
  4.  
  5. def main():
  6.  
  7.     with open('task1.txt') as file:
  8.         text = file.read()
  9.  
  10.     # Medians
  11.     list_with_text = re.split(r'[.?!]', text.replace('\n', ''))
  12.     '''for word in list_with_text:
  13.        if word == '':
  14.            list_with_text.remove(word)'''
  15.     list_with_text = list(filter(lambda  x : x != '', list_with_text))
  16.     print(list_with_text)
  17.  
  18.     median_words = median([len(word.split()) for word in list_with_text])
  19.  
  20.     list_words = ' '.join(list_with_text).split(' ')
  21.     for word in list_words:
  22.         if word == '':
  23.             list_words.remove(word)
  24.  
  25.     new_spl = []
  26.     for word in ' '.join(list_words).split('.'):
  27.         if word.startswith(' '):
  28.             new_spl.append(word[1:])
  29.         else:
  30.             new_spl.append(word)
  31.     new_spl = ''.join(new_spl).split(' ')
  32.  
  33.     count_words = len(new_spl) / len(list_with_text)
  34.  
  35.     dict_words = {word: list_words.count(word) for word in list_words}
  36.  
  37.     num_tuple = ('2', '3', '4')
  38.     for words, count in dict_words.items():
  39.         if str(count)[-1] in num_tuple:
  40.             print('Слово - {} повторяется: {} разa'.format(words, count))
  41.         else:
  42.             print('Слово - {} повторяется: {} раз'.format(words, count))
  43.  
  44.     if list_words:
  45.         print('Среднее количество слов в предложении: {:0.2f}'.format(float(count_words)))
  46.  
  47.     print('Медианное количество слов в предложение: {}'.format(median_words))
  48.  
  49.  
  50. if __name__ == '__main__':
  51.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement