|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +import Tokenizer from 'sentence-tokenizer'; |
| 4 | +import { ConceptNetwork, addLink, addNode } from '@ector/concept-network'; |
| 5 | +import { ConceptNetworkState, activate } from '@ector/state'; |
| 6 | + |
| 7 | +/** |
| 8 | + * @typedef {Object<string, any>} ECTOR |
| 9 | + * @property {string} [name] name of the bot |
| 10 | + * @property {string} [username] name of the user |
| 11 | + * @property {ConceptNetwork} [cn] |
| 12 | + * @property {Object<string, ConceptNetworkState>} [cns] One state per |
| 13 | + * username |
| 14 | + * @property {string} [lastSentenceLabel] Label of the last entry first sentence |
| 15 | + * @property {string[]} [lastTokenLabels] Labels of the last entry tokens |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * Add an entry to ector's model. |
| 20 | + * |
| 21 | + * @export |
| 22 | + * @param {ECTOR} ector |
| 23 | + * @param {string} entry |
| 24 | + * @returns {ECTOR|Error} |
| 25 | + */ |
3 | 26 | export function addEntry(ector, entry) {
|
| 27 | + if (!entry) return new Error('addEntry: No entry given!'); |
4 | 28 |
|
5 |
| -} |
| 29 | + const name = ector.name || 'ECTOR'; |
| 30 | + const username = ector.username || 'Guy'; |
| 31 | + let cn = ector.cn || {}; |
| 32 | + let cns = ector.cns || {}; |
| 33 | + let state = cns[username] || {}; |
| 34 | + cns = { ...cns, [username]: state }; |
6 | 35 |
|
7 |
| -export function generateResponse(ector) { |
| 36 | + const tokenizer = new Tokenizer(username, name); |
| 37 | + tokenizer.setEntry(entry); |
| 38 | + const sentences = tokenizer.getSentences(); |
| 39 | + let prevSentenceLabel; |
| 40 | + let lastSentenceLabel = ector.lastSentenceLabel; |
8 | 41 |
|
9 |
| -} |
| 42 | + const lastTokenLabels = sentences.reduce( |
| 43 | + (labels, sentence, sentenceIndex) => { |
| 44 | + const sentenceLabel = `s${sentence}`; |
| 45 | + cn = addNode(cn, sentenceLabel); |
| 46 | + if (prevSentenceLabel) { |
| 47 | + cn = addLink(cn, prevSentenceLabel, sentenceLabel); |
| 48 | + } |
| 49 | + if (sentenceIndex === 0) { |
| 50 | + lastSentenceLabel = sentenceLabel; |
| 51 | + } |
| 52 | + state = activate(state, sentenceLabel); |
10 | 53 |
|
11 |
| -export function getResponse(ector) { |
| 54 | + const tokens = tokenizer.getTokens(sentenceIndex); |
| 55 | + let prevTokenLabel; |
| 56 | + const tokenLabels = tokens.reduce( |
| 57 | + (tokenLabels, token, _tokenIndex) => { |
| 58 | + const tokenLabel = `w${token}`; |
| 59 | + cn = addNode(cn, tokenLabel); |
| 60 | + state = activate(state, tokenLabel); |
| 61 | + // TODO: add position in sentence (beg, mid, end) |
| 62 | + cn = addLink(cn, sentenceLabel, tokenLabel); |
| 63 | + if (prevTokenLabel) { |
| 64 | + cn = addLink(cn, prevTokenLabel, tokenLabel); |
| 65 | + } |
| 66 | + prevTokenLabel = tokenLabel; |
| 67 | + return [...tokenLabels, tokenLabel]; |
| 68 | + }, |
| 69 | + [], |
| 70 | + ); |
| 71 | + return [...labels, ...tokenLabels]; |
| 72 | + }, |
| 73 | + [], |
| 74 | + ); |
12 | 75 |
|
| 76 | + cns[username] = state; |
| 77 | + const newEctor = { |
| 78 | + ...ector, |
| 79 | + name, |
| 80 | + username, |
| 81 | + cn, |
| 82 | + cns, |
| 83 | + lastSentenceLabel, |
| 84 | + lastTokenLabels, |
| 85 | + }; |
| 86 | + return Object.freeze(newEctor); |
13 | 87 | }
|
| 88 | + |
| 89 | +export function generateResponse(ector) {} |
| 90 | + |
| 91 | +export function getResponse(ector) {} |
0 commit comments