Use introspection more meaningfully
This commit is contained in:
@@ -1,22 +1,20 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys, re, operator, string, inspect
|
import sys, re, operator, string, inspect
|
||||||
|
|
||||||
#
|
|
||||||
# The functions
|
|
||||||
#
|
|
||||||
def read_stop_words():
|
def read_stop_words():
|
||||||
# Meta-level data: inspect.stack() and locals()
|
""" This function can only be called from a function
|
||||||
print "My name is " + inspect.stack()[0][3] + ", my arguments are " + str(locals().keys())
|
named extract_words."""
|
||||||
print " and I'm being called from ", inspect.stack()[1][3]
|
# Meta-level data: inspect.stack()
|
||||||
|
if inspect.stack()[1][3] != 'extract_words':
|
||||||
|
return None
|
||||||
|
|
||||||
with open('../stop_words.txt') as f:
|
with open('../stop_words.txt') as f:
|
||||||
stop_words = f.read().split(',')
|
stop_words = f.read().split(',')
|
||||||
stop_words.extend(list(string.ascii_lowercase))
|
stop_words.extend(list(string.ascii_lowercase))
|
||||||
return stop_words
|
return stop_words
|
||||||
|
|
||||||
def extract_words(path_to_file):
|
def extract_words(path_to_file):
|
||||||
# Meta-level data: inspect.stack() and locals()
|
# Meta-level data: locals()
|
||||||
print "My name is " + inspect.stack()[0][3] + ", my arguments are " + str(locals().keys())
|
|
||||||
print " and I'm being called from ", inspect.stack()[1][3]
|
|
||||||
with open(locals()['path_to_file']) as f:
|
with open(locals()['path_to_file']) as f:
|
||||||
str_data = f.read()
|
str_data = f.read()
|
||||||
pattern = re.compile('[\W_]+')
|
pattern = re.compile('[\W_]+')
|
||||||
@@ -25,9 +23,7 @@ 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):
|
||||||
# Meta-level data: inspect.stack() and locals()
|
# Meta-level data: locals()
|
||||||
print "My name is " + inspect.stack()[0][3] + ", my arguments are " + str(locals().keys())
|
|
||||||
print " and I'm being called from ", inspect.stack()[1][3]
|
|
||||||
word_freqs = {}
|
word_freqs = {}
|
||||||
for w in locals()['word_list']:
|
for w in locals()['word_list']:
|
||||||
if w in word_freqs:
|
if w in word_freqs:
|
||||||
@@ -37,14 +33,9 @@ def frequencies(word_list):
|
|||||||
return word_freqs
|
return word_freqs
|
||||||
|
|
||||||
def sort(word_freq):
|
def sort(word_freq):
|
||||||
# Meta-level data: inspect.stack() and locals()
|
# Meta-level data: locals()
|
||||||
print "My name is " + inspect.stack()[0][3] + " and my arguments are " + str(locals().keys())
|
|
||||||
print " and I'm being called from ", inspect.stack()[1][3]
|
|
||||||
return sorted(locals()['word_freq'].iteritems(), key=operator.itemgetter(1), reverse=True)
|
return sorted(locals()['word_freq'].iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||||
|
|
||||||
#
|
|
||||||
# The main function
|
|
||||||
#
|
|
||||||
def main():
|
def main():
|
||||||
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
||||||
for (w, c) in word_freqs[0:25]:
|
for (w, c) in word_freqs[0:25]:
|
||||||
|
|||||||
Reference in New Issue
Block a user