Make the mvc example interactive
This commit is contained in:
@@ -1,23 +1,21 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, re, operator, collections
|
import sys, re, operator, collections
|
||||||
|
|
||||||
#
|
|
||||||
# Model
|
|
||||||
#
|
|
||||||
class WordFrequenciesModel:
|
class WordFrequenciesModel:
|
||||||
""" Models the data. In this case, we're only interested
|
""" Models the data. In this case, we're only interested
|
||||||
in words and their frequencies as an end result """
|
in words and their frequencies as an end result """
|
||||||
freqs = {}
|
freqs = {}
|
||||||
def __init__(self, path_to_file):
|
def __init__(self, path_to_file):
|
||||||
stopwords = set(open('../stop_words.txt').read().split(','))
|
self.update(path_to_file)
|
||||||
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
|
|
||||||
self.freqs = collections.Counter(w for w in words if w not in stopwords)
|
|
||||||
|
|
||||||
|
def update(self, path_to_file):
|
||||||
#
|
try:
|
||||||
# View
|
stopwords = set(open('../stop_words.txt').read().split(','))
|
||||||
#
|
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
|
||||||
|
self.freqs = collections.Counter(w for w in words if w not in stopwords)
|
||||||
|
except IOError:
|
||||||
|
print "File not found"
|
||||||
|
self.freqs = {}
|
||||||
class WordFrequenciesView:
|
class WordFrequenciesView:
|
||||||
def __init__(self, model):
|
def __init__(self, model):
|
||||||
self._model = model
|
self._model = model
|
||||||
@@ -27,18 +25,21 @@ class WordFrequenciesView:
|
|||||||
for (w, c) in sorted_freqs[:25]:
|
for (w, c) in sorted_freqs[:25]:
|
||||||
print w, '-', c
|
print w, '-', c
|
||||||
|
|
||||||
#
|
|
||||||
# Controller
|
|
||||||
#
|
|
||||||
class WordFrequencyController:
|
class WordFrequencyController:
|
||||||
def __init__(self, model, view):
|
def __init__(self, model, view):
|
||||||
self._model = model
|
self._model, self._view = model, view
|
||||||
self._view = view
|
|
||||||
view.render()
|
view.render()
|
||||||
|
|
||||||
#
|
def run(self):
|
||||||
# Main
|
while True:
|
||||||
#
|
print "Next file: "
|
||||||
|
sys.stdout.flush()
|
||||||
|
filename = sys.stdin.readline().strip()
|
||||||
|
self._model.update(filename)
|
||||||
|
self._view.render()
|
||||||
|
|
||||||
|
|
||||||
m = WordFrequenciesModel(sys.argv[1])
|
m = WordFrequenciesModel(sys.argv[1])
|
||||||
v = WordFrequenciesView(m)
|
v = WordFrequenciesView(m)
|
||||||
c = WordFrequencyController(m, v)
|
c = WordFrequencyController(m, v)
|
||||||
|
c.run()
|
||||||
|
|||||||
Reference in New Issue
Block a user