Last active
November 8, 2020 11:05
-
-
Save sleepless-se/c97e00cdae94722696b7364021eff0b5 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
import requests | |
import xmltodict | |
class YahooAPI: | |
_base_url = 'https://jlp.yahooapis.jp/FuriganaService/V1/furigana' | |
_client_id = '' | |
_secret_key = '' | |
kana_xml = None | |
kana_api_url = None | |
headers = None | |
grade = 1 | |
def __init__(self): | |
self.headers = { | |
'User-Agent': f'Yahoo AppID: {self._client_id}', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
} | |
def _set_headers_contents_length(self, text): | |
url_encoded_text = requests.utils.quote(text) | |
self.headers['Content-Length'] = str(9 + len(url_encoded_text)) | |
def _ask_kana_xml(self, text): | |
if self.kana_xml is not None: return self.kana_xml | |
data = { | |
'sentence':text, | |
'grade':self.grade, | |
} | |
self._set_headers_contents_length(text) | |
result = requests.post(self._base_url,data=data,headers=self.headers) | |
self.kana_xml = xmltodict.parse(result.text) | |
def _create_kana_text(self): | |
if self.kana_xml is None: | |
raise Exception('self.kana_xml is None. Please run ask_kana_xml() before this function.') | |
word_list = self.kana_xml['ResultSet']['Result']['WordList']['Word'] | |
text = '' | |
for word in word_list: | |
kana = word.get('Furigana') | |
if kana is None: | |
surface = word.get('Surface') | |
if surface is None:surface = ' ' | |
text += surface | |
continue | |
text += kana | |
return text | |
def to_kana_text(self, text): | |
self._ask_kana_xml(text) | |
return self._create_kana_text() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment