Merge pull request #37 from cclauss/modernize-python2-code
Modernize Python 2 code to get ready for Python 3
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys, os, string
|
||||
|
||||
# Utility for handling the intermediate 'secondary memory'
|
||||
@@ -121,6 +122,6 @@ while True:
|
||||
|
||||
for tf in data[0:25]: # elimination of symbol tf is exercise
|
||||
if len(tf) == 2:
|
||||
print (tf[0], ' - ', tf[1])
|
||||
print(tf[0], ' - ', tf[1])
|
||||
# We're done
|
||||
word_freqs.close()
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
#!/usr/local/bin/python
|
||||
#
|
||||
# f o r t h . p y
|
||||
# Author: Chris Meyers @
|
||||
# Author: Chris Meyers @
|
||||
# http://openbookproject.net/py4fun/forth/forth.html
|
||||
#
|
||||
import sys, re
|
||||
from __future__ import print_function
|
||||
import re
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
ds = [] # The data stack
|
||||
cStack = [] # The control struct stack
|
||||
@@ -15,14 +21,14 @@ words = [] # The input stream of tokens
|
||||
def main() :
|
||||
while 1 :
|
||||
pcode = compile() # compile/run from user
|
||||
if pcode == None : print; return
|
||||
if pcode == None : print(); return
|
||||
execute(pcode)
|
||||
|
||||
#============================== Lexical Parsing
|
||||
|
||||
|
||||
def getWord (prompt="... ") :
|
||||
global words
|
||||
while not words :
|
||||
while not words :
|
||||
try : lin = raw_input(prompt)+"\n"
|
||||
except : return None
|
||||
if lin[0:1] == "@" : lin = open(lin[1:-1]).read()
|
||||
@@ -56,8 +62,8 @@ def rSwap(cod,p) : a=ds.pop(); b=ds.pop(); ds.append(a); ds.append(b)
|
||||
def rDup (cod,p) : ds.append(ds[-1])
|
||||
def rDrop(cod,p) : ds.pop()
|
||||
def rOver(cod,p) : ds.append(ds[-2])
|
||||
def rDump(cod,p) : print "ds = ", ds
|
||||
def rDot (cod,p) : print ds.pop()
|
||||
def rDump(cod,p) : print("ds = ", ds)
|
||||
def rDot (cod,p) : print(ds.pop())
|
||||
def rJmp (cod,p) : return cod[p]
|
||||
def rJnz (cod,p) : return (cod[p],p+1)[ds.pop()]
|
||||
def rJz (cod,p) : return (p+1,cod[p])[ds.pop()==0]
|
||||
@@ -92,7 +98,7 @@ rDict = {
|
||||
|
||||
'create': rCreate, 'does>': rDoes,
|
||||
}
|
||||
#================================= Compile time
|
||||
#================================= Compile time
|
||||
|
||||
def compile() :
|
||||
pcode = []; prompt = "Forth> "
|
||||
@@ -114,12 +120,12 @@ def compile() :
|
||||
try : pcode.append(int(word))
|
||||
except :
|
||||
try: pcode.append(float(word))
|
||||
except :
|
||||
except :
|
||||
pcode[-1] = rRun # Change rPush to rRun
|
||||
pcode.append(word) # Assume word will be defined
|
||||
if not cStack : return pcode
|
||||
prompt = "... "
|
||||
|
||||
|
||||
def fatal (mesg) : raise mesg
|
||||
|
||||
def cColon (pcode) :
|
||||
@@ -168,5 +174,5 @@ cDict = {
|
||||
':' : cColon, ';' : cSemi, 'if': cIf, 'else': cElse, 'then': cThen,
|
||||
'begin': cBegin, 'until': cUntil,
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__" : main()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -104,7 +105,7 @@ stack.append(0)
|
||||
# the last word there will be one item left
|
||||
while stack[-1] < 25 and len(stack) > 1:
|
||||
heap['i'] = stack.pop()
|
||||
(w, f) = stack.pop(); print w, ' - ', f
|
||||
(w, f) = stack.pop(); print(w, ' - ', f)
|
||||
stack.append(heap['i']); stack.append(1)
|
||||
stack.append(stack.pop() + stack.pop())
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys, string
|
||||
# the global list of [word, frequency] pairs
|
||||
word_freqs = []
|
||||
@@ -46,5 +47,5 @@ for line in open(sys.argv[1]):
|
||||
i += 1
|
||||
|
||||
for tf in word_freqs[0:25]:
|
||||
print tf[0], ' - ', tf[1]
|
||||
print(tf[0], ' - ', tf[1])
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, string
|
||||
|
||||
# The shared mutable data
|
||||
@@ -54,7 +55,7 @@ def remove_stop_words():
|
||||
def frequencies():
|
||||
"""
|
||||
Creates a list of pairs associating
|
||||
words with frequencies
|
||||
words with frequencies
|
||||
"""
|
||||
global words
|
||||
global word_freqs
|
||||
@@ -70,7 +71,7 @@ def sort():
|
||||
Sorts word_freqs by frequency
|
||||
"""
|
||||
global word_freqs
|
||||
word_freqs.sort(lambda x, y: cmp(y[1], x[1]))
|
||||
word_freqs.sort(key=lambda x: x[1], reverse=True)
|
||||
|
||||
|
||||
#
|
||||
@@ -84,5 +85,4 @@ frequencies()
|
||||
sort()
|
||||
|
||||
for tf in word_freqs[0:25]:
|
||||
print tf[0], ' - ', tf[1]
|
||||
|
||||
print(tf[0], ' - ', tf[1])
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -65,7 +66,7 @@ def print_all(word_freqs):
|
||||
Takes a list of pairs where the entries are sorted by frequency and print them recursively.
|
||||
"""
|
||||
if(len(word_freqs) > 0):
|
||||
print word_freqs[0][0], ' - ', word_freqs[0][1]
|
||||
print(word_freqs[0][0], ' - ', word_freqs[0][1])
|
||||
print_all(word_freqs[1:]);
|
||||
|
||||
#
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import re, string, sys
|
||||
|
||||
stops = set(open("../stop_words.txt").read().split(",") + list(string.ascii_lowercase))
|
||||
words = [x.lower() for x in re.split("[^a-zA-Z]+", open(sys.argv[1]).read()) if len(x) > 0 and x.lower() not in stops]
|
||||
unique_words = list(set(words))
|
||||
unique_words.sort(lambda x, y: cmp(words.count(y), words.count(x)))
|
||||
print "\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]])
|
||||
unique_words.sort(key=lambda x: words.count(x), reverse=True)
|
||||
print("\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]]))
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
from __future__ import print_function
|
||||
from functools import reduce
|
||||
print (reduce(lambda string, tup: string + tup[0] + ' - ' + str(tup[1]) + '\n', sorted( filter(lambda tup: tup[0] not in open(__import__('os').path.join(__import__('os').path.dirname(__file__), '..', 'stop_words.txt')).read().lower().split(','), reduce(lambda word_dict, word: word_dict if (word_dict.__setitem__(word, word_dict.get(word, 0) + 1) if True else None) else word_dict, filter(lambda word: len(word) > 1, (''.join(map(lambda letter: ' ' if ord(letter) not in set(range(ord('a'), ord('z') + 1)) else letter, open(__import__('sys').argv[1]).read().lower()))).split()), {}).iteritems()), key=lambda tup: tup[1], reverse=True)[0:25], '')) # hole in one?
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
# My golf score is slightly lower!
|
||||
# Best wishes, Peter Norvig
|
||||
|
||||
from __future__ import print_function
|
||||
import re, sys, collections
|
||||
|
||||
stopwords = set(open('../stop_words.txt').read().split(','))
|
||||
words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower())
|
||||
counts = collections.Counter(w for w in words if w not in stopwords)
|
||||
for (w, c) in counts.most_common(25):
|
||||
print w, '-', c
|
||||
print(w, '-', c)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import heapq, re, sys
|
||||
|
||||
words = re.findall("[a-z]{2,}", open(sys.argv[1]).read().lower())
|
||||
for w in heapq.nlargest(25, set(words) - set(open("../stop_words.txt").read().split(",")), words.count):
|
||||
print w, "-", words.count(w)
|
||||
print(w, "-", words.count(w))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import re, sys, operator
|
||||
|
||||
# Mileage may vary. If this crashes, make it lower
|
||||
@@ -29,7 +30,7 @@ def wf_print(wordfreq):
|
||||
return
|
||||
else:
|
||||
(w, c) = wordfreq[0]
|
||||
print w, '-', c
|
||||
print(w, '-', c)
|
||||
wf_print(wordfreq[1:])
|
||||
|
||||
stop_words = set(open('../stop_words.txt').read().split(','))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -40,7 +41,7 @@ def sort(wf, func):
|
||||
|
||||
def print_text(word_freqs, func):
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, "-", c
|
||||
print(w, "-", c)
|
||||
func(None)
|
||||
|
||||
def no_op(func):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -13,7 +14,7 @@ class TFTheOne:
|
||||
return self
|
||||
|
||||
def printme(self):
|
||||
print self._value
|
||||
print(self._value)
|
||||
|
||||
#
|
||||
# The functions
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
from abc import ABCMeta
|
||||
|
||||
@@ -73,7 +74,7 @@ class WordFrequencyController(TFExercise):
|
||||
|
||||
word_freqs = self._word_freq_manager.sorted()
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
#
|
||||
# The main function
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
class DataStorageManager():
|
||||
@@ -89,7 +90,7 @@ class WordFrequencyController():
|
||||
|
||||
word_freqs = self._word_freq_manager.dispatch(['sorted'])
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
#
|
||||
# The main function
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
# Auxiliary functions that can't be lambdas
|
||||
@@ -46,4 +47,4 @@ for w in data_storage_obj['words']():
|
||||
|
||||
word_freqs = word_freqs_obj['sorted']()
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import abc, sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -97,7 +98,7 @@ class WordFrequencyController:
|
||||
|
||||
word_freqs = self._word_freq_counter.sorted()
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
#
|
||||
# The main function
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -89,7 +90,7 @@ class WordFrequencyCounter:
|
||||
def __print_freqs(self):
|
||||
word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
#
|
||||
# The main function
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -79,7 +80,7 @@ class WordFrequencyCounter:
|
||||
def print_freqs(self, event):
|
||||
word_freqs = sorted(self._word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
class WordFrequencyApplication:
|
||||
def __init__(self, event_manager):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, inspect
|
||||
|
||||
def read_stop_words():
|
||||
@@ -39,7 +40,7 @@ def sort(word_freq):
|
||||
def main():
|
||||
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, os
|
||||
|
||||
#
|
||||
@@ -44,5 +45,5 @@ exec('sort = ' + sort_func)
|
||||
word_freqs = locals()['sort'](locals()['frequencies'](locals()['extract_words'](filename)))
|
||||
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, time
|
||||
|
||||
#
|
||||
@@ -32,7 +33,7 @@ def profile(f):
|
||||
start_time = time.time()
|
||||
ret_value = f(*arg, **kw)
|
||||
elapsed = time.time() - start_time
|
||||
print "%s(...) took %s secs" % (f.__name__, elapsed)
|
||||
print("%s(...) took %s secs" % (f.__name__, elapsed))
|
||||
return ret_value
|
||||
return profilewrapper
|
||||
|
||||
@@ -40,10 +41,10 @@ def profile(f):
|
||||
tracked_functions = [extract_words, frequencies, sort]
|
||||
# weaver
|
||||
for func in tracked_functions:
|
||||
globals()[func.func_name]=profile(func)
|
||||
globals()[func.__name__]=profile(func)
|
||||
|
||||
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
||||
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, ConfigParser, imp
|
||||
|
||||
def load_plugins():
|
||||
@@ -14,5 +15,5 @@ load_plugins()
|
||||
word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1]))
|
||||
|
||||
for (w, c) in word_freqs:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, inspect
|
||||
|
||||
#
|
||||
@@ -12,7 +13,7 @@ def extract_words(path_to_file):
|
||||
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)
|
||||
print("I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror))
|
||||
return []
|
||||
|
||||
pattern = re.compile('[\W_]+')
|
||||
@@ -27,7 +28,7 @@ def remove_stop_words(word_list):
|
||||
with open('../stop_words.txt') as f:
|
||||
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)
|
||||
print("I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror))
|
||||
return word_list
|
||||
|
||||
stop_words.extend(list(string.ascii_lowercase))
|
||||
@@ -58,5 +59,5 @@ filename = sys.argv[1] if len(sys.argv) > 1 else "../input.txt"
|
||||
word_freqs = sort(frequencies(remove_stop_words(extract_words(filename))))
|
||||
|
||||
for tf in word_freqs[0:25]:
|
||||
print tf[0], ' - ', tf[1]
|
||||
print(tf[0], ' - ', tf[1])
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, traceback
|
||||
|
||||
#
|
||||
@@ -13,7 +14,7 @@ def extract_words(path_to_file):
|
||||
with open(path_to_file) as f:
|
||||
str_data = f.read()
|
||||
except IOError as e:
|
||||
print "I/O error({0}) when opening {1}: {2}! I quit!".format(e.errno, path_to_file, e.strerror)
|
||||
print("I/O error({0}) when opening {1}: {2}! I quit!".format(e.errno, path_to_file, e.strerror))
|
||||
raise e
|
||||
|
||||
pattern = re.compile('[\W_]+')
|
||||
@@ -27,7 +28,7 @@ def remove_stop_words(word_list):
|
||||
with open('../stop_words.txt') as f:
|
||||
stop_words = f.read().split(',')
|
||||
except IOError as e:
|
||||
print "I/O error({0}) when opening ../stops_words.txt: {1}! I quit!".format(e.errno, e.strerror)
|
||||
print("I/O error({0}) when opening ../stops_words.txt: {1}! I quit!".format(e.errno, e.strerror))
|
||||
raise e
|
||||
|
||||
stop_words.extend(list(string.ascii_lowercase))
|
||||
@@ -35,7 +36,7 @@ def remove_stop_words(word_list):
|
||||
|
||||
def frequencies(word_list):
|
||||
assert(type(word_list) is list), "I need a list!"
|
||||
assert(word_list <> []), "I need a non-empty list!"
|
||||
assert(word_list != []), "I need a non-empty list!"
|
||||
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
@@ -47,12 +48,12 @@ def frequencies(word_list):
|
||||
|
||||
def sort(word_freq):
|
||||
assert(type(word_freq) is dict), "I need a dictionary!"
|
||||
assert(word_freq <> {}), "I need a non-empty dictionary!"
|
||||
assert(word_freq != {}), "I need a non-empty dictionary!"
|
||||
|
||||
try:
|
||||
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
except Exception as e:
|
||||
print "Sorted threw {0}: {1}".format(e)
|
||||
print("Sorted threw {0}: {1}".format(e))
|
||||
raise e
|
||||
|
||||
#
|
||||
@@ -65,8 +66,8 @@ try:
|
||||
assert(type(word_freqs) is list), "OMG! This is not a list!"
|
||||
assert(len(word_freqs) > 25), "SRSLY? Less than 25 words!"
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
except Exception as e:
|
||||
print "Something wrong: {0}".format(e)
|
||||
print("Something wrong: {0}".format(e))
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -25,7 +26,7 @@ def remove_stop_words(word_list):
|
||||
|
||||
def frequencies(word_list):
|
||||
assert(type(word_list) is list), "I need a list! I quit!"
|
||||
assert(word_list <> []), "I need a non-empty list! I quit!"
|
||||
assert(word_list != []), "I need a non-empty list! I quit!"
|
||||
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
@@ -37,7 +38,7 @@ def frequencies(word_list):
|
||||
|
||||
def sort(word_freqs):
|
||||
assert(type(word_freqs) is dict), "I need a dictionary! I quit!"
|
||||
assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!"
|
||||
assert(word_freqs != {}), "I need a non-empty dictionary! I quit!"
|
||||
|
||||
return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
|
||||
@@ -50,8 +51,8 @@ try:
|
||||
|
||||
assert(len(word_freqs) > 25), "OMG! Less than 25 words! I QUIT!"
|
||||
for tf in word_freqs[0:25]:
|
||||
print tf[0], ' - ', tf[1]
|
||||
print(tf[0], ' - ', tf[1])
|
||||
except Exception as e:
|
||||
print "Something wrong: {0}".format(e)
|
||||
print("Something wrong: {0}".format(e))
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -21,9 +22,9 @@ class TFPassiveAggressive:
|
||||
|
||||
def printme(self):
|
||||
if self._e == None:
|
||||
print self._value
|
||||
print(self._value)
|
||||
else:
|
||||
print self._e, " in ", self._offending_func.__name__
|
||||
print(self._e, " in ", self._offending_func.__name__)
|
||||
|
||||
#
|
||||
# The functions
|
||||
@@ -53,7 +54,7 @@ def remove_stop_words(word_list):
|
||||
|
||||
def frequencies(word_list):
|
||||
assert(type(word_list) is list), "I need a list! I quit!"
|
||||
assert(word_list <> []), "I need a non-empty list! I quit!"
|
||||
assert(word_list != []), "I need a non-empty list! I quit!"
|
||||
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
@@ -65,13 +66,13 @@ def frequencies(word_list):
|
||||
|
||||
def sort(word_freqs):
|
||||
assert(type(word_freqs) is dict), "I need a dictionary! I quit!"
|
||||
assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!"
|
||||
assert(word_freqs != {}), "I need a non-empty dictionary! I quit!"
|
||||
|
||||
return sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
|
||||
def top25_freqs(word_freqs):
|
||||
assert(type(word_freqs) is list), "I need a list! I quit!"
|
||||
assert(word_freqs <> {}), "I need a non-empty dictionary! I quit!"
|
||||
assert(word_freqs != {}), "I need a non-empty dictionary! I quit!"
|
||||
|
||||
top25 = ""
|
||||
for tf in word_freqs[0:25]:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, inspect
|
||||
|
||||
#
|
||||
@@ -11,7 +12,7 @@ class AcceptTypes():
|
||||
def __call__(self, f):
|
||||
def wrapped_f(*args):
|
||||
for i in range(len(self._args)):
|
||||
if type(args[i]) <> self._args[i]:
|
||||
if type(args[i]) != self._args[i]:
|
||||
raise TypeError("Expecting %s got %s" % (str(self._args[i]), str(type(args[i]))))
|
||||
return f(*args)
|
||||
return wrapped_f
|
||||
@@ -45,5 +46,5 @@ def sort(word_freq):
|
||||
|
||||
word_freqs = sort(frequencies(extract_words(sys.argv[1])))
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
|
||||
#
|
||||
@@ -19,7 +20,7 @@ class TFQuarantine:
|
||||
value = lambda : None
|
||||
for func in self._funcs:
|
||||
value = func(guard_callable(value))
|
||||
print guard_callable(value)
|
||||
print(guard_callable(value))
|
||||
|
||||
#
|
||||
# The functions
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, string, sqlite3, os.path
|
||||
|
||||
#
|
||||
@@ -66,4 +67,4 @@ with sqlite3.connect('tf.db') as connection:
|
||||
for i in range(25):
|
||||
row = c.fetchone()
|
||||
if row != None:
|
||||
print row[0] + ' - ' + str(row[1])
|
||||
print(row[0] + ' - ' + str(row[1]))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, itertools, operator
|
||||
|
||||
#
|
||||
@@ -46,4 +47,4 @@ stop_words[0] = set(open('../stop_words.txt').read().split(','))
|
||||
update()
|
||||
|
||||
for (w, c) in sorted_data[0][:25]:
|
||||
print w, '-', c
|
||||
print(w, '-', c)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, operator, string
|
||||
|
||||
def characters(filename):
|
||||
@@ -42,7 +43,7 @@ def count_and_sort(filename):
|
||||
# The main function
|
||||
#
|
||||
for word_freqs in count_and_sort(sys.argv[1]):
|
||||
print "-----------------------------"
|
||||
print("-----------------------------")
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
print(w, ' - ', c)
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
from threading import Thread
|
||||
from Queue import Queue
|
||||
@@ -114,7 +115,7 @@ class WordFrequencyController(ActiveWFObject):
|
||||
def _display(self, message):
|
||||
word_freqs = message[0]
|
||||
for (w, f) in word_freqs[0:25]:
|
||||
print w, ' - ', f
|
||||
print(w, ' - ', f)
|
||||
send(self._storage_manager, ['die'])
|
||||
self._stopMe = True
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import re, sys, operator, Queue, threading
|
||||
|
||||
# Two data spaces
|
||||
@@ -49,4 +50,4 @@ while not freq_space.empty():
|
||||
word_freqs[k] = count
|
||||
|
||||
for (w, c) in sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)[:25]:
|
||||
print w, '-', c
|
||||
print(w, '-', c)
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
from functools import reduce
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
|
||||
|
||||
#
|
||||
# Functions for map reduce
|
||||
#
|
||||
def partition(data_str, nlines):
|
||||
"""
|
||||
"""
|
||||
Partitions the input data_str (a big string)
|
||||
into chunks of nlines.
|
||||
"""
|
||||
@@ -14,8 +22,8 @@ def partition(data_str, nlines):
|
||||
yield '\n'.join(lines[i:i+nlines])
|
||||
|
||||
def split_words(data_str):
|
||||
"""
|
||||
Takes a string, returns a list of pairs (word, 1),
|
||||
"""
|
||||
Takes a string, returns a list of pairs (word, 1),
|
||||
one for each word in the input, so
|
||||
[(w1, 1), (w2, 1), ..., (wn, 1)]
|
||||
"""
|
||||
@@ -37,10 +45,10 @@ def split_words(data_str):
|
||||
return result
|
||||
|
||||
def count_words(pairs_list_1, pairs_list_2):
|
||||
"""
|
||||
"""
|
||||
Takes two lists of pairs of the form
|
||||
[(w1, 1), ...]
|
||||
and returns a list of pairs [(w1, frequency), ...],
|
||||
and returns a list of pairs [(w1, frequency), ...],
|
||||
where frequency is the sum of all the reported occurrences
|
||||
"""
|
||||
mapping = dict((k, v) for k, v in pairs_list_1)
|
||||
@@ -70,5 +78,4 @@ splits.insert(0, []) # Normalize input to reduce
|
||||
word_freqs = sort(reduce(count_words, splits))
|
||||
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
|
||||
print(w, ' - ', c)
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string
|
||||
from functools import reduce
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range # Python 3
|
||||
|
||||
|
||||
#
|
||||
# Functions for map reduce
|
||||
#
|
||||
def partition(data_str, nlines):
|
||||
"""
|
||||
"""
|
||||
Partitions the input data_str (a big string)
|
||||
into chunks of nlines.
|
||||
"""
|
||||
@@ -14,8 +22,8 @@ def partition(data_str, nlines):
|
||||
yield '\n'.join(lines[i:i+nlines])
|
||||
|
||||
def split_words(data_str):
|
||||
"""
|
||||
Takes a string, returns a list of pairs (word, 1),
|
||||
"""
|
||||
Takes a string, returns a list of pairs (word, 1),
|
||||
one for each word in the input, so
|
||||
[(w1, 1), (w2, 1), ..., (wn, 1)]
|
||||
"""
|
||||
@@ -38,14 +46,14 @@ def split_words(data_str):
|
||||
|
||||
def regroup(pairs_list):
|
||||
"""
|
||||
Takes a list of lists of pairs of the form
|
||||
Takes a list of lists of pairs of the form
|
||||
[[(w1, 1), (w2, 1), ..., (wn, 1)],
|
||||
[(w1, 1), (w2, 1), ..., (wn, 1)],
|
||||
...]
|
||||
and returns a dictionary mapping each unique word to the
|
||||
and returns a dictionary mapping each unique word to the
|
||||
corresponding list of pairs, so
|
||||
{ w1 : [(w1, 1), (w1, 1)...],
|
||||
w2 : [(w2, 1), (w2, 1)...],
|
||||
{ w1 : [(w1, 1), (w1, 1)...],
|
||||
w2 : [(w2, 1), (w2, 1)...],
|
||||
...}
|
||||
"""
|
||||
mapping = {}
|
||||
@@ -56,11 +64,11 @@ def regroup(pairs_list):
|
||||
else:
|
||||
mapping[p[0]] = [p]
|
||||
return mapping
|
||||
|
||||
|
||||
def count_words(mapping):
|
||||
"""
|
||||
"""
|
||||
Takes a mapping of the form (word, [(word, 1), (word, 1)...)])
|
||||
and returns a pair (word, frequency), where frequency is the
|
||||
and returns a pair (word, frequency), where frequency is the
|
||||
sum of all the reported occurrences
|
||||
"""
|
||||
def add(x, y):
|
||||
@@ -87,5 +95,4 @@ splits_per_word = regroup(splits)
|
||||
word_freqs = sort(map(count_words, splits_per_word.items()))
|
||||
|
||||
for (w, c) in word_freqs[0:25]:
|
||||
print w, ' - ', c
|
||||
|
||||
print(w, ' - ', c)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, operator, string, os, threading, re
|
||||
from util import getch, cls, get_input
|
||||
from time import sleep
|
||||
@@ -38,7 +39,7 @@ class FreqObserver(threading.Thread):
|
||||
def refresh_screen(data):
|
||||
# clear screen
|
||||
cls()
|
||||
print data
|
||||
print(data)
|
||||
sys.stdout.flush()
|
||||
|
||||
data_str = ""
|
||||
@@ -57,7 +58,7 @@ class WordsCounter:
|
||||
for line in f:
|
||||
yield [w for w in re.findall('[a-z]{2,}', line.lower()) if w not in stopwords]
|
||||
|
||||
words = non_stop_words().next()
|
||||
words = next(non_stop_words())
|
||||
lock.acquire()
|
||||
for w in words:
|
||||
self.freqs[w] = 1 if w not in self.freqs else self.freqs[w]+1
|
||||
@@ -66,8 +67,8 @@ class WordsCounter:
|
||||
#
|
||||
# The controller
|
||||
#
|
||||
print "Press space bar to fetch words from the file one by one"
|
||||
print "Press ESC to switch to automatic mode"
|
||||
print("Press space bar to fetch words from the file one by one")
|
||||
print("Press ESC to switch to automatic mode")
|
||||
model = WordsCounter()
|
||||
view = FreqObserver(model.freqs)
|
||||
with open(sys.argv[1]) as f:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, collections
|
||||
|
||||
class WordFrequenciesModel:
|
||||
@@ -19,7 +20,7 @@ class WordFrequenciesModel:
|
||||
for obs in self._observers:
|
||||
obs.render()
|
||||
except IOError:
|
||||
print "File not found"
|
||||
print("File not found")
|
||||
self.freqs = {}
|
||||
|
||||
class WordFrequenciesView:
|
||||
@@ -30,7 +31,7 @@ class WordFrequenciesView:
|
||||
def render(self):
|
||||
sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
for (w, c) in sorted_freqs[:25]:
|
||||
print w, '-', c
|
||||
print(w, '-', c)
|
||||
|
||||
class WordFrequencyController:
|
||||
def __init__(self, model, view):
|
||||
@@ -39,7 +40,7 @@ class WordFrequencyController:
|
||||
def run(self):
|
||||
self._model.update(sys.argv[1])
|
||||
while True:
|
||||
print "Next file: "
|
||||
print("Next file: ")
|
||||
sys.stdout.flush()
|
||||
filename = sys.stdin.readline().strip()
|
||||
self._model.update(filename)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, collections
|
||||
|
||||
class WordFrequenciesModel:
|
||||
@@ -14,7 +15,7 @@ class WordFrequenciesModel:
|
||||
words = re.findall('[a-z]{2,}', open(path_to_file).read().lower())
|
||||
self.freqs = collections.Counter(w for w in words if w not in self.stopwords)
|
||||
except IOError:
|
||||
print "File not found"
|
||||
print("File not found")
|
||||
self.freqs = {}
|
||||
|
||||
class WordFrequenciesView:
|
||||
@@ -24,7 +25,7 @@ class WordFrequenciesView:
|
||||
def render(self):
|
||||
sorted_freqs = sorted(self._model.freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
for (w, c) in sorted_freqs[0:25]:
|
||||
print w, '-', c
|
||||
print(w, '-', c)
|
||||
|
||||
class WordFrequencyController:
|
||||
def __init__(self, model, view):
|
||||
@@ -33,7 +34,7 @@ class WordFrequencyController:
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
print "Next file: "
|
||||
print("Next file: ")
|
||||
sys.stdout.flush()
|
||||
filename = sys.stdin.readline().strip()
|
||||
self._model.update(filename)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import sys, os
|
||||
import os
|
||||
|
||||
#
|
||||
# getch in a platform-independent way
|
||||
@@ -65,11 +65,10 @@ def get_input():
|
||||
if not interactive:
|
||||
return True
|
||||
|
||||
while True:
|
||||
while True:
|
||||
key = ord(getch())
|
||||
if key == 32: # space bar
|
||||
return True
|
||||
elif key == 27: # ESC
|
||||
interactive = False
|
||||
return True
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
from __future__ import print_function
|
||||
import re, string, sys
|
||||
|
||||
with open("../stop_words.txt") as f:
|
||||
@@ -22,7 +23,7 @@ def quit_handler(args):
|
||||
|
||||
def upload_get_handler(args):
|
||||
return "Name of file to upload?", ["post", "file"]
|
||||
|
||||
|
||||
def upload_post_handler(args):
|
||||
def create_data(filename):
|
||||
if filename in data:
|
||||
@@ -32,7 +33,7 @@ def upload_post_handler(args):
|
||||
for w in [x.lower() for x in re.split("[^a-zA-Z]+", f.read()) if len(x) > 0 and x.lower() not in stops]:
|
||||
word_freqs[w] = word_freqs.get(w, 0) + 1
|
||||
word_freqsl = word_freqs.items()
|
||||
word_freqsl.sort(lambda x, y: cmp(y[1], x[1]))
|
||||
word_freqsl.sort(key=lambda x: x[1], reverse=True)
|
||||
data[filename] = word_freqsl
|
||||
|
||||
if args == None:
|
||||
@@ -49,7 +50,7 @@ def word_get_handler(args):
|
||||
if word_index < len(data[filename]):
|
||||
return data[filename][word_index]
|
||||
else:
|
||||
return ("no more words", 0)
|
||||
return ("no more words", 0)
|
||||
|
||||
filename = args[0]; word_index = args[1]
|
||||
word_info = get_word(filename, word_index)
|
||||
@@ -57,16 +58,16 @@ def word_get_handler(args):
|
||||
rep += "\n\nWhat would you like to do next?"
|
||||
rep += "\n1 - Quit" + "\n2 - Upload file"
|
||||
rep += "\n3 - See next most-frequently occurring word"
|
||||
links = {"1" : ["post", "execution", None],
|
||||
"2" : ["get", "file_form", None],
|
||||
links = {"1" : ["post", "execution", None],
|
||||
"2" : ["get", "file_form", None],
|
||||
"3" : ["get", "word", [filename, word_index+1]]}
|
||||
return rep, links
|
||||
|
||||
# Handler registration
|
||||
handlers = {"post_execution" : quit_handler,
|
||||
"get_default" : default_get_handler,
|
||||
"get_file_form" : upload_get_handler,
|
||||
"post_file" : upload_post_handler,
|
||||
"get_default" : default_get_handler,
|
||||
"get_file_form" : upload_get_handler,
|
||||
"post_file" : upload_post_handler,
|
||||
"get_word" : word_get_handler }
|
||||
|
||||
# The "server" core
|
||||
@@ -81,7 +82,7 @@ def handle_request(verb, uri, args):
|
||||
|
||||
# A very simple client "browser"
|
||||
def render_and_get_input(state_representation, links):
|
||||
print state_representation
|
||||
print(state_representation)
|
||||
sys.stdout.flush()
|
||||
if type(links) is dict: # many possible next states
|
||||
input = sys.stdin.readline().strip()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys, re, operator, string, inspect
|
||||
|
||||
# Reusing the defensive style program to illustrate this
|
||||
@@ -20,7 +21,7 @@ def extract_words(path_to_file):
|
||||
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)
|
||||
print("I/O error({0}) when opening {1}: {2}".format(e.errno, path_to_file, e.strerror))
|
||||
fail = True
|
||||
|
||||
if not fail:
|
||||
@@ -31,7 +32,7 @@ def extract_words(path_to_file):
|
||||
with open('../stop_words.txt') as f:
|
||||
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)
|
||||
print("I/O error({0}) when opening ../stops_words.txt: {1}".format(e.errno, e.strerror))
|
||||
fail = True
|
||||
|
||||
if not fail:
|
||||
@@ -44,7 +45,7 @@ def frequencies(word_list):
|
||||
Takes a list of words and returns a dictionary associating
|
||||
words with frequencies of occurrence
|
||||
"""
|
||||
if type(word_list) is list and word_list <> []:
|
||||
if type(word_list) is list and word_list != []:
|
||||
word_freqs = {}
|
||||
for w in word_list:
|
||||
if w in word_freqs:
|
||||
@@ -61,7 +62,7 @@ def sort(word_freq):
|
||||
and returns a list of pairs where the entries are
|
||||
sorted by frequency
|
||||
"""
|
||||
if type(word_freq) is dict and word_freq <> {}:
|
||||
if type(word_freq) is dict and word_freq != {}:
|
||||
return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True)
|
||||
else:
|
||||
return []
|
||||
@@ -73,5 +74,5 @@ filename = sys.argv[1] if len(sys.argv) > 1 else "../input.txt"
|
||||
word_freqs = sort(frequencies(extract_words(filename)))
|
||||
|
||||
for tf in word_freqs[0:25]:
|
||||
print tf[0], ' - ', tf[1]
|
||||
print(tf[0], ' - ', tf[1])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user