Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ふりがな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