Created
August 22, 2019 17:11
-
-
Save takanakahiko/98974aeb78a91d6c5ac5f29697117ec7 to your computer and use it in GitHub Desktop.
This file contains 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 requests | |
import xmltodict | |
import fileinput | |
def getAccess(url, **option): | |
r = requests.get(url, params=option) | |
return r.text | |
def addReplaceWord(replaceList,word,begin="$",sep="#",end="$"): | |
if word['Surface'] != word['Furigana']: | |
rubytext = "%s%s%s%s%s" % (begin,word['Surface'],sep,word['Furigana'],end) | |
replaceList[word['Surface']] = rubytext | |
return replaceList | |
def replace(sentence,begin="$",sep="#",end="$"): | |
url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana" | |
appid = "XXXXXXXXXXXXXXXXXXXXXXXXX" | |
ret = getAccess(url,appid=appid,sentence=sentence,grade=1) | |
tmp = xmltodict.parse(ret) | |
replaceList = {} | |
if 'ResultSet' in tmp: | |
for val in (tmp['ResultSet']['Result']['WordList']['Word']): | |
if 'SubWordList' in val: | |
for subword in val['SubWordList']['SubWord']: | |
replaceList = addReplaceWord(replaceList,subword,begin,sep,end) | |
elif 'Furigana' in val: | |
replaceList = addReplaceWord(replaceList,val,begin,sep,end) | |
text = sentence | |
for k, v in replaceList.items(): | |
text = text.replace(k,v) | |
return text | |
if __name__ == "__main__": | |
for line in fileinput.input(): | |
line = replace(line) | |
line = line.replace(",","、") | |
line = line.replace(".","。") | |
print(line, end="") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment