Created
November 19, 2020 04:35
-
-
Save joelmasters/93ae6c8d58e828fb10eed122ceb7491e to your computer and use it in GitHub Desktop.
Sentiment Analyzer App.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
import './App.css'; | |
const ml5 = window.ml5; | |
let sentiment; | |
function App() { | |
let [text, setText] = useState(''); | |
let [score, setScore] = useState(0); | |
let [modelIsReady, setModelIsReady] = useState(false); | |
const handleChange = (e) => { | |
setText(e.target.value); | |
} | |
const calculateSentiment = () => { | |
const prediction = sentiment.predict(text); | |
setScore(prediction.score); | |
} | |
useEffect(() => { | |
console.log("loading model"); | |
sentiment = ml5.sentiment('movieReviews', modelReady); | |
}, []) | |
function modelReady() { | |
console.log('Model Loaded!'); | |
setModelIsReady(true); | |
} | |
return ( | |
<div className="App"> | |
<h1>Sentiment Analyzer</h1> | |
<textarea id="input" onChange={handleChange} placeholder="hello I like you!" disabled={!modelIsReady}></textarea> | |
<br /> | |
<p>{modelIsReady ? '' : 'Loading model...'}</p> | |
<button onClick={calculateSentiment}>Calculate</button> | |
<br /> | |
<p>Sentiment Score: {score.toFixed(5)}</p> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment