Massive renaming!
This commit is contained in:
20
11-things/README.md
Normal file
20
11-things/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
Style #10
|
||||
==============================
|
||||
|
||||
Constraints:
|
||||
|
||||
- The larger problem is decomposed into 'things' that make sense for
|
||||
the problem domain
|
||||
|
||||
- Each 'thing' is a capsule of data that exposes procedures to the
|
||||
rest of the world
|
||||
|
||||
- Data is never accessed directly, only through these procedures
|
||||
|
||||
- Capsules can reappropriate procedures defined in other capsules
|
||||
|
||||
Possible names:
|
||||
|
||||
- Things
|
||||
- Object-oriented style
|
||||
- The Kingdom of Nouns (http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html)
|
||||
81
11-things/tf-11.py
Executable file
81
11-things/tf-11.py
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python
|
||||
import sys, re, operator, string
|
||||
from abc import ABCMeta
|
||||
|
||||
#
|
||||
# The classes
|
||||
#
|
||||
class TFExercise():
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
def info(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
class DataStorageManager(TFExercise):
|
||||
""" Models the contents of the file """
|
||||
|
||||
def __init__(self, path_to_file):
|
||||
with open(path_to_file) as f:
|
||||
self._data = f.read()
|
||||
pattern = re.compile('[\W_]+')
|
||||
self._data = pattern.sub(' ', self._data).lower()
|
||||
|
||||
def words(self):
|
||||
""" Returns the list words in storage """
|
||||
return self._data.split()
|
||||
|
||||
def info(self):
|
||||
return super(DataStorageManager, self).info() + ": My major data structure is a " + self._data.__class__.__name__
|
||||
|
||||
class StopWordManager(TFExercise):
|
||||
""" Models the stop word filter """
|
||||
|
||||
def __init__(self):
|
||||
with open('../stop_words.txt') as f:
|
||||
self._stop_words = f.read().split(',')
|
||||
# add single-letter words
|
||||
self._stop_words.extend(list(string.ascii_lowercase))
|
||||
|
||||
def is_stop_word(self, word):
|
||||
return word in self._stop_words
|
||||
|
||||
def info(self):
|
||||
return super(StopWordManager, self).info() + ": My major data structure is a " + self._stop_words.__class__.__name__
|
||||
|
||||
class WordFrequencyManager(TFExercise):
|
||||
""" Keeps the word frequency data """
|
||||
|
||||
def __init__(self):
|
||||
self._word_freqs = {}
|
||||
|
||||
def increment_count(self, word):
|
||||
if word in self._word_freqs:
|
||||
self._word_freqs[word] += 1
|
||||
else:
|
||||
self._word_freqs[word] = 1
|
||||
|
||||
def sorted(self):
|
||||
return sorted(self._word_freqs.items(), key=operator.itemgetter(1), reverse=True)
|
||||
|
||||
def info(self):
|
||||
return super(WordFrequencyManager, self).info() + ": My major data structure is a " + self._word_freqs.__class__.__name__
|
||||
|
||||
class WordFrequencyController(TFExercise):
|
||||
def __init__(self, path_to_file):
|
||||
self._storage_manager = DataStorageManager(path_to_file)
|
||||
self._stop_word_manager = StopWordManager()
|
||||
self._word_freq_manager = WordFrequencyManager()
|
||||
|
||||
def run(self):
|
||||
for w in self._storage_manager.words():
|
||||
if not self._stop_word_manager.is_stop_word(w):
|
||||
self._word_freq_manager.increment_count(w)
|
||||
|
||||
word_freqs = self._word_freq_manager.sorted()
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print(w, ' - ', c)
|
||||
|
||||
#
|
||||
# The main function
|
||||
#
|
||||
WordFrequencyController(sys.argv[1]).run()
|
||||
180
11-things/tf_10.java
Normal file
180
11-things/tf_10.java
Normal file
@@ -0,0 +1,180 @@
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class tf_10 {
|
||||
/*
|
||||
* The main function
|
||||
*/
|
||||
public static void main(String[] args) throws IOException {
|
||||
new WordFrequencyController(args[0]).run();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The classes
|
||||
*/
|
||||
|
||||
abstract class TFExercise {
|
||||
public String getInfo() {
|
||||
return this.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
class WordFrequencyController extends TFExercise {
|
||||
private DataStorageManager storageManager;
|
||||
private StopWordManager stopWordManager;
|
||||
private WordFrequencyManager wordFreqManager;
|
||||
|
||||
public WordFrequencyController(String pathToFile) throws IOException {
|
||||
this.storageManager = new DataStorageManager(pathToFile);
|
||||
this.stopWordManager = new StopWordManager();
|
||||
this.wordFreqManager = new WordFrequencyManager();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (String word : this.storageManager.getWords()) {
|
||||
if (!this.stopWordManager.isStopWord(word)) {
|
||||
this.wordFreqManager.incrementCount(word);
|
||||
}
|
||||
}
|
||||
|
||||
int numWordsPrinted = 0;
|
||||
for (WordFrequencyPair pair : this.wordFreqManager.sorted()) {
|
||||
System.out.println(pair.getWord() + " - " + pair.getFrequency());
|
||||
|
||||
numWordsPrinted++;
|
||||
if (numWordsPrinted >= 25) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Models the contents of the file. */
|
||||
class DataStorageManager extends TFExercise {
|
||||
private List<String> words;
|
||||
|
||||
public DataStorageManager(String pathToFile) throws IOException {
|
||||
this.words = new ArrayList<String>();
|
||||
|
||||
Scanner f = new Scanner(new File(pathToFile), "UTF-8");
|
||||
try {
|
||||
f.useDelimiter("[\\W_]+");
|
||||
while (f.hasNext()) {
|
||||
this.words.add(f.next().toLowerCase());
|
||||
}
|
||||
} finally {
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getWords() {
|
||||
return this.words;
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return super.getInfo() + ": My major data structure is a " + this.words.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
/** Models the stop word filter. */
|
||||
class StopWordManager extends TFExercise {
|
||||
private Set<String> stopWords;
|
||||
|
||||
public StopWordManager() throws IOException {
|
||||
this.stopWords = new HashSet<String>();
|
||||
|
||||
Scanner f = new Scanner(new File("../stop_words.txt"), "UTF-8");
|
||||
try {
|
||||
f.useDelimiter(",");
|
||||
while (f.hasNext()) {
|
||||
this.stopWords.add(f.next());
|
||||
}
|
||||
} finally {
|
||||
f.close();
|
||||
}
|
||||
|
||||
// Add single-letter words
|
||||
for (char c = 'a'; c <= 'z'; c++) {
|
||||
this.stopWords.add("" + c);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStopWord(String word) {
|
||||
return this.stopWords.contains(word);
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return super.getInfo() + ": My major data structure is a " + this.stopWords.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
/** Keeps the word frequency data. */
|
||||
class WordFrequencyManager extends TFExercise {
|
||||
private Map<String, MutableInteger> wordFreqs;
|
||||
|
||||
public WordFrequencyManager() {
|
||||
this.wordFreqs = new HashMap<String, MutableInteger>();
|
||||
}
|
||||
|
||||
public void incrementCount(String word) {
|
||||
MutableInteger count = this.wordFreqs.get(word);
|
||||
if (count == null) {
|
||||
this.wordFreqs.put(word, new MutableInteger(1));
|
||||
} else {
|
||||
count.setValue(count.getValue() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public List<WordFrequencyPair> sorted() {
|
||||
List<WordFrequencyPair> pairs = new ArrayList<WordFrequencyPair>();
|
||||
for (Map.Entry<String, MutableInteger> entry : wordFreqs.entrySet()) {
|
||||
pairs.add(new WordFrequencyPair(entry.getKey(), entry.getValue().getValue()));
|
||||
}
|
||||
Collections.sort(pairs);
|
||||
Collections.reverse(pairs);
|
||||
return pairs;
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return super.getInfo() + ": My major data structure is a " + this.wordFreqs.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
class MutableInteger {
|
||||
private int value;
|
||||
|
||||
public MutableInteger(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
class WordFrequencyPair implements Comparable<WordFrequencyPair> {
|
||||
private String word;
|
||||
private int frequency;
|
||||
|
||||
public WordFrequencyPair(String word, int frequency) {
|
||||
this.word = word;
|
||||
this.frequency = frequency;
|
||||
}
|
||||
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
public int getFrequency() {
|
||||
return frequency;
|
||||
}
|
||||
|
||||
public int compareTo(WordFrequencyPair other) {
|
||||
return this.frequency - other.frequency;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user