-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzeSentiment.js
More file actions
81 lines (69 loc) · 2.92 KB
/
Copy pathanalyzeSentiment.js
File metadata and controls
81 lines (69 loc) · 2.92 KB
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
import { HfInference } from '@huggingface/inference';
import gplay from 'google-play-scraper';
import readlineSync from 'readline-sync';
import dotenv from 'dotenv';
import { dbConnection } from "./config/dbConfig.js";
import { Review } from './Schemas/reviewSchema.js';
dotenv.config();
const hf = new HfInference(process.env.HUGGINGFACE_API_KEY);
const AppList = ['com.esense.topschool.student', 'com.hurix.navneet.cloudreader','com.hurix.navneet.cloudreader']
async function analyzeSentiment(text) {
const response = await hf.textClassification({
model: 'distilbert-base-uncased-finetuned-sst-2-english',
// model:' tabularisai/multilingual-sentiment-analysis',
inputs: text,
});
return response;
}
async function fetchAndAnalyzeReviews() {
try {
// Database connection done
const db = await dbConnection();
console.log('Enter Enter Your Application Id:');
const systemPrompt = readlineSync.question('>> ');
if (!AppList.includes(systemPrompt)) {
throw new Error("Invalid App id")
} else {
const reviews = await gplay.reviews({
appId: systemPrompt,
sort: gplay.sort.NEWEST,
num: 100,
});
console.log(JSON.stringify(reviews.data))
console.log(`Fetched ${reviews.data.length} reviews. Analyzing sentiment...`);
for (const review of reviews.data) {
const sentiment = await analyzeSentiment(review.text);
console.log('\nReview:', review.text);
console.log('Sentiment Analysis:', sentiment);
let userSentiment = '';
(sentiment[0]?.score > 0.5) ? userSentiment = sentiment[0]?.label : userSentiment = sentiment[0]?.label
await Review.findOneAndUpdate(
{ reviewId: review.id },
{
reviewId: review.id,
appId: systemPrompt,
userName: review.userName,
userImage: review.userImage,
date: review.date,
score: review.score,
scoreText: review.scoreText,
url: review.url,
title: review.title,
text: review.text,
replyDate: review.replyDate,
replyText: review.replyText,
version: review.version,
thumbsUp: review.thumbsUp,
criterias: review.criterias,
sentiment: userSentiment,
},
{ upsert: true, new: true }
);
console.log(`Review with ID ${review.id} processed and saved to the database.`);
}
}
} catch (error) {
console.error('Error:', error.message);
}
}
fetchAndAnalyzeReviews();