Skip to content

Instantly share code, notes, and snippets.

@jsharf
Created August 1, 2012 10:35
Show Gist options
  • Save jsharf/3225699 to your computer and use it in GitHub Desktop.
Save jsharf/3225699 to your computer and use it in GitHub Desktop.
Beta wikipedia voice browser for android sl4a. Say a topic or "more" to hear more about previous topic
import urllib
import android
import string
from HTMLParser import HTMLParser
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def find_nth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
def lookup(topic, paragraph=1):
topic = topic.replace(" ", "_")
page = urllib.urlopen("http://en.m.wikipedia.org/wiki/"+topic);
content = page.read();
page.close();
content = content[content.find('<div id="content">'):content.find('<div class="section">')]
content = strip_tags(content[find_nth(content, '<p>', paragraph):find_nth(content,'</p>',paragraph)])
droid.ttsSpeak(content)
droid = android.Android()
input = ""
tmp = ""
p = 1
while(1):
if(not droid.ttsIsSpeaking().result):
tmp = input
input = droid.recognizeSpeech("Search Wikipedia...").result
if(input != "more"):
lookup(input)
p=1
else:
input = tmp
p+=1
lookup(input, p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment