-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSentimentBot.jsx
178 lines (154 loc) · 6.08 KB
/
SentimentBot.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React from 'react';
// import ToneAnalyzerV3 from 'watson-developer-cloud/tone-analyzer/v3'
export default class SentimentBot extends React.Component {
// static propTypes = {
// name: PropTypes.string.isRequired, // this is passed from the Rails view
// };
/**
* @param props - Comes from your rails view.
* @param _railsContext - Comes from React on Rails
*/
constructor(props, _railsContext) {
super(props);
this.state = {
// steps: this.props.steps,
// response: this.props.response
};
// How to set initial state in ES6 class syntax
// https://facebook.github.io/react/docs/reusable-components.html#es6-classes
// this.state = { name: this.props.name };
}
// componentDidMount() {
//
// }
checktone(message) {
// console.log('Messagse incoming:', message)
if(message) {
message = encodeURIComponent(message)
// let message = encodeURIComponent(document.getElementById('message').value)
// fetch(`http://omdbapi.com/?s=${message}`)
fetch(`https://watson-api-explorer.mybluemix.net/tone-analyzer/api/v3/tone?version=2016-05-19&text=${message}`)
.then((response) => {
// console.log('first fetch response')
return response.json()
})
.then((json) => {
// console.log('second fetch json')
// console.log(json)
// console.log(input)
var tonesArr = []
// var res = JSON.parse(input)
// var res = json
// console.log(res.document_tone.tone_categories)
json.document_tone.tone_categories.map( (tones) => {
// console.log(val)
tones['tones'].map((tone) => {
// if (tone['tone_name'] === 'Anger') {
// angerScore = tone['score']
// }
var toneObj = {}
toneObj.tone_name = tone['tone_name']
toneObj.score = tone['score'] // + Math.random()*0.05
tonesArr.push(toneObj)
})
})
this.renderGraph(tonesArr, this.props.isadmin)
// this.setState({
// searchResult: json.Search.map((movies) => movies.Title ),
// })
})
.catch((err) => {
console.log(err)
})
}
}
renderGraph(data, is_admin) { // seth
if(is_admin) { // run graph only for admin
var svg = d3.select("#sentiment-graph"),
margin = {top: 20, right: 20, bottom: 100, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1);
var y = d3.scaleLinear().rangeRound([height, 0]);
// Define the axes
// var xAxis = d3.svg.axis().scale(x)
// .orient("bottom").ticks(5);
// var yAxis = d3.svg.axis().scale(y)
// .orient("left").ticks(5);
if(document.getElementsByTagName("rect").length === 0) {
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(data.map(function(d) { return d.tone_name; }));
y.domain([0, d3.max(data, function(d) { return d.score; })]);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)" );
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "%"))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Score");
g.selectAll(".svg")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.tone_name); })
.attr("y", function(d) { return y(d.score); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d.score); });
} else {
// udpate the graph
x.domain(data.map(function(d) { return d.tone_name; }));
y.domain([0, d3.max(data, function(d) { return d.score; })]);
svg.selectAll("rect")
.data(data)
// .enter()
// .append("rect")
// .attr("class", "bar")
.transition()
.duration(1000)
.attr("x", function(d) { return x(d.tone_name); })
.attr("y", function(d) { return y(d.score); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d.score); })
// y axis
svg.select(".axis--y")
.transition()
.duration(1000)
.call(d3.axisLeft(y).ticks(10, "%"))
}
} // run graph only for admin
this.props.handleLoadingDone(data) // seth
}
componentWillReceiveProps(nextProps) {
// console.log("called receive props!")
// console.log(this.props.opened)
// console.log(nextProps.opened)
// console.log(this.props.opened !== nextProps.opened)
if (this.props.response !== nextProps.response && this.props.toneArr === nextProps.toneArr) {
this.checktone(nextProps.response)
}
}
render() {
return (
<div className="graph">
{/* <input type="text" placeholder="type something" id="message" /> */}
{/* <button onClick={ (e) => this.checktone(e, this.props.response)}>Check Tone!</button> */}
{/* <div> */}
<svg id="sentiment-graph" width="480" height="300"></svg>
{/* </div> */}
{/* write logic for sentiment bot here */}
</div>
);
}
}