Skip to content

Commit 3d62c5f

Browse files
shranithgermanattanasio
authored andcommitted
feat: Add syntax Api to demo (#34)
Added new Syntax API Feature to the NLU Demo.
1 parent 5c9bcad commit 3d62c5f

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

views/Output/Output.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Keywords from './Keywords.jsx';
99
import Entities from './Entities.jsx';
1010
import Categories from './Categories.jsx';
1111
import Concept from './Concept.jsx';
12+
import Syntax from './Syntax.jsx';
1213
import SemanticRoles from './SemanticRoles.jsx';
1314
import { MAX_CONTENT_WIDTH } from '../utils/variables';
1415

@@ -79,6 +80,12 @@ function Output(props) {
7980
language={languages.getLanguageName(props.language)}
8081
/>
8182
</Pane>
83+
<Pane label="Syntax">
84+
<Syntax
85+
data={props.data.results.syntax.tokens}
86+
language={languages.getLanguageName(props.language)}
87+
/>
88+
</Pane>
8289
<Pane label="Semantic Roles">
8390
<SemanticRoles
8491
data={props.data.results.semantic_roles}

views/Output/Syntax.jsx

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import Waypoint from 'react-waypoint';
4+
import OutputTemplate from './OutputTemplate.jsx';
5+
import { breakpoint, breakpointsObj as bp } from '../utils/breakpoints';
6+
import { colors } from '../utils/colors';
7+
import { weight } from '../utils/typography';
8+
import Table from '../Table.jsx';
9+
10+
const tableTheme = {
11+
row_two: {
12+
maxWidth: '300px',
13+
[breakpoint(bp.SMALL)]: {
14+
maxWidth: '450px',
15+
borderBottom: `1px solid ${colors.WARM_GRAY}`,
16+
paddingBottom: '1rem',
17+
marginBottom: '1rem',
18+
},
19+
},
20+
row_header_two: {
21+
[breakpoint(bp.SMALL)]: {
22+
borderBottom: 'none',
23+
},
24+
},
25+
cell_header_two: {
26+
fontWeight: weight.MEDIUM,
27+
color: colors.COOL_GRAY,
28+
':first-child': {
29+
fontWeight: weight.MEDIUM,
30+
color: colors.COOL_GRAY,
31+
},
32+
':nth-of-type(2)': {
33+
fontWeight: weight.MEDIUM,
34+
color: colors.COOL_GRAY,
35+
},
36+
},
37+
cell_two: {
38+
':first-child': {
39+
color: colors.PRIMARY_LIGHT,
40+
fontWeight: weight.MEDIUM,
41+
},
42+
':nth-of-type(2)': {
43+
fontWeight: weight.MEDIUM,
44+
color: colors.DARK_GRAY,
45+
},
46+
':before': {
47+
width: '5rem',
48+
fontWeight: weight.MEDIUM,
49+
color: colors.COOL_GRAY,
50+
},
51+
},
52+
inner_two: {
53+
width: 'calc(100% - 5rem)',
54+
},
55+
};
56+
57+
export default React.createClass({
58+
displayName: 'Syntax',
59+
60+
propTypes: {
61+
data: PropTypes.arrayOf(PropTypes.shape({
62+
text: PropTypes.string,
63+
part_of_speech: PropTypes.string,
64+
lemma: PropTypes.string,
65+
})),
66+
language: PropTypes.string,
67+
},
68+
69+
getInitialState() {
70+
return {
71+
showJson: false,
72+
visibleItems: 10,
73+
};
74+
},
75+
76+
toggleJson() {
77+
this.setState({ showJson: !this.state.showJson });
78+
},
79+
80+
onWaypoint() {
81+
if (this.state.visibleItems < this.props.data.length) {
82+
setTimeout(() => {
83+
this.setState({
84+
visibleItems: this.state.visibleItems + 10,
85+
});
86+
}, 150);
87+
}
88+
},
89+
90+
render() {
91+
return (
92+
<div>
93+
<OutputTemplate
94+
description={<p className="base--p_small">Identify <a href="https://www.ibm.com/watson/developercloud/natural-language-understanding/api/v1/#syntax" target="_blank" rel="noopener noreferrer">tokens</a>, part of speech, sentence boundaries and lemmas in the text</p>}
95+
data={{ syntax: this.props.data }}
96+
showJson={this.state.showJson}
97+
onExitJson={this.toggleJson}
98+
onShowJson={this.toggleJson}
99+
>
100+
{console.log(this.props.data)}
101+
{this.props.data && this.props.data.length > 0 ? (
102+
<div>
103+
<Table
104+
columns={['Token', 'Part of Speech', 'Lemma']}
105+
theme={tableTheme}
106+
data={this.props.data.reduce((acc, item) => {
107+
acc.push({ Token: item.text,
108+
'Part of Speech': item.part_of_speech,
109+
Lemma: item.lemma });
110+
return acc;
111+
}, []).filter((val, i) => i <= this.state.visibleItems)}
112+
/>
113+
<Waypoint onEnter={this.onWaypoint} />
114+
</div>
115+
) : (
116+
<p>{`No Syntax results returned for ${this.props.language} input.`}</p>
117+
)}
118+
</OutputTemplate>
119+
</div>
120+
);
121+
},
122+
});

views/utils/request.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const FEATURES = {
99
emotion: {},
1010
sentiment: {},
1111
semantic_roles: {},
12+
syntax: {
13+
tokens: {
14+
lemma: true,
15+
part_of_speech: true,
16+
},
17+
sentences: true,
18+
},
1219
},
1320
};
1421

0 commit comments

Comments
 (0)