forked from coderdj/reviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataframe.py
102 lines (79 loc) · 2.77 KB
/
dataframe.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from langdetect import detect,LangDetectException
import pandas as pd
import numpy as np
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from scipy.special import softmax
def LanguagedetectTweets(df):
##
n = len(df)
lanlist = []
for i in range(n):
lanlist.append(detect(df.iloc[i]["content"]))
## add language to df
df["language_prob"] = lanlist
return df
def GetDataframeTweets(df):
##
n = len(df)
lanlist = []
for i in range(n):
lanlist.append(detect(df.iloc[i]["content"]))
## add language to df
df["language_prob"] = lanlist
# sentiment
# load model and tokenizer in English
roberta = "oliverguhr/german-sentiment-bert"
model = AutoModelForSequenceClassification.from_pretrained(roberta)
tokenizer = AutoTokenizer.from_pretrained(roberta)
labels = ['Positive', 'Negative', 'Neutral']
german_tweets = df["renderedContent"].tolist()
# print(german_tweets[:10])
positive = []
negative = []
neutral = []
count = 0
for tweet in german_tweets:
count += 1
print(count)
tweet_words = []
for word in tweet.split(' '):
if word.startswith('@') and len(word) > 1:
word = '@user'
elif word.startswith('http'):
word = "http"
tweet_words.append(word)
tweet_proc = " ".join(tweet_words)
encoded_tweet = tokenizer(tweet_proc, return_tensors='pt')
output = model(**encoded_tweet)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
positive.append(scores[0])
negative.append(scores[1])
neutral.append(scores[2])
df_result = pd.DataFrame(list(zip(positive, negative, neutral)), columns =['Positive', 'Negative', 'Neutral'])
df = pd.concat([df, df_result], axis=1)
return df
def GetDataframeStores(df_play, df_app):
##
app_liste = ["date", "title", "review", "rating"]
df_app = df_app[app_liste]
df_app["content"] = df_app["title"] + ": " + df_app["review"]
df_app = df_app[["rating", "content", "date"]]
# Playstore DatenFarme bearbeiten
play_list = ['at', 'score', "content"]
df_play = df_play[play_list]
df_play = df_play.rename(columns={"score": "rating", "at": "date"})
df = pd.concat([df_app, df_play], axis = 0)
df["content"].fillna("",inplace=True)
n = len(df)
lanlist = []
for i in range(n):
try:
# print(str(df.iloc[i]["content"]))
# print(detect(str(df.iloc[i]["content"])))
lanlist.append(str(detect(df.iloc[i]["content"])))
except LangDetectException:
lanlist.append(np.nan)
## add language to df
df["language_prob"] = lanlist
return df