Last active
January 7, 2025 03:05
-
-
Save orj-takizawa/bd511117810bc2f2486c45842430a09e to your computer and use it in GitHub Desktop.
Google App Scriptとgooのふりがな化APIを使って、単語のフリガナを得る関数
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
/* Google App Scriptとgooのふりがな化APIを使って、単語のフリガナを得る関数 | |
ふりがな化API(https://labs.goo.ne.jp/api/jp/hiragana-translation/) */ | |
function getPhonetic(term, app_id="<add your app_id>") { | |
const alnum = /^[a-zA-Z\(\)\.\$\*\+\-\#\{\}\[\] ]+$/; | |
if (term.length==0){ | |
return ''; | |
} else if (term.match(alnum)==null){ | |
const data = { | |
"app_id": app_id, | |
"sentence": term, | |
"output_type": "hiragana" | |
}; | |
const option = { | |
method: 'post', | |
contentType: 'application/json', | |
payload: JSON.stringify(data), | |
}; | |
const resp = UrlFetchApp.fetch('https://labs.goo.ne.jp/api/hiragana', option); | |
const json = resp.getContentText() | |
const jsonData = JSON.parse(json); | |
return jsonData['converted'].replace(' ', '') | |
} else { | |
return term; | |
} | |
} | |
function test_getPhonetic(term='同じ話'){ | |
console.log(getPhonetic(term)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment