-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetRating.py
More file actions
59 lines (45 loc) · 1.55 KB
/
getRating.py
File metadata and controls
59 lines (45 loc) · 1.55 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
from transformers import pipeline
import pandas as pd
import pymongo
from pymongo.server_api import ServerApi
sentiment_pipeline = pipeline("sentiment-analysis")
USERNAME = ""
PASSWORD = ""
CLUSTERURL = ""
uri = f"mongodb+srv://{USERNAME}:{PASSWORD}@{CLUSTERURL}/?retryWrites=true&w=majority"
client = pymongo.MongoClient(uri, server_api=ServerApi('1'))
db = client["Ratings"]
collection = db["RatingCollections"]
def getReviews(gameName):
data = pd.read_csv(f"gameReviews/{gameName}.csv")
reviews = list(data.Review)
for i in range(len(reviews)):
if len(reviews[i].split(" ")):
reviews[i] = reviews[i][:512]
return reviews
def process(gameName):
# Call the getReviews() function to get the reviews
print("running sentiment analysis for " + gameName)
reviews = getReviews(gameName)
res = sentiment_pipeline(reviews)
ratings = []
for i in res:
if i["label"] == "POSITIVE":
if i["score"] >= 0.999:
i["score"] = 1
ratings.append(i["score"])
else:
if i["score"] <= 0.099:
i["score"] = 0
ratings.append(1-i["score"])
avg_rating = sum(ratings) / len(ratings) * 10
avg_rating = round(avg_rating, 2)
print(f"\n-------- {gameName} result: {avg_rating} --------")
# Upload the results to MongoDB
document = {
'rating': str(avg_rating),
'name': gameName,
'cover': 'https://go.dev/blog/gopher/portrait.jpg'
}
collection.insert_one(document)
print("Result uploaded to MongoDB.")