Skip to content

Instantly share code, notes, and snippets.

@sdziallas
Created May 7, 2012 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdziallas/2629901 to your computer and use it in GitHub Desktop.
Save sdziallas/2629901 to your computer and use it in GitHub Desktop.
Smart Word Blocks
# by Cypress Frankenfeld and Sebastian Dziallas
import serial
import signal
import sys
import subprocess
import os
import re
import qrcode
def signal_handler(signal, frame):
print('closing')
ser.close()
sys.exit(0)
def grab_words():
signal.signal(signal.SIGINT, signal_handler)
ser = serial.Serial('/dev/ttyACM0', 9600)
d = qrcode.Decoder()
cmd = 'streamer -c /dev/video1 -f jpeg -o $HOME/out.jpeg -s 640x480'
decoded = 0
words = []
while True:
x = ser.readline()
if decoded == 0 and int(x) < 55:
print int(x)
subprocess.call(cmd, shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
subprocess.call('convert $HOME/out.jpeg $HOME/out.png', shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
if d.decode('out.png'):
# make sure that the list this generates is actually valid
words.append(d.result)
decoded = 1
return words
if decoded == 1 and int(x) > 60:
decoded = 0
def splitParagraphIntoSentences(paragraph):
''' break a paragraph into sentences
and return a list '''
# regular expressions are easiest (and fastest)
sentenceEnders = re.compile('[.!?]')
sentenceList = sentenceEnders.split(paragraph)
return sentenceList
def parse_book(filename):
f = open(filename, 'r')
read = f.read()
text = read.translate(None, '\n\r')
sentences = splitParagraphIntoSentences(text)
return sentences
def parse_words(sentences, words):
match = []
length = len(words)
compare = sentences
for h in range(0,length):
word = words[length]
if h > 0:
compare = match
for i in compare:
pattern = "(?P<sentence>.*?%s.*?)" % word
hit = re.search(pattern, i)
if hit != None:
sentence = hit.group("sentence")
match.append(sentence)
else:
return match
return match
sentences = parse_book('sherlock.txt')
words = grab_words()
match = parse_words(sentences, words)
index = sentences.index(match[0])
print sentences[index - 1]
print sentences[index]
print sentences[index + 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment