-
Notifications
You must be signed in to change notification settings - Fork 2
/
v_sent.py
36 lines (22 loc) · 924 Bytes
/
v_sent.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
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
PATH_TO_INPUT = 'instances.jsonl'
TRUTH = 'truth.jsonl'
PATH_TO_FILE = 'features.csv'
analyser = SentimentIntensityAnalyzer()
#function that assign 1 to positive text
def sentiment_scores(sentence):
score = analyser.polarity_scores(sentence)
#return list(snt.keys())[list(snt.values()).index(max(snt.values()))]
return 1 if score['pos'] > score['neg'] else 0
def main():
df = pd.read_json(PATH_TO_INPUT, dtype={'id': str}, lines=True)
df['postText'] = df['postText'].apply(lambda x: x[0])
features = pd.DataFrame()
features['id'] = df['id']
fields = ['postText', 'targetTitle', 'targetDescription']
for f in fields[0:2]:
features['sentiment' + f] = df[f].apply(sentiment_scores)
print(features)
if __name__ == '__main__':
main()