Minor
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, re, operator, string
|
import sys, re, operator, string
|
||||||
|
|
||||||
class DataStorageManager():
|
class DataStorageManager():
|
||||||
@@ -15,20 +14,16 @@ class DataStorageManager():
|
|||||||
raise Exception("Message not understood " + message[0])
|
raise Exception("Message not understood " + message[0])
|
||||||
|
|
||||||
def _init(self, path_to_file):
|
def _init(self, path_to_file):
|
||||||
f = open(path_to_file)
|
with open(path_to_file) as f:
|
||||||
self._data = f.read()
|
self._data = f.read()
|
||||||
f.close()
|
|
||||||
pattern = re.compile('[\W_]+')
|
pattern = re.compile('[\W_]+')
|
||||||
self._data = pattern.sub(' ', self._data).lower()
|
self._data = pattern.sub(' ', self._data).lower()
|
||||||
|
|
||||||
def _words(self):
|
def _words(self):
|
||||||
"""
|
""" Returns the list words in storage"""
|
||||||
Returns the list words in storage
|
|
||||||
"""
|
|
||||||
data_str = ''.join(self._data)
|
data_str = ''.join(self._data)
|
||||||
return data_str.split()
|
return data_str.split()
|
||||||
|
|
||||||
|
|
||||||
class StopWordManager():
|
class StopWordManager():
|
||||||
""" Models the stop word filter """
|
""" Models the stop word filter """
|
||||||
_stop_words = []
|
_stop_words = []
|
||||||
@@ -42,9 +37,8 @@ class StopWordManager():
|
|||||||
raise Exception("Message not understood " + message[0])
|
raise Exception("Message not understood " + message[0])
|
||||||
|
|
||||||
def _init(self):
|
def _init(self):
|
||||||
f = open('../stop_words.txt')
|
with open('../stop_words.txt') as f:
|
||||||
self._stop_words = f.read().split(',')
|
self._stop_words = f.read().split(',')
|
||||||
f.close()
|
|
||||||
self._stop_words.extend(list(string.ascii_lowercase))
|
self._stop_words.extend(list(string.ascii_lowercase))
|
||||||
|
|
||||||
def _is_stop_word(self, word):
|
def _is_stop_word(self, word):
|
||||||
@@ -94,8 +88,8 @@ class WordFrequencyController():
|
|||||||
self._word_freq_manager.dispatch(['increment_count', w])
|
self._word_freq_manager.dispatch(['increment_count', w])
|
||||||
|
|
||||||
word_freqs = self._word_freq_manager.dispatch(['sorted'])
|
word_freqs = self._word_freq_manager.dispatch(['sorted'])
|
||||||
for tf in word_freqs[0:25]:
|
for (w, c) in word_freqs[0:25]:
|
||||||
print tf[0], ' - ', tf[1]
|
print w, ' - ', c
|
||||||
|
|
||||||
#
|
#
|
||||||
# The main function
|
# The main function
|
||||||
|
|||||||
Reference in New Issue
Block a user