mw-dict
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -0,1 +1,2 @@ | ||
const url = require('url') | ||
const { CollegiateDictionary, WordNotFoundError } = require('../') | ||
@@ -11,2 +12,3 @@ | ||
'functional_label', | ||
'pronunciation', | ||
'definition' | ||
@@ -65,1 +67,19 @@ ]) | ||
}) | ||
describe('pronunciation', () => { | ||
it('return sound url', () => { | ||
return dict.lookup('TEST_SOUND').then(results => { | ||
let { pronunciation } = results[0] | ||
expect(pronunciation).toHaveLength(5) | ||
}) | ||
}) | ||
it('extract correct subdirectory for sound url', () => { | ||
return dict.lookup('TEST_SOUND').then(results => { | ||
let { pronunciation } = results[0] | ||
let subdirectories = pronunciation.map(p => { | ||
return url.parse(p).pathname.split('/')[2] | ||
}) | ||
expect(subdirectories).toEqual(['p', 'p', '00', 'gg', 'bix']) | ||
}) | ||
}) | ||
}) |
@@ -5,2 +5,3 @@ /* | ||
http://www.dictionaryapi.com/content/products/documentation/collegiate-tag-description.txt | ||
https://www.dictionaryapi.com/info/faq-audio-image.htm | ||
*/ | ||
@@ -14,2 +15,3 @@ | ||
'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/' | ||
const SOUND_URL = 'http://media.merriam-webster.com/soundc11' | ||
@@ -201,2 +203,20 @@ const slice = Array.prototype.slice | ||
function soundWalker(soundNode) { | ||
if (!soundNode) { | ||
return [] | ||
} | ||
let wav = soundNode.getElementsByTagName('wav') | ||
return slice.call(wav).map(w => { | ||
let filename = textContent(w) | ||
let matches = filename.match(/^[0-9]+|gg|bix/) | ||
let subdirectory | ||
if (matches) { | ||
subdirectory = matches[0] | ||
} else { | ||
subdirectory = filename[0] | ||
} | ||
return `${SOUND_URL}/${subdirectory}/${filename}` | ||
}) | ||
} | ||
class CollegiateDictionary { | ||
@@ -228,2 +248,3 @@ constructor(key) { | ||
functional_label: textContent(entry.getElementsByTagName('fl')[0]), | ||
pronunciation: soundWalker(entry.getElementsByTagName('sound')[0]), | ||
definition: defWalker(entry.getElementsByTagName('def')[0]) | ||
@@ -230,0 +251,0 @@ } |
{ | ||
"name": "mw-dict", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -6,2 +6,3 @@ # mw-dict | ||
* exports word senses with hierarchy (sense, subsense...) | ||
* outputs pronunciation url | ||
* cares about sense number, functional label, synonyms, verbal illustrations ... | ||
@@ -40,4 +41,5 @@ | ||
{ | ||
word: String, | ||
word: String, | ||
functional_label: String, | ||
pronunciation: String[], | ||
definition: Sense[] | ||
@@ -44,0 +46,0 @@ } |
104380
17
392
71