Created
August 1, 2012 10:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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