-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3. Detect_Emotion.py
56 lines (50 loc) · 2.05 KB
/
3. Detect_Emotion.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
## 감정분석
## 결과물 : titleid, nom, comment, emotion
## 긍정:true 부정:false
# Imports the Google Cloud client library
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import pandas as pd
import os
# 자신의 구글 클라우드 인증, 환경변수 설정
json_path = 'path/to/own.json'
high_csv_path = 'path/to/highstar_reply.csv'
low_csv_path = 'path/to/lowstar_reply.csv'
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = json_path
highstar_csv = pd.read_csv(high_csv_path)
lowstar_csv = pd.read_csv(low_csv_path)
client = language.LanguageServiceClient()
def emotion_detect(csvlist, filename):
## 2차원 배열 동적 선언
emotion_lst = []
for i in range(10):
emotion_lst.append([])
for i in range(1,11):
txt_lst = list(csvlist[f'comment{i}'])
for txt in txt_lst :
document = types.Document(
content=str(txt),
type=enums.Document.Type.PLAIN_TEXT)
try :
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
score = sentiment.score
if score >= 0.10 :
print('Text: {}'.format(txt))
print('Sentiment: {}'.format(score))
emotion_lst[i-1].append(True)
else :
print('Text: {}'.format(txt))
print('Sentiment: {}'.format(score))
emotion_lst[i-1].append(False)
except:
print('오류! : 구글 클라우드에서 지원하지 않는 문자열입니다.')
print('Text: {}'.format(txt))
emotion_lst[i-1].append('추출불가')
for i in range(1,11) :
csvlist[f'comment{i}e'] = emotion_lst[i-1]
print(csvlist)
csvlist.to_csv(f"./{filename}", encoding="utf_8_sig", mode='w', index=True)
emotion_detect(lowstar_csv,'lowstar_emotion.csv')
emotion_detect(highstar_csv,'highstar_emotion.csv')