Created
May 14, 2012 18:00
-
-
Save foota/2695369 to your computer and use it in GitHub Desktop.
SL4A speaks in Japanese
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
| # -*- coding: utf-8 -*- | |
| import android | |
| import sys,os,urllib,urllib2,simplejson | |
| from BeautifulSoup import BeautifulSoup | |
| YAHOO_APP_ID='Yahoo!デベロッパーネットワークのアプリケーションID' | |
| KEITAISO_API='http://jlp.yahooapis.jp/MAService/V1/parse?appid=%s&results=ma&sentence=%s' | |
| KANA=[u'きゃ',u'きぃ',u'きゅ',u'きぇ',u'きょ', | |
| u'くぁ',u'くぃ',u'くぅ',u'くぇ',u'くぉ', | |
| u'ぎゃ',u'ぎぃ',u'ぎゅ',u'ぎぇ',u'ぎょ', | |
| u'ぐぁ',u'ぐぃ',u'ぐぅ',u'ぐぇ',u'ぐぉ', | |
| u'しゃ',u'しぃ',u'しゅ',u'しぇ',u'しょ', | |
| u'すぁ',u'すぃ',u'すぅ',u'すぇ',u'すぉ', | |
| u'じゃ',u'じぃ',u'じゅ',u'じぇ',u'じょ', | |
| u'ちゃ',u'ちぃ',u'ちゅ',u'ちぇ',u'ちょ', | |
| u'てゃ',u'てぃ',u'てゅ',u'てぇ',u'てょ', | |
| u'とぁ',u'とぃ',u'とぅ',u'とぇ',u'とぉ', | |
| u'ぢゃ',u'ぢぃ',u'ぢゅ',u'ぢぇ',u'ぢょ', | |
| u'でゃ',u'でぃ',u'でゅ',u'でぇ',u'でょ', | |
| u'どぁ',u'どぃ',u'どぅ',u'どぇ',u'どぉ', | |
| u'にゃ',u'にぃ',u'にゅ',u'にぇ',u'にょ', | |
| u'ひゃ',u'ひぃ',u'ひゅ',u'ひぇ',u'ひょ', | |
| u'ふぁ',u'ふぃ',u'ふぅ',u'ふぇ',u'ふぉ', | |
| u'びゃ',u'びぃ',u'びゅ',u'びぇ',u'びょ', | |
| u'ぴゃ',u'ぴぃ',u'ぴゅ',u'ぴぇ',u'ぴょ', | |
| u'みゃ',u'みぃ',u'みゅ',u'みぇ',u'みょ', | |
| u'りゃ',u'りぃ',u'りゅ',u'りぇ',u'りょ', | |
| u'うぁ',u'うぃ',u'うぇ',u'うぉ'] | |
| PRON=['kya','kyi','kyu','kyei','kio', | |
| 'qa','qi','qwu','qei','qo', | |
| 'gya','gyi','gyu','gyei','gyo', | |
| 'gwa','gwi','gwu','gwei','gwo', | |
| 'sha','syi','shu','shei','sho', | |
| 'swa','swi','swu','swei','swo', | |
| 'ja','zee','juu','jei','jo', | |
| 'cha','tyi','chu','chei','cho', | |
| 'tha','thi','thu','thei','tho', | |
| 'twa','twi','two','twei','two', | |
| 'dya','dyi','dyu','dyei','dyo', | |
| 'dha','dhi','dhu','dhei','dho', | |
| 'dwa','dwi','dwu','dwei','dwo', | |
| 'nya','nyi','nyu','nyei','nyo', | |
| 'hya','hyi','hyu','hyei','hyo', | |
| 'fa','fi','foo','fei','fo', | |
| 'bya','byi','byu','byei','byo', | |
| 'pya','pyi','pyu','pyei','pyo', | |
| 'mya','myi','myu','myei','myo', | |
| 'rya','ryi','ryu','ryei','ryo', | |
| 'wha','wi','wei','wo'] | |
| KANA+=u'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをんがぎぐげござじずぜぞだぢづでどばびぶべぼぱぴぷぺぽぁぃぅぇぉ' | |
| PRON+=['a','ee','oo','ae','o', | |
| 'ka','kee','koo','kei','ko', | |
| 'sa','she','soo','sei','so', | |
| 'ta','chee','tsu','tei','toe', | |
| 'na','nee','nuu','neigh','no', | |
| 'ha','hee','foo','hey','ho', | |
| 'ma','mee','moo','mei','mo', | |
| 'yah','you','yor', | |
| 'ra','ree','roo','rei','ro', | |
| 'wa','ee','ae','wo','nn', | |
| 'ga','gee','goo','gei','go', | |
| 'za','zee','zoo','zei','zo', | |
| 'da','zee','doo','dei','do', | |
| 'ba','bee','boo','bei','bo', | |
| 'pa','pee','poo','pei','po', | |
| 'a','ee','oo','ae','o'] | |
| def get_keitaiso(text): | |
| xml=urllib2.urlopen(KEITAISO_API % (YAHOO_APP_ID,urllib.quote(text.encode('utf-8')))).read() | |
| ret='' | |
| for data in BeautifulSoup(xml)('ma_result'): | |
| for r,p in zip(data('reading'),data('pos')): | |
| d,x=r.renderContents(),p.renderContents() | |
| if x=='助詞' and d=='は': ret+='わ' | |
| elif x!='特殊': ret+=d | |
| ret=ret.decode('utf-8') | |
| T={} | |
| for i in range(86): T[ord(u'ァ')+i]=unichr(ord(u'ぁ')+i) | |
| ret=ret.translate(T) | |
| for k,r in zip(KANA,PRON): ret=ret.replace(k,r+' ') | |
| rett=ret; | |
| for i,c in enumerate(ret): | |
| c=c.encode('utf-8') | |
| if c=='ー': | |
| ret=ret.replace(u'ー',rett[i-2]+' ',1) | |
| if c=='っ': | |
| if len(rett)>i+1: | |
| ret=ret.replace(u'っ',rett[i+1],1) | |
| else: | |
| ret=ret.replace(u'っ','',1) | |
| return ret | |
| def translator(text,from_lang,to_lang): | |
| url='http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s' % (urllib.quote(text.encode('utf-8')),from_lang,to_lang) | |
| data=simplejson.loads(urllib2.urlopen(url).read()) | |
| return data['responseData']['translatedText'] | |
| droid=android.Android() | |
| text=droid.dialogGetInput(u'日本語を喋ります',u'何を喋らせますか?').result.strip() | |
| text=translator(translator(text,'ja','en'),'en','ja') | |
| droid.ttsSpeak(get_keitaiso(text)) | |
| droid.makeToast(text) |
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
| # -*- coding: utf-8 -*- | |
| import android | |
| import sys,os,urllib,urllib2 | |
| from BeautifulSoup import BeautifulSoup | |
| YAHOO_APP_ID='Yahoo!デベロッパーネットワークのアプリケーションID' | |
| KEITAISO_API='http://jlp.yahooapis.jp/MAService/V1/parse?appid=%s&results=ma&sentence=%s' | |
| KANA=[u'きゃ',u'きぃ',u'きゅ',u'きぇ',u'きょ', | |
| u'くぁ',u'くぃ',u'くぅ',u'くぇ',u'くぉ', | |
| u'ぎゃ',u'ぎぃ',u'ぎゅ',u'ぎぇ',u'ぎょ', | |
| u'ぐぁ',u'ぐぃ',u'ぐぅ',u'ぐぇ',u'ぐぉ', | |
| u'しゃ',u'しぃ',u'しゅ',u'しぇ',u'しょ', | |
| u'すぁ',u'すぃ',u'すぅ',u'すぇ',u'すぉ', | |
| u'じゃ',u'じぃ',u'じゅ',u'じぇ',u'じょ', | |
| u'ちゃ',u'ちぃ',u'ちゅ',u'ちぇ',u'ちょ', | |
| u'てゃ',u'てぃ',u'てゅ',u'てぇ',u'てょ', | |
| u'とぁ',u'とぃ',u'とぅ',u'とぇ',u'とぉ', | |
| u'ぢゃ',u'ぢぃ',u'ぢゅ',u'ぢぇ',u'ぢょ', | |
| u'でゃ',u'でぃ',u'でゅ',u'でぇ',u'でょ', | |
| u'どぁ',u'どぃ',u'どぅ',u'どぇ',u'どぉ', | |
| u'にゃ',u'にぃ',u'にゅ',u'にぇ',u'にょ', | |
| u'ひゃ',u'ひぃ',u'ひゅ',u'ひぇ',u'ひょ', | |
| u'ふぁ',u'ふぃ',u'ふぅ',u'ふぇ',u'ふぉ', | |
| u'びゃ',u'びぃ',u'びゅ',u'びぇ',u'びょ', | |
| u'ぴゃ',u'ぴぃ',u'ぴゅ',u'ぴぇ',u'ぴょ', | |
| u'みゃ',u'みぃ',u'みゅ',u'みぇ',u'みょ', | |
| u'りゃ',u'りぃ',u'りゅ',u'りぇ',u'りょ', | |
| u'うぁ',u'うぃ',u'うぇ',u'うぉ'] | |
| PRON=['kya','kyi','kyu','kyei','kio', | |
| 'qa','qi','qwu','qei','qo', | |
| 'gya','gyi','gyu','gyei','gyo', | |
| 'gwa','gwi','gwu','gwei','gwo', | |
| 'sha','syi','shu','shei','sho', | |
| 'swa','swi','swu','swei','swo', | |
| 'ja','zee','juu','jei','jo', | |
| 'cha','tyi','chu','chei','cho', | |
| 'tha','thi','thu','thei','tho', | |
| 'twa','twi','two','twei','two', | |
| 'dya','dyi','dyu','dyei','dyo', | |
| 'dha','dhi','dhu','dhei','dho', | |
| 'dwa','dwi','dwu','dwei','dwo', | |
| 'nya','nyi','nyu','nyei','nyo', | |
| 'hya','hyi','hyu','hyei','hyo', | |
| 'fa','fi','foo','fei','fo', | |
| 'bya','byi','byu','byei','byo', | |
| 'pya','pyi','pyu','pyei','pyo', | |
| 'mya','myi','myu','myei','myo', | |
| 'rya','ryi','ryu','ryei','ryo', | |
| 'wha','wi','wei','wo'] | |
| KANA+=u'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをんがぎぐげござじずぜぞだぢづでどばびぶべぼぱぴぷぺぽぁぃぅぇぉ' | |
| PRON+=['a','ee','oo','ae','o', | |
| 'ka','kee','koo','kei','ko', | |
| 'sa','she','soo','sei','so', | |
| 'ta','chee','tsu','tei','toe', | |
| 'na','nee','nuu','neigh','no', | |
| 'ha','hee','foo','hey','ho', | |
| 'ma','mee','moo','mei','mo', | |
| 'yah','you','yor', | |
| 'ra','ree','roo','rei','ro', | |
| 'wa','ee','ae','wo','nn', | |
| 'ga','gee','goo','gei','go', | |
| 'za','zee','zoo','zei','zo', | |
| 'da','zee','doo','dei','do', | |
| 'ba','bee','boo','bei','bo', | |
| 'pa','pee','poo','pei','po', | |
| 'a','ee','oo','ae','o'] | |
| def get_keitaiso(text): | |
| xml=urllib2.urlopen(KEITAISO_API % (YAHOO_APP_ID,urllib.quote(text.encode('utf-8')))).read() | |
| ret='' | |
| for data in BeautifulSoup(xml)('ma_result'): | |
| for r,p in zip(data('reading'),data('pos')): | |
| d,x=r.renderContents(),p.renderContents() | |
| if x=='助詞' and d=='は': ret+='わ' | |
| elif x!='特殊': ret+=d | |
| ret=ret.decode('utf-8') | |
| T={} | |
| for i in range(86): T[ord(u'ァ')+i]=unichr(ord(u'ぁ')+i) | |
| ret=ret.translate(T) | |
| for k,r in zip(KANA,PRON): ret=ret.replace(k,r+' ') | |
| rett=ret; | |
| for i,c in enumerate(ret): | |
| c=c.encode('utf-8') | |
| if c=='ー': | |
| ret=ret.replace(u'ー',rett[i-2]+' ',1) | |
| if c=='っ': | |
| if len(rett)>i+1: | |
| ret=ret.replace(u'っ',rett[i+1],1) | |
| else: | |
| ret=ret.replace(u'っ','',1) | |
| return ret | |
| droid=android.Android() | |
| text=droid.dialogGetInput(u'日本語を喋ります',u'何を喋らせますか?').result.strip() | |
| droid.ttsSpeak(get_keitaiso(text)) | |
| droid.makeToast(text) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://handasse.blogspot.com/2010/11/sl4apythonandroid.html