(text)
| 58 | } |
| 59 | |
| 60 | predict(text) { |
| 61 | // Convert to lower case and remove all punctuations. |
| 62 | const inputText = |
| 63 | text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(' '); |
| 64 | // Convert the words to a sequence of word indices. |
| 65 | const sequence = inputText.map(word => { |
| 66 | let wordIndex = this.wordIndex[word] + this.indexFrom; |
| 67 | if (wordIndex > this.vocabularySize) { |
| 68 | wordIndex = OOV_INDEX; |
| 69 | } |
| 70 | return wordIndex; |
| 71 | }); |
| 72 | // Perform truncation and padding. |
| 73 | const paddedSequence = padSequences([sequence], this.maxLen); |
| 74 | const input = tf.tensor2d(paddedSequence, [1, this.maxLen]); |
| 75 | |
| 76 | const beginMs = performance.now(); |
| 77 | const predictOut = this.model.predict(input); |
| 78 | const score = predictOut.dataSync()[0]; |
| 79 | predictOut.dispose(); |
| 80 | const endMs = performance.now(); |
| 81 | |
| 82 | return {score: score, elapsed: (endMs - beginMs)}; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | /** |
no test coverage detected