Changed the name of the style to abstract things, because the most important bit here is the fact that they are abstract. Also cleaned up the code a bit

This commit is contained in:
Crista Lopes
2013-12-06 08:29:14 -08:00
parent 06d99ebbe3
commit dd0700368e
3 changed files with 37 additions and 54 deletions

View File

@@ -0,0 +1,21 @@
Style #11
==============================
Constraints:
- The larger problem is decomposed into 'abstract things' that make
sense for the problem domain
- Each abstract thing is described by what operations the things of
that abstraction can eventually do
- Concrete things are then bound, somehow, to the abstractions; mechanisms
for doing that vary
- The rest of the application uses the things not by what they are but
by they do in abstract
Possible names:
- Abstract things
- Abstract data types

View File

@@ -1,9 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
import abc, sys, re, operator, string import abc, sys, re, operator, string
# #
# The abstract data types # The abstract things
# #
class IDataStorage (object): class IDataStorage (object):
""" Models the contents of the file """ """ Models the contents of the file """
@@ -12,7 +11,7 @@ class IDataStorage (object):
@abc.abstractmethod @abc.abstractmethod
def words(self): def words(self):
""" Returns the words in storage """ """ Returns the words in storage """
raise NotImplementedError("Abstract Data Type") pass
class IStopWordFilter (object): class IStopWordFilter (object):
""" Models the stop word filter """ """ Models the stop word filter """
@@ -21,7 +20,7 @@ class IStopWordFilter (object):
@abc.abstractmethod @abc.abstractmethod
def is_stop_word(self, word): def is_stop_word(self, word):
""" Checks whether the given word is a stop word """ """ Checks whether the given word is a stop word """
raise NotImplementedError("Abstract Data Type") pass
class IWordFrequencyCounter(object): class IWordFrequencyCounter(object):
""" Keeps the word frequency data """ """ Keeps the word frequency data """
@@ -30,58 +29,39 @@ class IWordFrequencyCounter(object):
@abc.abstractmethod @abc.abstractmethod
def increment_count(self, word): def increment_count(self, word):
""" Increments the count for the given word """ """ Increments the count for the given word """
raise NotImplementedError("Abstract Data Type") pass
@abc.abstractmethod @abc.abstractmethod
def sorted(self): def sorted(self):
""" Returns the words and their frequencies, sorted by frequency""" """ Returns the words and their frequencies, sorted by frequency"""
raise NotImplementedError("Abstract Data Type") pass
# #
# The concrete type implementations # The concrete things
# #
class DataStorage: class DataStorage:
""" Implements the contents of the file """
_data = '' _data = ''
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()
self.__filter_chars_normalize()
self.__scan()
def __filter_chars_normalize(self):
"""
Takes a string and returns a copy with all nonalphanumeric chars
replaced by white space
"""
pattern = re.compile('[\W_]+') pattern = re.compile('[\W_]+')
self._data = pattern.sub(' ', self._data).lower() self._data = pattern.sub(' ', self._data).lower()
def __scan(self):
self._data = ''.join(self._data).split() self._data = ''.join(self._data).split()
def words(self): def words(self):
"""
Returns the list words in storage
"""
return self._data return self._data
class StopWordFilter: class StopWordFilter:
""" Implements the stop word filter """
_stop_words = [] _stop_words = []
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()
# add single-letter words
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):
return word in self._stop_words return word in self._stop_words
class WordFrequencyCounter: class WordFrequencyCounter:
""" Implements the word frequency data """
_word_freqs = {} _word_freqs = {}
def increment_count(self, word): def increment_count(self, word):
@@ -95,12 +75,15 @@ class WordFrequencyCounter:
# #
# The wiring between ADTs and concrete implementations # The wiring between abstract things and concrete things
# #
IDataStorage.register(DataStorage) IDataStorage.register(DataStorage)
IStopWordFilter.register(StopWordFilter) IStopWordFilter.register(StopWordFilter)
IWordFrequencyCounter.register(WordFrequencyCounter) IWordFrequencyCounter.register(WordFrequencyCounter)
#
# The application object
#
class WordFrequencyApplication: class WordFrequencyApplication:
def __init__(self, path_to_file): def __init__(self, path_to_file):
self._storage = DataStorage(path_to_file) self._storage = DataStorage(path_to_file)
@@ -113,8 +96,8 @@ class WordFrequencyApplication:
self._word_freq_counter.increment_count(w) self._word_freq_counter.increment_count(w)
word_freqs = self._word_freq_counter.sorted() word_freqs = self._word_freq_counter.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

View File

@@ -1,21 +0,0 @@
Style #11
==============================
Constraints:
- The larger problem is decomposed into types of 'things' that make
sense for the problem domain
- Each type of thing is described by what operations the things of
that type can eventually do
- Concrete things are then bound, somehow, to the types; mechanisms
for doing that vary
- The rest of the application uses the things not by what they are but
by their types
Possible names:
- Types of things
- Abstract data types