@@ -26,33 +26,39 @@ class Word2Vec {
26
26
} ;
27
27
28
28
this . ready = callCallback ( loadModel ( model ) , callback ) ;
29
+ this . then = this . ready . then . bind ( this . ready ) ;
29
30
}
30
31
31
- add ( inputs , max = 1 ) {
32
+ async add ( inputs , max = 1 ) {
33
+ await this . ready ;
32
34
const sum = Word2Vec . addOrSubtract ( this . model , inputs , 'ADD' ) ;
33
35
return Word2Vec . nearest ( this . model , sum , inputs . length , inputs . length + max ) ;
34
36
}
35
37
36
- subtract ( inputs , max = 1 ) {
38
+ async subtract ( inputs , max = 1 ) {
39
+ await this . ready ;
37
40
const subtraction = Word2Vec . addOrSubtract ( this . model , inputs , 'SUBTRACT' ) ;
38
41
return Word2Vec . nearest ( this . model , subtraction , inputs . length , inputs . length + max ) ;
39
42
}
40
43
41
- average ( inputs , max = 1 ) {
44
+ async average ( inputs , max = 1 ) {
45
+ await this . ready ;
42
46
const sum = Word2Vec . addOrSubtract ( this . model , inputs , 'ADD' ) ;
43
47
const avg = tf . div ( sum , tf . tensor ( inputs . length ) ) ;
44
48
return Word2Vec . nearest ( this . model , avg , inputs . length , inputs . length + max ) ;
45
49
}
46
50
47
- nearest ( input , max = 10 ) {
51
+ async nearest ( input , max = 10 ) {
52
+ await this . ready ;
48
53
const vector = this . model [ input ] ;
49
54
if ( ! vector ) {
50
55
return null ;
51
56
}
52
57
return Word2Vec . nearest ( this . model , vector , 1 , max + 1 ) ;
53
58
}
54
59
55
- getRandomWord ( ) {
60
+ async getRandomWord ( ) {
61
+ await this . ready ;
56
62
const words = Object . keys ( this . model ) ;
57
63
return words [ Math . floor ( Math . random ( ) * words . length ) ] ;
58
64
}
@@ -100,8 +106,7 @@ class Word2Vec {
100
106
}
101
107
102
108
const word2vec = ( model , cb ) => {
103
- const instance = new Word2Vec ( model , cb ) ;
104
- return cb ? instance : instance . ready ;
109
+ return new Word2Vec ( model , cb ) ;
105
110
} ;
106
111
107
112
export default word2vec ;
0 commit comments