And this one too
This commit is contained in:
29
19-plugins/README.md
Normal file
29
19-plugins/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
Style #19
|
||||
==============================
|
||||
|
||||
Constraints:
|
||||
|
||||
- The problem is decomposed using some form of abstraction
|
||||
(procedures, functions, objects, etc.)
|
||||
|
||||
- All or some of those abstractions are physically encapsulated into
|
||||
their own, usually pre-compiled, packages. Main program and each of
|
||||
the packages are compiled independently. These packages are loaded
|
||||
dynamically by the main program, usually in the beginning (but not
|
||||
necessarily).
|
||||
|
||||
- Main program uses functions/objects from the dynamically-loaded
|
||||
packages, without knowing which exact implementations will be
|
||||
used. New implementations can be used without having to adapt or
|
||||
recompile the main program.
|
||||
|
||||
- External specification of which packages to load. This can be done
|
||||
by a configuration file, path conventions, user input or other
|
||||
mechanisms for external specification of code to be linked at run
|
||||
time.
|
||||
|
||||
Possible names:
|
||||
|
||||
- No commitment
|
||||
- Plugins
|
||||
- Dependency injection
|
||||
5
19-plugins/config.ini
Normal file
5
19-plugins/config.ini
Normal file
@@ -0,0 +1,5 @@
|
||||
[Plugins]
|
||||
;; Options: plugins/words1.pyc, plugins/words2.pyc
|
||||
words = plugins/words1.pyc
|
||||
;; Options: plugins/frequencies1.pyc, plugins/frequencies2.pyc
|
||||
frequencies = plugins/frequencies1.pyc
|
||||
2
19-plugins/plugins-src/compile.sh
Normal file
2
19-plugins/plugins-src/compile.sh
Normal file
@@ -0,0 +1,2 @@
|
||||
python -m compileall .
|
||||
cp *.pyc ../plugins
|
||||
11
19-plugins/plugins-src/frequencies1.py
Normal file
11
19-plugins/plugins-src/frequencies1.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import operator
|
||||
|
||||
def top25(word_list):
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
if w in word_freqs:
|
||||
word_freqs[w] += 1
|
||||
else:
|
||||
word_freqs[w] = 1
|
||||
return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)[:25]
|
||||
|
||||
6
19-plugins/plugins-src/frequencies2.py
Normal file
6
19-plugins/plugins-src/frequencies2.py
Normal file
@@ -0,0 +1,6 @@
|
||||
import operator, collections
|
||||
|
||||
def top25(word_list):
|
||||
counts = collections.Counter(w for w in word_list)
|
||||
return counts.most_common(25)
|
||||
|
||||
14
19-plugins/plugins-src/words1.py
Normal file
14
19-plugins/plugins-src/words1.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import sys, re, string
|
||||
|
||||
def extract_words(path_to_file):
|
||||
with open(path_to_file) as f:
|
||||
str_data = f.read()
|
||||
pattern = re.compile('[\W_]+')
|
||||
word_list = pattern.sub(' ', str_data).lower().split()
|
||||
|
||||
with open('../stop_words.txt') as f:
|
||||
stop_words = f.read().split(',')
|
||||
stop_words.extend(list(string.ascii_lowercase))
|
||||
|
||||
return [w for w in word_list if not w in stop_words]
|
||||
|
||||
7
19-plugins/plugins-src/words2.py
Normal file
7
19-plugins/plugins-src/words2.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import sys, re, string
|
||||
|
||||
def extract_words(path_to_file):
|
||||
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
|
||||
stopwords = set(open('../stop_words.txt').read().split(','))
|
||||
return [w for w in words if w not in stopwords]
|
||||
|
||||
BIN
19-plugins/plugins/frequencies1.pyc
Normal file
BIN
19-plugins/plugins/frequencies1.pyc
Normal file
Binary file not shown.
BIN
19-plugins/plugins/frequencies2.pyc
Normal file
BIN
19-plugins/plugins/frequencies2.pyc
Normal file
Binary file not shown.
BIN
19-plugins/plugins/words1.pyc
Normal file
BIN
19-plugins/plugins/words1.pyc
Normal file
Binary file not shown.
BIN
19-plugins/plugins/words2.pyc
Normal file
BIN
19-plugins/plugins/words2.pyc
Normal file
Binary file not shown.
18
19-plugins/tf-19.py
Executable file
18
19-plugins/tf-19.py
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
import sys, ConfigParser, imp
|
||||
|
||||
def load_plugins():
|
||||
config = ConfigParser.ConfigParser()
|
||||
config.read("config.ini")
|
||||
words_plugin = config.get("Plugins", "words")
|
||||
frequencies_plugin = config.get("Plugins", "frequencies")
|
||||
global tfwords, tffreqs
|
||||
tfwords = imp.load_compiled('tfwords', words_plugin)
|
||||
tffreqs = imp.load_compiled('tffreqs', frequencies_plugin)
|
||||
|
||||
load_plugins()
|
||||
word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1]))
|
||||
|
||||
for (w, c) in word_freqs:
|
||||
print w, ' - ', c
|
||||
|
||||
Reference in New Issue
Block a user