Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. I Love you
  2. I Miss you
  3. Miss you Baby
  4. You are the best
  5. I Miss you
  6.  
  7. from collections import Counter
  8. import pprint
  9. class TextAnalyzer:
  10.  
  11. text_file = 'example.txt'
  12.  
  13. raw_data = None
  14.  
  15. word_map = None
  16.  
  17. def __init__(self):
  18. self.prepare_data()
  19. self.analyze()
  20. pprint.pprint(self.word_map)
  21.  
  22. def prepare_data(self):
  23. with open(self.text_file, 'r') as example:
  24. self.raw_data=example.read().replace('n', ' ')
  25. example.close()
  26.  
  27. def analyze(self):
  28. words = self.raw_data.split()
  29.  
  30. word_pairs = [[words[i],words[i+1]] for i in range(len(words)-1)]
  31.  
  32. self.word_map = dict()
  33.  
  34. for word in list(set(words)):
  35. for pair in word_pairs:
  36. if word == pair[0]:
  37. self.word_map.setdefault(word, []).append(pair[1])
  38.  
  39. self.word_map[word] = Counter(self.word_map[word]).most_common(11)
  40.  
  41. TextAnalyzer()
  42.  
  43. {'Baby': ['You'],
  44. 'I': ['Love', 'Miss', 'Miss'],
  45. 'Love': ['you'],
  46. 'Miss': ['you', 'you', 'you'],
  47. 'You': ['are'],
  48. 'are': ['the'],
  49. 'best': ['I'],
  50. 'the': ['best'],
  51. 'you': [('I', 1), ('Miss', 1), ('Baby', 1)]}
  52.  
  53. {'Miss': [('you',3)],
  54. 'I': [('Love',1), ('Miss',2)],
  55. 'Love': ['you',1],
  56. 'Baby': ['You',1],
  57. 'You': ['are',1],
  58. 'are': ['the',1],
  59. 'best': ['I',1],
  60. 'the': ['best'],
  61. 'you': [('I', 1), ('Miss', 1), ('Baby', 1)]}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement