Skip to content

Commit 5d2402c

Browse files
committed
feat(core): Make addEntry test pass
1 parent 480815b commit 5d2402c

File tree

4 files changed

+144
-5
lines changed

4 files changed

+144
-5
lines changed

packages/core/__tests__/core.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
'use strict';
22

3-
import * as CNS from '../src/core';
3+
import * as ECTOR from '../src/core';
44

55
describe('@ector/core', () => {
6+
describe('addEntry', () => {
7+
it('should in the model', () => {
8+
expect(ECTOR.addEntry({
9+
username: 'Guy'
10+
}, `Hello.`))
11+
.toEqual({
12+
name: 'ECTOR',
13+
username: 'Guy',
14+
cn: {
15+
node: [{
16+
label: 'sHello.',
17+
occ: 1
18+
}, {
19+
label: 'wHello.',
20+
occ: 1
21+
}],
22+
link: [{ from: 0, to: 1, coOcc: 1 }]
23+
},
24+
cns: {
25+
Guy: {
26+
'sHello.': { value: 100 },
27+
'wHello.': { value: 100 }
28+
}
29+
},
30+
lastSentenceLabel: 'sHello.',
31+
lastTokenLabels: ['wHello.']
32+
})
33+
});
34+
});
635
});

packages/core/package-lock.json

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@
3636
"url": "https://github.com/parmentf/ector-monorepo/issues"
3737
},
3838
"dependencies": {
39+
"@ector/concept-network": "1.0.2",
40+
"@ector/state": "1.0.0",
41+
"sentence-tokenizer": "1.0.1"
3942
}
4043
}

packages/core/src/core.js

+82-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,91 @@
11
'use strict';
22

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+
*/
326
export function addEntry(ector, entry) {
27+
if (!entry) return new Error('addEntry: No entry given!');
428

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 };
635

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;
841

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);
1053

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+
);
1275

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);
1387
}
88+
89+
export function generateResponse(ector) {}
90+
91+
export function getResponse(ector) {}

0 commit comments

Comments
 (0)