|
1 |
| -// import Word2Vec from './index'; |
2 |
| - |
3 |
| -// const URL = 'https://raw.githubusercontent.com/ml5js/ml5-examples/master/p5js/07_Word2Vec/data/wordvecs1000.json'; |
4 |
| - |
5 |
| -// describe('initialize word2vec', () => { |
6 |
| -// let word2vec; |
7 |
| -// beforeAll((done) => { |
8 |
| -// // word2vec = new Word2Vec(URL); |
9 |
| -// done(); |
10 |
| -// }); |
11 |
| - |
12 |
| -// // it('creates a new instance', (done) => { |
13 |
| -// // expect(word2vec).toEqual(jasmine.objectContaining({ |
14 |
| -// // ready: true, |
15 |
| -// // modelSize: 1, |
16 |
| -// // })); |
17 |
| -// // done(); |
18 |
| -// // }); |
19 |
| -// }); |
| 1 | +const { tf, word2vec } = ml5; |
| 2 | + |
| 3 | +const URL = 'https://raw.githubusercontent.com/ml5js/ml5-examples/master/p5js/Word2Vec/data/wordvecs1000.json'; |
| 4 | + |
| 5 | +describe('word2vec', () => { |
| 6 | + let word2vecInstance; |
| 7 | + let numTensorsBeforeAll; |
| 8 | + let numTensorsBeforeEach; |
| 9 | + beforeAll((done) => { |
| 10 | + numTensorsBeforeAll = tf.memory().numTensors; |
| 11 | + word2vecInstance = word2vec(URL, done); |
| 12 | + }); |
| 13 | + |
| 14 | + afterAll(() => { |
| 15 | + word2vecInstance.dispose(); |
| 16 | + let numTensorsAfterAll = tf.memory().numTensors; |
| 17 | + if(numTensorsBeforeAll !== numTensorsAfterAll) { |
| 18 | + throw new Error(`Leaking Tensors (${numTensorsAfterAll} vs ${numTensorsBeforeAll})`); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + numTensorsBeforeEach = tf.memory().numTensors; |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + let numTensorsAfterEach = tf.memory().numTensors; |
| 28 | + if(numTensorsBeforeEach !== numTensorsAfterEach) { |
| 29 | + throw new Error(`Leaking Tensors (${numTensorsAfterEach} vs ${numTensorsBeforeEach})`); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it('creates a new instance', () => { |
| 34 | + expect(word2vecInstance).toEqual(jasmine.objectContaining({ |
| 35 | + ready: true, |
| 36 | + modelSize: 1, |
| 37 | + })); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('getRandomWord', () => { |
| 41 | + it('returns a word', () => { |
| 42 | + let word = word2vecInstance.getRandomWord(); |
| 43 | + expect(typeof word).toEqual('string'); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('nearest', () => { |
| 48 | + it('returns a sorted array of nearest words', () => { |
| 49 | + for(let i = 0; i < 100; i++) { |
| 50 | + let word = word2vecInstance.getRandomWord(); |
| 51 | + let nearest = word2vecInstance.nearest(word); |
| 52 | + let currentDistance = 0; |
| 53 | + for(let { word, distance: nextDistance } of nearest) { |
| 54 | + expect(typeof word).toEqual('string'); |
| 55 | + expect(nextDistance).toBeGreaterThan(currentDistance); |
| 56 | + currentDistance = nextDistance; |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns a list of the right length', () => { |
| 62 | + for(let i = 0; i < 100; i++) { |
| 63 | + let word = word2vecInstance.getRandomWord(); |
| 64 | + let nearest = word2vecInstance.nearest(word, i); |
| 65 | + expect(nearest.length).toEqual(i); |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('add', () => { |
| 71 | + it('returns a value', () => { |
| 72 | + let word1 = word2vecInstance.getRandomWord(); |
| 73 | + let word2 = word2vecInstance.getRandomWord(); |
| 74 | + let sum = word2vecInstance.subtract([word1, word2]); |
| 75 | + expect(sum[0].distance).toBeGreaterThan(0); |
| 76 | + }) |
| 77 | + }); |
| 78 | + |
| 79 | + describe('subtract', () => { |
| 80 | + it('returns a value', () => { |
| 81 | + let word1 = word2vecInstance.getRandomWord(); |
| 82 | + let word2 = word2vecInstance.getRandomWord(); |
| 83 | + let sum = word2vecInstance.subtract([word1, word2]); |
| 84 | + expect(sum[0].distance).toBeGreaterThan(0); |
| 85 | + }) |
| 86 | + }); |
| 87 | + |
| 88 | + describe('average', () => { |
| 89 | + it('returns a value', () => { |
| 90 | + let word1 = word2vecInstance.getRandomWord(); |
| 91 | + let word2 = word2vecInstance.getRandomWord(); |
| 92 | + let average = word2vecInstance.average([word1, word2]); |
| 93 | + expect(average[0].distance).toBeGreaterThan(0); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments