Created
June 19, 2016 22:43
-
-
Save rinchik/eb4f5996dcc7583623b5d4c704df7168 to your computer and use it in GitHub Desktop.
Python class that does text analysis and compiles a hash map of words and word following given word sorted by frequency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import pprint | |
class TextAnalyzer: | |
text_file = 'example.txt' | |
raw_data = None | |
word_map = None | |
def __init__(self): | |
self.prepare_data() | |
self.analyze() | |
pprint.pprint(self.word_map) | |
def prepare_data(self): | |
with open(self.text_file, 'r') as example: | |
self.raw_data=example.read().replace('\n', ' ') | |
example.close() | |
def analyze(self): | |
words = self.raw_data.split() | |
word_pairs = [[words[i],words[i+1]] for i in range(len(words)-1)] | |
self.word_map = dict() | |
for word in list(set(words)): | |
for pair in word_pairs: | |
if word == pair[0]: | |
self.word_map.setdefault(word, []).append(pair[1]) | |
self.word_map[word] = Counter(self.word_map[word]).most_common(11) | |
TextAnalyzer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment