Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish project #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
36,599 changes: 36,599 additions & 0 deletions sentimentAnalyzeClient/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sentimentAnalyzeClient/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Sentiment Analyzer</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
162 changes: 87 additions & 75 deletions sentimentAnalyzeClient/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,100 @@ import EmotionTable from './EmotionTable.js';
import React from 'react';

class App extends React.Component {
/*
We are setting the component as a state named innercomp.
When this state is accessed, the HTML that is set as the
value of the state, will be returned. The initial input mode
is set to text
*/
state = {innercomp:<textarea rows="4" cols="50" id="textinput"/>,
mode: "text",
sentimentOutput:[],
sentiment:true
}

/*
This method returns the component based on what the input mode is.
If the requested input mode is "text" it returns a textbox with 4 rows.
If the requested input mode is "url" it returns a textbox with 1 row.
*/

renderOutput = (input_mode)=>{
let rows = 1
let mode = "url"
//If the input mode is text make it 4 lines
if(input_mode === "text"){
mode = "text"
rows = 4
/*
We are setting the component as a state named innercomp.
When this state is accessed, the HTML that is set as the
value of the state, will be returned. The initial input mode
is set to text
*/
state = {
innercomp: <textarea rows="4" cols="50" id="textinput" />,
mode: "text",
sentimentOutput: [],
sentiment: true
}
this.setState({innercomp:<textarea rows={rows} cols="50" id="textinput"/>,
mode: mode,
sentimentOutput:[],
sentiment:true
});
}

sendForSentimentAnalysis = () => {
this.setState({sentiment:true});
let url = ".";
let mode = this.state.mode
url = url+"/" + mode + "/sentiment?"+ mode + "="+document.getElementById("textinput").value;

fetch(url).then((response)=>{
response.json().then((data)=>{
this.setState({sentimentOutput:data.label});
let output = data.label;
let color = "white"
switch(output) {
case "positive": color = "black";break;
case "negative": color = "black";break;
default: color = "black";
/*
This method returns the component based on what the input mode is.
If the requested input mode is "text" it returns a textbox with 4 rows.
If the requested input mode is "url" it returns a textbox with 1 row.
*/

renderOutput = (input_mode) => {
let rows = 1
let mode = "url"
//If the input mode is text make it 4 lines
if (input_mode === "text") {
mode = "text"
rows = 4
}
output = <div style={{color:color,fontSize:20}}>{output}</div>
this.setState({sentimentOutput:output});
})});
}
this.setState({
innercomp: <textarea rows={rows} cols="50" id="textinput" />,
mode: mode,
sentimentOutput: [],
sentiment: true
});
}

sendForSentimentAnalysis = () => {
this.setState({ sentiment: true });
let url = ".";
let mode = this.state.mode
url = url + "/" + mode + "/sentiment?" + mode + "=" + document.getElementById("textinput").value;

sendForEmotionAnalysis = () => {
fetch(url).then((response) => {
response.json().then((data) => {
this.setState({ sentimentOutput: data.label });
let output = data.label;
let color = "white"
switch (output) {
case "positive":
color = "green";
break;
case "negative":
color = "red";
break;
case "neutral":
color = "yellow";
break;
default:
color = "black";
}
output = <div style={{ color: color, fontSize: 20 }}>{output}</div>
this.setState({ sentimentOutput: output });
})
});
}

sendForEmotionAnalysis = () => {

this.setState({sentiment:false});
let url = ".";
let mode = this.state.mode
url = url+"/" + mode + "/emotion?"+ mode + "="+document.getElementById("textinput").value;
this.setState({ sentiment: false });
let url = ".";
let mode = this.state.mode
url = url + "/" + mode + "/emotion?" + mode + "=" + document.getElementById("textinput").value;

fetch(url).then((response) => {
response.json().then((data) => {
this.setState({ sentimentOutput: <EmotionTable emotions={data} /> });
})
});
}

fetch(url).then((response)=>{
response.json().then((data)=>{
this.setState({sentimentOutput:<EmotionTable emotions={data}/>});
})}) ;
}


render() {
return (
<div className="App">
<button className="btn btn-info" onClick={()=>{this.renderOutput('text')}}>Text</button>
<button className="btn btn-dark" onClick={()=>{this.renderOutput('url')}}>URL</button>
<br/><br/>
{this.state.innercomp}
<br/>
<button className="btn-primary" onClick={this.sendForSentimentAnalysis}>Analyze Sentiment</button>
<button className="btn-primary" onClick={this.sendForEmotionAnalysis}>Analyze Emotion</button>
<br/>
{this.state.sentimentOutput}
</div>
);
render() {
return (
<div className="App">
<button className="btn btn-info" onClick={() => { this.renderOutput('text') }}>Text</button>
<button className="btn btn-dark" onClick={() => { this.renderOutput('url') }}>URL</button>
<br /><br />
{this.state.innercomp}
<br />
<button className="btn-primary" onClick={this.sendForSentimentAnalysis}>Analyze Sentiment</button>
<button className="btn-primary" onClick={this.sendForEmotionAnalysis}>Analyze Emotion</button>
<br />
{this.state.sentimentOutput}
</div>
);
}
}

Expand Down
40 changes: 24 additions & 16 deletions sentimentAnalyzeClient/src/EmotionTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ import './bootstrap.min.css';

class EmotionTable extends React.Component {
render() {
//Returns the emotions as an HTML table
return (
<div>
<table className="table table-bordered">
<tbody>
{
/*Write code to use the .map method that you worked on in the
Hands-on React lab to extract the emotions. If you are stuck,
please click the instructions to see how to implement a map*/
}
</tbody>
</table>
</div>
);
}

//Returns the emotions as an HTML table
return (
<div>
<table className="table table-bordered">
<tbody>
{
/*Write code to use the .map method that you worked on in the
Hands-on React lab to extract the emotions. If you are stuck,
please click the instructions to see how to implement a map*/
Object.entries(this.props.emotions).map(function (mapentry) {
return (
<tr>
<td>{mapentry[0]}</td>
<td>{mapentry[1]}</td>
</tr>
)
})
}
</tbody>
</table>
</div>
);
}

}
export default EmotionTable;
Loading