Deleted unneeded comments and checks
This commit is contained in:
@@ -26,4 +26,4 @@ Possible names:
|
|||||||
|
|
||||||
- No commitment
|
- No commitment
|
||||||
- Plugins
|
- Plugins
|
||||||
- Why one when we can have many?
|
- Dependency injection
|
||||||
|
|||||||
@@ -1,13 +1,6 @@
|
|||||||
import operator
|
import operator
|
||||||
|
|
||||||
def top25(word_list):
|
def top25(word_list):
|
||||||
"""
|
|
||||||
Takes a list of words and returns a dictionary associating
|
|
||||||
words with frequencies of occurrence
|
|
||||||
"""
|
|
||||||
if type(word_list) is not list or word_list == []:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
word_freqs = {}
|
word_freqs = {}
|
||||||
for w in word_list:
|
for w in word_list:
|
||||||
if w in word_freqs:
|
if w in word_freqs:
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
import operator, collections
|
import operator, collections
|
||||||
|
|
||||||
def top25(word_list):
|
def top25(word_list):
|
||||||
"""
|
|
||||||
Takes a list of words and returns a dictionary associating
|
|
||||||
words with frequencies of occurrence
|
|
||||||
"""
|
|
||||||
counts = collections.Counter(w for w in word_list)
|
counts = collections.Counter(w for w in word_list)
|
||||||
return counts.most_common(25)
|
return counts.most_common(25)
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,13 @@
|
|||||||
import sys, re, string
|
import sys, re, string
|
||||||
|
|
||||||
def extract_words(path_to_file):
|
def extract_words(path_to_file):
|
||||||
"""
|
with open(path_to_file) as f:
|
||||||
Takes a path to a file and returns the non-stop
|
str_data = f.read()
|
||||||
words, after properly removing nonalphanumeric chars
|
|
||||||
and normalizing for lower case
|
|
||||||
"""
|
|
||||||
if type(path_to_file) is not str or not path_to_file:
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(path_to_file) as f:
|
|
||||||
str_data = f.read()
|
|
||||||
except IOError as e:
|
|
||||||
print "I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror)
|
|
||||||
return []
|
|
||||||
|
|
||||||
pattern = re.compile('[\W_]+')
|
pattern = re.compile('[\W_]+')
|
||||||
word_list = pattern.sub(' ', str_data).lower().split()
|
word_list = pattern.sub(' ', str_data).lower().split()
|
||||||
|
|
||||||
try:
|
with open('../stop_words.txt') as f:
|
||||||
with open('../stop_words.txt') as f:
|
stop_words = f.read().split(',')
|
||||||
stop_words = f.read().split(',')
|
|
||||||
except IOError as e:
|
|
||||||
print "I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror)
|
|
||||||
return []
|
|
||||||
|
|
||||||
stop_words.extend(list(string.ascii_lowercase))
|
stop_words.extend(list(string.ascii_lowercase))
|
||||||
return [w for w in word_list if not w in stop_words]
|
return [w for w in word_list if not w in stop_words]
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
import sys, re, string
|
import sys, re, string
|
||||||
|
|
||||||
def extract_words(path_to_file):
|
def extract_words(path_to_file):
|
||||||
"""
|
|
||||||
Takes a path to a file and returns the non-stop
|
|
||||||
words, after properly removing nonalphanumeric chars
|
|
||||||
and normalizing for lower case
|
|
||||||
"""
|
|
||||||
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
|
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
|
||||||
stopwords = set(open('../stop_words.txt').read().split(','))
|
stopwords = set(open('../stop_words.txt').read().split(','))
|
||||||
return [w for w in words if w not in stopwords]
|
return [w for w in words if w not in stopwords]
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, ConfigParser, imp
|
import sys, ConfigParser, imp
|
||||||
|
|
||||||
def load_plugins():
|
def load_plugins():
|
||||||
@@ -11,13 +10,9 @@ def load_plugins():
|
|||||||
tfwords = imp.load_compiled('tfwords', words_plugin)
|
tfwords = imp.load_compiled('tfwords', words_plugin)
|
||||||
tffreqs = imp.load_compiled('tffreqs', frequencies_plugin)
|
tffreqs = imp.load_compiled('tffreqs', frequencies_plugin)
|
||||||
|
|
||||||
#
|
|
||||||
# The main function
|
|
||||||
#
|
|
||||||
|
|
||||||
load_plugins()
|
load_plugins()
|
||||||
word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1]))
|
word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1]))
|
||||||
|
|
||||||
for tf in word_freqs:
|
for (w, c) in word_freqs:
|
||||||
print tf[0], ' - ', tf[1]
|
print w, ' - ', c
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user