@sc-voice/tools 2.22.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/text/word-vector.mjs +40 -5
package/package.json
CHANGED
package/src/text/word-vector.mjs
CHANGED
|
@@ -59,7 +59,7 @@ export class WordVector extends Object {
|
|
|
59
59
|
return sv.join(',');
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
norm() {
|
|
62
|
+
norm() { // L2 norm
|
|
63
63
|
let keys = Object.keys(this);
|
|
64
64
|
if (keys.length === 0) {
|
|
65
65
|
return 0;
|
|
@@ -114,17 +114,27 @@ export class WordVector extends Object {
|
|
|
114
114
|
}, this);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
hadamardL1(vec2 = {}) {
|
|
118
|
+
// L1-norm of Hadamard product shows how
|
|
119
|
+
// the cosine similarity score is apportioned
|
|
118
120
|
let keys = Object.keys(this);
|
|
119
|
-
|
|
121
|
+
let n = 0;
|
|
122
|
+
let hadamard = keys.reduce((a, k) => {
|
|
120
123
|
let v1 = this[k];
|
|
121
124
|
let v2 = vec2[k] || 0;
|
|
122
125
|
if (v1 && v2) {
|
|
123
126
|
a[k] = v1 * v2;
|
|
127
|
+
n++;
|
|
124
128
|
}
|
|
125
129
|
|
|
126
130
|
return a;
|
|
127
131
|
}, new WordVector());
|
|
132
|
+
|
|
133
|
+
if (n === 0) {
|
|
134
|
+
return hadamard; // empty vector
|
|
135
|
+
}
|
|
136
|
+
let n12 = this.norm() * vec2.norm();
|
|
137
|
+
return hadamard.scale(1/n12);
|
|
128
138
|
}
|
|
129
139
|
|
|
130
140
|
similar(vec2) {
|
|
@@ -135,8 +145,8 @@ export class WordVector extends Object {
|
|
|
135
145
|
let d = this.dot(vec2);
|
|
136
146
|
let norm1 = this.norm();
|
|
137
147
|
let norm2 = vec2.norm();
|
|
138
|
-
let
|
|
139
|
-
return
|
|
148
|
+
let n12 = norm1 * norm2;
|
|
149
|
+
return n12 ? d / n12 : 0;
|
|
140
150
|
}
|
|
141
151
|
|
|
142
152
|
oneHot() {
|
|
@@ -147,4 +157,29 @@ export class WordVector extends Object {
|
|
|
147
157
|
return a;
|
|
148
158
|
}, new WordVector());
|
|
149
159
|
}
|
|
160
|
+
|
|
161
|
+
andOneHot(vec2) {
|
|
162
|
+
return Object.keys(this).reduce((a, k) => {
|
|
163
|
+
if (this[k] && vec2[k]) {
|
|
164
|
+
a[k] = 1;
|
|
165
|
+
}
|
|
166
|
+
return a;
|
|
167
|
+
}, new WordVector());
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
orOneHot(vec2) {
|
|
171
|
+
let result = Object.keys(this).reduce((a, k) => {
|
|
172
|
+
if (this[k] || vec2[k]) {
|
|
173
|
+
a[k] = 1;
|
|
174
|
+
}
|
|
175
|
+
return a;
|
|
176
|
+
}, new WordVector());
|
|
177
|
+
return Object.keys(vec2).reduce((a, k) => {
|
|
178
|
+
if (this[k] || vec2[k]) {
|
|
179
|
+
a[k] = 1;
|
|
180
|
+
}
|
|
181
|
+
return a;
|
|
182
|
+
}, result);
|
|
183
|
+
}
|
|
184
|
+
|
|
150
185
|
} // WordVector
|