import re
from collections import Counter
from cppy.cp_util import *

class DataStorageManager1:    
    def __init__(self, path_to_file):
        self._data = read_file(path_to_file)        
        self._data = re_split(self._data)

    def words(self): return self._data
    

class DataStorageManager2:    
    def __init__(self, path_to_file):
        self._data = read_file(path_to_file)
        self._data = re.findall('[a-z]{2,}', self._data)        

    def words(self): return self._data


class StopWordManager:    
    def __init__(self):
        self._stop_words = set(get_stopwords())
        
    def is_stop_word(self, word):
        return word in self._stop_words


class WordFrequencyManager:
    def __init__(self):
        self.word_freqs = Counter()

    def increment_count(self, word):
        self.word_freqs[word] += 1

    def sorted(self):
        return self.word_freqs.most_common()    


#
# 应用类
#
class WordFrequencyController:
    def __init__(self, path_to_file):
        # self._storage = DataStorageManager1(path_to_file)
        self.storage = DataStorageManager2(path_to_file)
        self.stop_word_manager = StopWordManager()
        self.word_freq_counter = WordFrequencyManager()

    def run(self): # 可以看做面向协议编程
        for word in self.storage.words():
            if not self.stop_word_manager.is_stop_word(word):
                self.word_freq_counter.increment_count(word)
        
        print_word_freqs( self.word_freq_counter.sorted() ) 


if __name__ == '__main__':
    WordFrequencyController(testfilepath).run()