First version of the reactive style. Still needs work.

This commit is contained in:
Crista Lopes
2013-10-17 18:32:18 -07:00
parent 40637fdc9b
commit a9ec3715f9
2 changed files with 159 additions and 0 deletions

98
32-reactive/tf-32.py Normal file
View File

@@ -0,0 +1,98 @@
#!/usr/bin/env python
import sys, operator, string, os
from util import getch, cls
from time import sleep
def refresh_screen(data):
# clear screen
cls()
print data
sys.stdout.flush()
def update_display(tuples):
data_str = ""
for tf in tuples:
data_str += str(tf[0]) + ' - ' + str(tf[1]) + '\n'
refresh_screen(data_str)
automatic = False
def get_input():
global automatic
if automatic:
return True
while True:
key = ord(getch())
if key == 32: # space bar
return True
elif key == 27: # ESC
automatic = True
return True
else: pass
f = open(sys.argv[1])
def characters(filename):
c = f.read(1)
if c != "":
yield c
else:
raise StopIteration()
def all_words(filename):
found_word = False
start_char = True
while not found_word:
try:
c = characters(filename).next()
except StopIteration:
raise StopIteration()
if start_char == True:
word = ""
if c.isalnum():
# We found the start of a word
word = c.lower()
start_char = False
else:
if c.isalnum():
word += c.lower()
else:
# We found the end of a word, emit it
start_char = True
found_word = True
yield word
def non_stop_words(filename):
stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase))
while True:
w = all_words(filename).next()
if not w in stopwords:
yield w
def count_and_sort(filename):
freqs_0 = ()
freqs_1 = ()
freqs = {}
while get_input():
try:
w = non_stop_words(filename).next()
freqs[w] = 1 if w not in freqs else freqs[w]+1
freqs_0 = freqs_1
freqs_1 = sorted(freqs.iteritems(), key=operator.itemgetter(1), reverse=True)
if (freqs_1[0:25] != freqs_0[0:25]):
update_display(freqs_1[0:25])
except StopIteration:
break
return freqs_1
#
# The main function
#
print "Press space bar to fetch words from the file one by one"
print "Press ESC to switch to automatic mode"
count_and_sort(sys.argv[1])

61
32-reactive/util.py Normal file
View File

@@ -0,0 +1,61 @@
import sys, os
#
# getch in a platform-independent way
# Credit: http://code.activestate.com/recipes/134892/
#
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
try:
self.impl = _GetchMacCarbon()
except(AttributeError, ImportError):
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
class _GetchMacCarbon:
def __init__(self):
import Carbon
def __call__(self):
import Carbon
if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask
return ''
else:
(what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
return chr(msg & 0x000000FF)
getch = _Getch()
def cls():
os.system(['clear','cls'][os.name == 'nt'])