-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from meiamsome/testing/word2vec
Word2Vec tests
- Loading branch information
Showing
5 changed files
with
172 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,96 @@ | ||
// import Word2Vec from './index'; | ||
|
||
// const URL = 'https://raw.githubusercontent.com/ml5js/ml5-examples/master/p5js/07_Word2Vec/data/wordvecs1000.json'; | ||
|
||
// describe('initialize word2vec', () => { | ||
// let word2vec; | ||
// beforeAll((done) => { | ||
// // word2vec = new Word2Vec(URL); | ||
// done(); | ||
// }); | ||
|
||
// // it('creates a new instance', (done) => { | ||
// // expect(word2vec).toEqual(jasmine.objectContaining({ | ||
// // ready: true, | ||
// // modelSize: 1, | ||
// // })); | ||
// // done(); | ||
// // }); | ||
// }); | ||
const { tf, word2vec } = ml5; | ||
|
||
const URL = 'https://raw.githubusercontent.com/ml5js/ml5-examples/master/p5js/Word2Vec/data/wordvecs1000.json'; | ||
|
||
describe('word2vec', () => { | ||
let word2vecInstance; | ||
let numTensorsBeforeAll; | ||
let numTensorsBeforeEach; | ||
beforeAll((done) => { | ||
numTensorsBeforeAll = tf.memory().numTensors; | ||
word2vecInstance = word2vec(URL, done); | ||
}); | ||
|
||
afterAll(() => { | ||
word2vecInstance.dispose(); | ||
let numTensorsAfterAll = tf.memory().numTensors; | ||
if(numTensorsBeforeAll !== numTensorsAfterAll) { | ||
throw new Error(`Leaking Tensors (${numTensorsAfterAll} vs ${numTensorsBeforeAll})`); | ||
} | ||
}); | ||
|
||
beforeEach(() => { | ||
numTensorsBeforeEach = tf.memory().numTensors; | ||
}); | ||
|
||
afterEach(() => { | ||
let numTensorsAfterEach = tf.memory().numTensors; | ||
if(numTensorsBeforeEach !== numTensorsAfterEach) { | ||
throw new Error(`Leaking Tensors (${numTensorsAfterEach} vs ${numTensorsBeforeEach})`); | ||
} | ||
}); | ||
|
||
it('creates a new instance', () => { | ||
expect(word2vecInstance).toEqual(jasmine.objectContaining({ | ||
ready: true, | ||
modelSize: 1, | ||
})); | ||
}); | ||
|
||
describe('getRandomWord', () => { | ||
it('returns a word', () => { | ||
let word = word2vecInstance.getRandomWord(); | ||
expect(typeof word).toEqual('string'); | ||
}); | ||
}); | ||
|
||
describe('nearest', () => { | ||
it('returns a sorted array of nearest words', () => { | ||
for(let i = 0; i < 100; i++) { | ||
let word = word2vecInstance.getRandomWord(); | ||
let nearest = word2vecInstance.nearest(word); | ||
let currentDistance = 0; | ||
for(let { word, distance: nextDistance } of nearest) { | ||
expect(typeof word).toEqual('string'); | ||
expect(nextDistance).toBeGreaterThan(currentDistance); | ||
currentDistance = nextDistance; | ||
} | ||
} | ||
}); | ||
|
||
it('returns a list of the right length', () => { | ||
for(let i = 0; i < 100; i++) { | ||
let word = word2vecInstance.getRandomWord(); | ||
let nearest = word2vecInstance.nearest(word, i); | ||
expect(nearest.length).toEqual(i); | ||
} | ||
}); | ||
}); | ||
|
||
describe('add', () => { | ||
it('returns a value', () => { | ||
let word1 = word2vecInstance.getRandomWord(); | ||
let word2 = word2vecInstance.getRandomWord(); | ||
let sum = word2vecInstance.subtract([word1, word2]); | ||
expect(sum[0].distance).toBeGreaterThan(0); | ||
}) | ||
}); | ||
|
||
describe('subtract', () => { | ||
it('returns a value', () => { | ||
let word1 = word2vecInstance.getRandomWord(); | ||
let word2 = word2vecInstance.getRandomWord(); | ||
let sum = word2vecInstance.subtract([word1, word2]); | ||
expect(sum[0].distance).toBeGreaterThan(0); | ||
}) | ||
}); | ||
|
||
describe('average', () => { | ||
it('returns a value', () => { | ||
let word1 = word2vecInstance.getRandomWord(); | ||
let word2 = word2vecInstance.getRandomWord(); | ||
let average = word2vecInstance.average([word1, word2]); | ||
expect(average[0].distance).toBeGreaterThan(0); | ||
}); | ||
}); | ||
}); |