Deleted unneeded comments
This commit is contained in:
@@ -1,16 +1,10 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys, re, operator, string, time
|
import sys, re, operator, string, time
|
||||||
|
|
||||||
#
|
#
|
||||||
# The functions
|
# The functions
|
||||||
#
|
#
|
||||||
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
|
|
||||||
"""
|
|
||||||
with open(path_to_file) as f:
|
with open(path_to_file) as f:
|
||||||
str_data = f.read()
|
str_data = f.read()
|
||||||
pattern = re.compile('[\W_]+')
|
pattern = re.compile('[\W_]+')
|
||||||
@@ -21,10 +15,6 @@ def extract_words(path_to_file):
|
|||||||
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]
|
||||||
|
|
||||||
def frequencies(word_list):
|
def frequencies(word_list):
|
||||||
"""
|
|
||||||
Takes a list of words and returns a dictionary associating
|
|
||||||
words with frequencies of occurrence
|
|
||||||
"""
|
|
||||||
word_freqs = {}
|
word_freqs = {}
|
||||||
for w in word_list:
|
for w in word_list:
|
||||||
if w in word_freqs:
|
if w in word_freqs:
|
||||||
@@ -34,11 +24,6 @@ def frequencies(word_list):
|
|||||||
return word_freqs
|
return word_freqs
|
||||||
|
|
||||||
def sort(word_freq):
|
def sort(word_freq):
|
||||||
"""
|
|
||||||
Takes a dictionary of words and their frequencies
|
|
||||||
and returns a list of pairs where the entries are
|
|
||||||
sorted by frequency
|
|
||||||
"""
|
|
||||||
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
|
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||||
|
|
||||||
# The side functionality
|
# The side functionality
|
||||||
@@ -57,11 +42,8 @@ tracked_functions = [extract_words, frequencies, sort]
|
|||||||
for func in tracked_functions:
|
for func in tracked_functions:
|
||||||
globals()[func.func_name]=profile(func)
|
globals()[func.func_name]=profile(func)
|
||||||
|
|
||||||
#
|
|
||||||
# The main function
|
|
||||||
#
|
|
||||||
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
||||||
|
|
||||||
for tf in word_freqs[0:25]:
|
for (w, c) in word_freqs[0:25]:
|
||||||
print tf[0], ' - ', tf[1]
|
print w, ' - ', c
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user