Skip to content

Commit

Permalink
Add simple tweet text sentiment analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesley LeMahieu committed Oct 16, 2022
1 parent 9c5828f commit a5f97b5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"license": "MIT",
"dependencies": {
"@google-cloud/functions-framework": "^3.1.2",
"axios": "^1.1.0"
"@types/sentiment": "^5.0.1",
"axios": "^1.1.0",
"sentiment": "^5.0.2"
},
"scripts": {
"start": "functions-framework --source=build/src/ --target=run --port=8088",
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/calculateSentiment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as Sentiment from 'sentiment';

const sentiment = new Sentiment();
const calculateSentiment = (text: string) => sentiment.analyze(text);

export default calculateSentiment;
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {HttpFunction} from '@google-cloud/functions-framework/build/src/functions';
import getUser from './api/getUser';
import getTweets from './api/getTweets';
import calculateSentiment from './helpers/calculateSentiment';

const twitterUsername = 'wesleylemahieu';

Expand Down Expand Up @@ -39,6 +40,10 @@ export const run: HttpFunction = async (req, res) => {
res.send({
// likes: likes?.data,
nextToken: newNextToken,
tweets,
tweets: tweets.map(tweet => ({
...tweet,
sentiment: calculateSentiment(tweet.text),
})),
twitterUsername,
});
};

0 comments on commit a5f97b5

Please sign in to comment.