Hi Bob:
Could you specify what it does until now? where it fails? which is the step 
you would like to improve?

On a sidenote here is a script which will get text, and transform it into 
MP3 through google's translator (its instant, as soon as you use it you 
have the files in your computer). Im posting it because its 'similar' to 
yours, and maybe you can find some information on it.

#http://glowingpython.blogspot.com.es/2012/11/text-to-speech-with-correct-intonation.html
> import urllib2
> import os
>
>
> def parseText(text):
> """ returns a list of sentences with less than 100 caracters """
> toSay = []
> punct = [',',':',';','.','?','!'] # punctuation
> words = text.split(' ')
> sentence = ''
> for w in words:
> if w[len(w)-1] in punct: # encountered a punctuation mark
> if (len(sentence)+len(w)+1 < 100): # is there enough space?
> sentence += ' '+w # add the word
> toSay.append(sentence.strip()) # save the sentence
> else:
> toSay.append(sentence.strip()) # save the sentence
> toSay.append(w.strip()) # save the word as a sentence
> sentence = '' # start another sentence
> else:
> if (len(sentence)+len(w)+1 < 100):
> sentence += ' '+w # add the word
> else:
> toSay.append(sentence.strip()) # save the sentence
> sentence = w # start a new sentence
> if len(sentence) > 0:
> toSay.append(sentence.strip())
> return toSay
> text = 'Think of color, pitch, loudness, heaviness, and hotness. Each is 
> the topic of a branch of physics.'
> g.es(text)
> toSay = parseText(text)
> google_translate_url = 'http://translate.google.com/translate_tts'
> opener = urllib2.build_opener()
> opener.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; 
> Windows NT 5.0)')]
> for i,sentence in enumerate(toSay):
> print i,len(sentence), sentence
> response = opener.open(google_translate_url+'?q='+sentence.replace(' 
> ','%20')+'&tl=en')
> ofp = open(str(i)+'speech_google.mp3','wb')
> ofp.write(response.read())
> ofp.close()
> os.system('cvlc --play-and-exit -q '+str(i)+'speech_google.mp3')

  

On Wednesday, May 29, 2013 12:36:47 PM UTC+2, Bob Wong wrote:
>
> i have been with Leo for a short time, but i realized that Leo is very 
> powerful than what i can imagine.
> with an need, i really hope someone could share the highlights to make it 
> possible.  very appreciated!
>
> I had tried to figure out this function, so i wrote a small button to work 
> with google dict api. let me share the code as below. hope to get some 
> response/suggestions to improve this. I assume there must be a better 
> approach to fulfill this function.   :) don't laugh at my poor code, as i 
> am just new to python and Leo. 
>
> >>>>>>>>>code to work with google dictionary>>>>>>>>
>
> # -*- coding: utf8 -*- 
> @language python
>
> # 
> http://www.jsmix.com/blog/others/google-dictionary-api-and-voice-library.html
> g.redirectStderr() # Redirect stderr to the current log pane.
> g.redirectStdout() # Redirect stdout to the current log pane.
>
> import codecs
> import socket
> import urllib2
>
> timeout = 5 # timeout en seconds
> socket.setdefaulttimeout(timeout)
> w = c.frame.body.bodyCtrl # Leo's body pane.
> urlStr_1 = '
> http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=
> '
> urlStr_2 = w.getSelectedText().strip().lower().replace(' ','_') # Return 
> selected text if any. default = ''
> urlStr_3 = '&sl=en&tl=en&restrict=pr%2Cde&client=te'
>
> # initialize the str in case no data from google
>
> g_query = urlStr_2
> g_meaning_1st = 'NO-meaning'
> g_type = 'NO-type'
> g_mp3url= 'NO-mp3'
> g_phonetic = 'NO-phonetic'
>
> try:
>     
>     assert len(urlStr_2) > 1
>                 #get dict html data from google, it is a JSON
>     req = urllib2.Request(urlStr_1 + urlStr_2 + urlStr_3)
>     handle = urllib2.urlopen(req)
>     webStr = handle.read()
>                 #webStr = urlopen(urlStr_1 + urlStr_2 + urlStr_3, 
> timeout=5).read()
>                 # convert str to a dict obj
>     dictinfo = eval(webStr[25:-10])
>                 #dict.keys()=['query', 'webDefinitions', 'targetLanguage', 
> 'primaries', 'sourceLanguage']
>                 #dictinfo['primaries'][0] #dict.keys()= ['type', 'terms', 
> 'entries']
>     
>     g_query = dictinfo['primaries'][0]['terms'][0]['text'] 
>     g_meaning_1st = 
> dictinfo['primaries'][0]['entries'][1]['terms'][0]['text']
>     g_type  = dictinfo['primaries'][0]['terms'][0]['labels'][0]['text']
>     g_mp3url= dictinfo['primaries'][0]['terms'][2]['text'] 
>     g_phonetic = dictinfo['primaries'][0]['terms'][1]['text'] 
>
> except AssertionError:
>     g.es('No Text Selected!',color='red',tabName='Dict')
> except TypeError:
>     g.es('webStr is abnormal!',color='red',tabName='Dict')
> except IndexError:
>     g.es('No mp3/phonetic!',color='red',tabName='Dict')
> except SyntaxError:
>     g.es('Nothing from Google, or illegal 
> query!',color='red',tabName='Dict')
> except KeyError:
>     g.es('No definition found!',color='red',tabName='Dict')
>
> except:
>     raise
>
> else:
>     Body = g_mp3url + '\n\n' + g_query + ' | ' + g_type + ' ' + g_phonetic 
> + '\n\n' + g_meaning_1st #define output format
>     
>     for p in c.all_unique_positions():
>         if p.h == '@button DictGoogle @keys Alt-d':
>             child = p.insertAsLastChild()
>             child.h = g_query + ' | ' + g_type 
>             child.b = Body.decode("utf-8")
>             c.redraw()
>             break
>
> finally:
>     Body = g_mp3url + '\n\n' + g_query + ' | ' + g_type + ' ' + g_phonetic 
> + '\n\n' + g_meaning_1st
>     c.frame.log.selectTab('Dict')
>     g.es('>>>',tabName='Dict')
>     g.es(Body.decode("utf-8"),color='blue',tabName='Dict')
>     
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to