Skip to content

Instantly share code, notes, and snippets.

@orj-takizawa
Last active January 7, 2025 03:05
Show Gist options
  • Save orj-takizawa/bd511117810bc2f2486c45842430a09e to your computer and use it in GitHub Desktop.
Save orj-takizawa/bd511117810bc2f2486c45842430a09e to your computer and use it in GitHub Desktop.
Google App Scriptとgooのふりがな化APIを使って、単語のフリガナを得る関数
/* 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