Skip to content

Instantly share code, notes, and snippets.

@supersupermomonga
Created September 27, 2015 08:39
Show Gist options
  • Save supersupermomonga/b8acedbdfd970e91e181 to your computer and use it in GitHub Desktop.
Save supersupermomonga/b8acedbdfd970e91e181 to your computer and use it in GitHub Desktop.
/*
* DejizoJp - library to get a description from デ辞蔵Webサービス for Apps Script
* author: yoshiyuki hirano
*
* @doc http://dejizo.jp/dev/rest.html
*/
/**
* Get description by EJDict英和辞典 in Japanese from English
*
* @params {string} word
* @return {string} description
*/
function getDescriptionByEJdict(word) {
var d = new DejizoJp();
return d.getDescription(word, 0);
}
/**
* Get description by Edict和英辞典 in English from Japanese
*
* @params {string} word
* @return {string} description
*/
function getDescriptionByEdictJE(word) {
var d = new DejizoJp();
return d.getDescription(word, 1);
}
/**
* Get description by ウィキペディア日本語版
*
* @params {string} word
* @return {string} description
*/
function getDescriptionByWikipedia(word) {
var d = new DejizoJp();
return d.getDescription(word, 2);
}
/**
* constructor DejizoJp
*
* @return instance
*/
function DejizoJp() {
this.namespace = XmlService.getNamespace('http://btonic.est.co.jp/NetDic/NetDicV09');
this.searchItemsUrl = 'http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite';
this.getItemUrl = 'http://public.dejizo.jp/NetDicV09.asmx/GetDicItemLite';
this.dictionaries = {
// EJDict英和辞典
0: 'EJdict',
// Edict和英辞典
1: 'EdictJE',
// ウィキペディア日本語版
2: 'wpedia'
};
}
/**
* Search items from デ辞蔵Webサービス 検索メソッド
* @doc http://dejizo.jp/dev/rest.html
*
* @params {string} word
* @params {integer} dictionaryId as enum
* @return {object}
*/
DejizoJp.prototype.searchItems = function(word, dictionaryId) {
if (!word) throw new Error('Missing args');
var url = this.searchItemsUrl+'?Dic='+this.dictionaries[dictionaryId]+'&Word='+word+'&Scope=HEADWORD&Match=STARTWITH&Merge=OR&Prof=XHTML&PageSize=50&PageIndex=0',
xml = UrlFetchApp.fetch(url).getContentText(),
doc = XmlService.parse(xml),
root = doc.getRootElement(),
items = root.getChild('TitleList', this.namespace).getChildren(),
result = {};
for(var i = 0; i < items.length; i++) {
var ItemID = items[i].getChild('ItemID', this.namespace).getValue(),
Title = this.strip( items[i].getChild('Title', this.namespace).getValue() );
result[Title] = { ItemID: ItemID };
}
return result;
};
/**
* Get Item By ItemId from デ辞蔵Webサービス 内容取得メソッド
* @doc http://dejizo.jp/dev/rest.html
*
* @params {string} word
* @params {integer} dictionaryId as enum
* @return {object}
*/
DejizoJp.prototype.getItem = function(itemId, dictionaryId) {
if (!itemId) throw new Error('Not Found');
var url = this.getItemUrl+'?Dic='+this.dictionaries[dictionaryId]+'&Item='+itemId+'&Loc=&Prof=XHTML',
xml = UrlFetchApp.fetch(url).getContentText(),
doc = XmlService.parse(xml),
root = doc.getRootElement(),
title = this.strip( root.getChild('Head', this.namespace).getValue() ),
content = this.strip( root.getChild('Body', this.namespace).getValue() );
return { Title: title, Content: content };
}
/**
* Get description of word from デ辞蔵Webサービス
* @doc http://dejizo.jp/dev/rest.html
*
* @params {string} word
* @params {integer} dictionaryId as enum
* @return {string} description
*/
DejizoJp.prototype.getDescription = function(word, dictionaryId) {
try {
if (typeof word != 'string') throw new Error('Invalid Value');
var result = this.searchItems(word, dictionaryId);
if (result[word] != null) {
var item = this.getItem(result[word].ItemID, dictionaryId),
desc = item.Content;
return desc;
} else {
return 'Not Exact: Perhaps...' + Object.keys(result).join(',');
}
} catch(ex) {
return ex.toString();
}
}
/**
* Strip \t, \r, \n, \s
*
* @params {string}
* @return {string}
*/
DejizoJp.prototype.strip = function(str) {
return str.replace(/\t|\r|\n/g, '').trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment