Skip to content

Instantly share code, notes, and snippets.

@takanakahiko
Created August 22, 2019 18:34
Show Gist options
  • Save takanakahiko/966350de5d4cbfe436eb3fbf1472455f to your computer and use it in GitHub Desktop.
Save takanakahiko/966350de5d4cbfe436eb3fbf1472455f to your computer and use it in GitHub Desktop.
ふりがなGAS
var nsDefault = XmlService.getNamespace("urn:yahoo:jp:jlp:FuriganaService");
function parseXml() {
var url = 'http://jlp.yahooapis.jp/FuriganaService/V1/furigana';
var params = [
'appid=XXXXXXXXXXXXXXXXXXXX',
'sentence=' + encodeURIComponent('初音ミクさんを忘れないでください'),
'grade=1'
]
var xml = UrlFetchApp.fetch(url + '?' + params.join('&') ).getContentText();
var document = XmlService.parse(xml);
var root = document.getRootElement();
var words = root
.getChild('Result', nsDefault)
.getChild('WordList', nsDefault)
.getChildren('Word', nsDefault);
for (var i = 0; i < words.length; i++) {
var subWordList = words[i].getChild('SubWordList', nsDefault);
var furigana = words[i].getChild('Furigana', nsDefault);
if(subWordList){ // 入れ子の単語
var subwords = subWordList.getChildren('SubWord', nsDefault);
for(var j = 0; j < subwords.length; j++){
printWord(subwords[j])
}
}else if(furigana){ //入れ子じゃない単語(ふりがなつき)
printWord(words[i])
}else{ //入れ子じゃない単語(ふりがななし)
var surfaceValue = words[i].getChild('Surface', nsDefault).getValue()
Logger.log(surfaceValue);
}
}
}
function printWord(words){
var surface = words.getChild('Surface', nsDefault);
var furigana = words.getChild('Furigana', nsDefault);
var surfaceValue = surface.getValue() || '[empty]';
var furiganaValue = furigana.getValue() || '[empty]';
Logger.log(surfaceValue + ' : ' + furiganaValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment