Skip to content

Commit 787e58b

Browse files
authored
It's alive (#2)
* It is alive * Trigger build * Remove prepublish script
1 parent 46ca762 commit 787e58b

19 files changed

+2344
-788
lines changed

.travis.yml

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
language: node_js
2-
node_js:
3-
- 'node'
4-
- '6'
5-
addons:
6-
code_climate:
7-
repo_token: 'your repo token'
8-
notifications:
9-
email: false
10-
before_script:
11-
- npm install -g codeclimate-test-reporter
12-
after_script:
13-
- codeclimate-test-reporter < coverage/lcov.info
2+
node_js: 'node'
3+
# addons:
4+
# code_climate:
5+
# repo_token: 'your repo token'
6+
# notifications:
7+
# email: false
8+
# before_script:
9+
# - npm install -g codeclimate-test-reporter
10+
# after_script:
11+
# - codeclimate-test-reporter < coverage/lcov.info

index.html

+37-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,45 @@
88
</head>
99
<body>
1010
<div id="content" class="full"></div>
11+
<script src="http://localhost/development/mysam/mysam/dist/mysam.js"></script>
1112
<script src="./dist/mysam-ui.js"></script>
1213
<script>
13-
mysam.ui({
14-
configure(fn) {
15-
fn.call(this);
16-
return this;
17-
}
18-
}, document.getElementById('content'));
14+
const app = window.sam = mysam();
15+
const sam = MysamUi(app, document.getElementById('content'));
16+
17+
// localStorage.clear();
18+
19+
// sam.service('classify').on('trained', data => {
20+
// sam.service('classify').create({
21+
// text: 'Yo, the name is test'
22+
// }).then(res => {
23+
// console.log(res);
24+
// });
25+
26+
// sam.service('classify').create({
27+
// text: 'How\'s the weather in Berlin?'
28+
// }).then(res => {
29+
// console.log(res);
30+
// });
31+
// });
32+
33+
// Promise.all([
34+
// sam.service('trainings').create({
35+
// text: 'hi my name is david',
36+
// action: {
37+
// type: 'hi',
38+
// tags: { to: [ 4,4 ] }
39+
// }
40+
// }),
41+
42+
// sam.service('trainings').create({
43+
// text: 'what is the weather in vancouver',
44+
// action: {
45+
// type: 'weather',
46+
// tags: { location: [ 5, 5 ] }
47+
// }
48+
// })
49+
// ]);
1950
</script>
2051
</body>
2152
</html>

lib/components/base.js

-5
This file was deleted.

lib/components/learner.js

+23-32
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
11
import React from 'react';
22
import map from 'lodash/map';
3-
import assign from 'lodash/assign';
4-
import cloneDeep from 'lodash/cloneDeep';
53
import omit from 'lodash/omit';
64
import isEqual from 'lodash/isEqual';
75
import _debug from 'debug';
86

9-
import Component from './base';
10-
import app from '../app';
117
import Typer from './typer';
128
import Tagger from './tagger';
139

1410
const debug = _debug('mysam-frontend:components/learner');
1511

16-
class Learner extends Component {
17-
constructor(props) {
12+
class Learner extends React.Component {
13+
constructor (props) {
1814
super(props);
1915
this.submitForm = this.submitForm.bind(this);
2016
}
2117

22-
componentWillMount() {
23-
app.on('confirm', this.submitForm);
18+
get app () {
19+
return this.props.sam;
2420
}
2521

26-
componentWillUnmount() {
27-
app.removeListener('confirm', this.submitForm);
28-
}
29-
30-
submitForm(ev) {
31-
if(ev) {
22+
submitForm (ev) {
23+
if (ev) {
3224
ev.preventDefault();
3325
}
3426

35-
const base = typeof this.learner.onSubmit === 'function' ?
36-
this.learner.onSubmit(this.form) : {};
27+
const base = typeof this.learner.onSubmit === 'function'
28+
? this.learner.onSubmit(this.form) : {};
3729
let action = Object.assign({}, this.state.action, base);
3830

39-
if(isEqual(action, this.state.action) && action._id) {
31+
if (isEqual(action, this.state.action) && action._id) {
4032
action = action._id;
4133
} else {
4234
action = omit(action, '_id');
@@ -48,51 +40,50 @@ class Learner extends Component {
4840

4941
debug('Creating training', data);
5042

51-
app.service('trainings').create(data).then(training => {
43+
this.app.service('trainings').create(data).then(training => {
5244
debug('Sending classification from new training', training);
53-
app.processClassification(training.classification);
45+
this.app.processClassification(training.classification);
5446
});
5547
}
5648

57-
render() {
58-
const { classification, action } = this.state;
59-
49+
render () {
50+
const { classification, action } = this.app;
6051
let text = 'There is nothing here for me to learn';
61-
62-
if(!classification.action || !action.type) {
52+
53+
if (!classification || !classification.action || !action.type) {
6354
return <h1>
6455
<Typer key={text}>{text}</Typer>
6556
</h1>;
6657
}
6758

68-
const learner = this.learner = app.learners[action.type];
69-
const form = typeof learner.form === 'function' ? learner.form : function() {};
59+
const learner = this.learner = this.app.learners[action.type];
60+
const form = typeof learner.form === 'function' ? learner.form : function () {};
7061

7162
const selectAction = ev => {
72-
app.state.action = {
63+
this.app.action = {
7364
type: ev.target.value,
7465
text: action.text
7566
};
76-
}
67+
};
7768

78-
if(classification.confidence > 0.3) {
69+
if (classification.confidence > 0.3) {
7970
text = 'Is this what you would like me to do?';
8071
} else {
8172
text = 'I am not sure what to do. Tell me?';
8273
}
8374

84-
return <form id="learner" onSubmit={this.submitForm} ref={el => this.form = el}>
75+
return <form id='learner' onSubmit={this.submitForm} ref={el => (this.form = el)}>
8576
<Tagger action={action} classification={classification} learner={learner} />
8677
<h1><Typer key={text}>{text}</Typer></h1>
8778
<select onChange={selectAction} value={action.type}>
88-
{map(app.learners, (learner, name) =>
79+
{map(this.app.learners, (learner, name) =>
8980
<option key={name} value={name}>
9081
{learner.description}
9182
</option>
9283
)}
9384
</select>
9485
{form(classification)}
95-
</form>
86+
</form>;
9687
}
9788
}
9889

lib/components/main.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
2-
// import Toolbar from './toolbar';
2+
import { observer } from 'mobx-react';
33

4-
export default function({ app }) {
5-
// <Toolbar recognizer={app.service('recognizer')}
6-
// listening={app.listening}
7-
// transcript={app.transcript} />
8-
return <div className="full">
9-
<div id="main"></div>
4+
import Toolbar from './toolbar';
5+
6+
export default observer(({ sam }) => {
7+
return <div className='full'>
8+
<Toolbar recognizer={sam.recognizer} />
9+
<div id='main' />
1010
<footer />
1111
</div>;
12-
}
12+
});

lib/components/tagger.js

+20-28
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,42 @@
11
import React from 'react';
2-
import each from 'lodash/each';
3-
import map from 'lodash/map';
4-
import assign from 'lodash/assign';
52
import cloneDeep from 'lodash/cloneDeep';
6-
import omit from 'lodash/omit';
73

8-
import Component from './base';
9-
import app from '../app';
10-
import Typer from './typer';
11-
12-
class Tagger extends Component {
13-
get selectedTag() {
14-
if(this.state.selectedTag) {
4+
class Tagger extends React.Component {
5+
get selectedTag () {
6+
if (this.state.selectedTag) {
157
return this.state.selectedTag;
168
}
179

18-
if(this.props.learner && this.props.learner.tags) {
10+
if (this.props.learner && this.props.learner.tags) {
1911
return this.props.learner.tags[0];
2012
}
2113

2214
return null;
2315
}
2416

25-
setBound(index) {
17+
setBound (index) {
2618
const selectedTag = this.selectedTag;
2719

28-
if(!this.props.learner.tags || !this.props.learner.tags.length || !selectedTag) {
20+
if (!this.props.learner.tags || !this.props.learner.tags.length || !selectedTag) {
2921
return;
3022
}
3123

3224
const action = cloneDeep(this.props.action);
33-
34-
if(!action.tags) {
25+
26+
if (!action.tags) {
3527
action.tags = {};
3628
}
3729

3830
let bounds = action.tags[selectedTag];
3931

40-
if(!bounds) {
32+
if (!bounds) {
4133
bounds = [ index, index ];
4234
} else {
4335
let [ start, end ] = bounds;
4436

45-
if(index > end) {
37+
if (index > end) {
4638
end = index;
47-
} else if(index < start) {
39+
} else if (index < start) {
4840
start = index;
4941
} else {
5042
start = end = index;
@@ -55,10 +47,10 @@ class Tagger extends Component {
5547

5648
action.tags[selectedTag] = bounds;
5749

58-
app.state.action = action;
50+
this.props.sam.action = action;
5951
}
6052

61-
render() {
53+
render () {
6254
const { action, learner, classification } = this.props;
6355
const words = this.props.classification.tokens;
6456
const renderWord = (word, index) => {
@@ -67,20 +59,20 @@ class Tagger extends Component {
6759

6860
return <span key={`${classification._id}-${index}`} className={className}
6961
onClick={() => this.setBound(index)}>
70-
{word}
71-
</span>
72-
}
62+
{word}
63+
</span>;
64+
};
7365
const renderTag = tag => <span className={tag === this.selectedTag ? 'tagged' : ''}
7466
key={tag} onClick={() => this.setState({ selectedTag: tag })}>
75-
{tag}
76-
</span>
67+
{tag}
68+
</span>;
7769

7870
const tags = learner.tags || [];
7971

80-
return <div id="tagger">
72+
return <div id='tagger'>
8173
<h2>{words.map(renderWord)}</h2>
8274
{tags.length > 1 ? <div><strong>Tags:</strong> {tags.map(renderTag)}</div> : null}
83-
</div>
75+
</div>;
8476
}
8577
}
8678

lib/components/toolbar.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import React from 'react';
2-
import Component from './base';
2+
import { observer } from 'mobx-react';
33

4-
class Toolbar extends Component {
5-
render() {
6-
const { listening, transcript } = this.state;
7-
const className = `icon-base ${listening ? 'animated pulse infinite icon-mysam-icon' : 'icon-microphone'}`;
8-
9-
return <header>
10-
<div className="padded">
11-
<div className="pull-right">
12-
<small>{transcript.text}</small>
4+
export default observer(({ recognizer }) => {
5+
const { listening, transcript } = recognizer;
6+
const className = `icon-base ${listening ? 'animated pulse infinite icon-mysam-icon' : 'icon-microphone'}`;
137

14-
<button onClick={() => this.props.recognizer.toggle()}
15-
className={className}>
16-
</button>
17-
</div>
18-
</div>
19-
</header>
20-
}
21-
}
8+
return <header>
9+
<div className='padded'>
10+
<div className='pull-right'>
11+
<small>{transcript.text}</small>
2212

23-
export default Toolbar;
13+
<button onClick={() => recognizer.toggle()}
14+
className={className} />
15+
</div>
16+
</div>
17+
</header>;
18+
});

0 commit comments

Comments
 (0)