SHARE
TWEET

Untitled

a guest Aug 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. f = open('text.txt', 'r', encoding='utf-8')
  4. s = f.read()
  5. s = s.split()
  6. struct = dict()
  7.  
  8. alp = set()
  9. for i in range(ord('a'), ord('z') + 1):
  10.     alp.add(chr(i))
  11.  
  12. for i in range(ord('A'), ord('Z') + 1):
  13.     alp.add(chr(i))
  14.  
  15. alp.add('.')
  16. alp.add('?')
  17. alp.add('!')
  18.  
  19. for i in range(ord('а'), ord('я')):
  20.     alp.add(chr(i))
  21.  
  22. for i in range(ord('А'), ord('Я')):
  23.     alp.add(chr(i))
  24.  
  25. n = len(s)
  26. words = []
  27. for i in range(n):
  28.     f = True
  29.     for j in range(len(s[i])):
  30.         if s[i][j] not in alp:
  31.             words += s[i].split(s[i][j])
  32.             f = False
  33.     if f:
  34.         words += [s[i]]
  35.  
  36. n = len(words)
  37.  
  38. for i in range(n):
  39.     if words[i] != "\n" and words[i] != "":
  40.         struct[words[i]] = dict()
  41.  
  42. for i in range(1, n):
  43.     if words[i] == "" or words[i - 1] == "" or words[i] == "\n" or words[i - 1] == "\n":
  44.         continue
  45.     if words[i] not in struct[words[i - 1]].keys():
  46.         struct[words[i - 1]][words[i]] = 1
  47.     else:
  48.         struct[words[i - 1]][words[i]] += 1
  49.  
  50. count = dict()
  51.  
  52. for i in struct.keys():
  53.     count[i] = sum(struct[i].values())
  54.     print(i, ', ', count[i], ': ', struct[i], sep='')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top