-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyze.py
276 lines (202 loc) · 9.95 KB
/
Analyze.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import openai
import requests
import sys
import json
from googletrans import Translator
from google.cloud import language_v1
from google.oauth2 import service_account
from collections import Counter
from datetime import datetime
import time
import requests
from flask import Flask, request,jsonify
from multiprocessing import Process
from konlpy.tag import Okt
import numpy as np
import threading
import time
import App
import Config
import Data
import Utils
def sentiment_store(content,average_negative,average_positive,average_neutral,id):
time = Utils.call_time()
negative_value = average_negative
positive_value = average_positive
neutral_value = average_neutral
Data.analyze_data.append({"content":content,"negative":negative_value,"positive":positive_value,"neutral":neutral_value,"date":time,"username":id})
def analyze_sentiment(content,id):
okt = Okt()
CheckSentence = False
# 품사 태깅
tagged = okt.pos(content)
print(tagged)
# 태그가 'Adjective' , 'Noun'+'Josa'+'Verb' , 'Noun'+'Verb'인 단어들만 추출
adjectives_r = []
for i in range(len(tagged)) :
word, tag = tagged[i]
adjectives = []
if tag == 'Adjective':
if i > 0 and (tagged[i-1][1] in ('Adverb','VerbPrefix')) : #부사, #접두사
if(i > 1 and (tagged[i-2][1] in ('Noun') and not(tagged[i-2][0] in ('개','꺼져','뭐')))) :
adjectives.append(tagged[i-2][0]+tagged[i-1][0] + word)
CheckSentence = True
elif(i > 2 and (tagged[i-2][1] in ('Josa') and (tagged[i-3][1] in ('Noun')))) :
adjectives.append(tagged[i-3][0]+tagged[i-2][0]+tagged[i-1][0] + word)
CheckSentence = True
else :
adjectives.append(tagged[i-1][0] + word)
CheckSentence = True
elif(i > 0 and (tagged[i-1][1] in ('Noun') and not(tagged[i-2][0] in ('개','꺼져','뭐')))) :
adjectives.append(tagged[i-1][0] + word)
CheckSentence = True
elif(i > 1 and (tagged[i-1][1] in ('Josa') and (tagged[i-2][1] in ('Noun')))) :
adjectives.append(tagged[i-2][0] + tagged[i-1][0] + word)
CheckSentence = True
else :
adjectives.append(word)
CheckSentence = True
if tag == 'Verb' and not(tagged[i-1][1] in ('Verb')):
if (i > 0 and (tagged[i-1][1] in ('Adverb','VerbPrefix'))) :
if(i > 1 and (tagged[i-2][1] in ('Noun'))) :
adjectives.append(tagged[i-2][0]+tagged[i-1][0] + word)
CheckSentence = True
elif(i > 2 and (tagged[i-2][1] in ('Josa') and (tagged[i-3][1] in ('Noun')))) :
adjectives.append(tagged[i-3][0]+tagged[i-2][0]+tagged[i-1][0] + word)
CheckSentence = True
else :
adjectives.append(tagged[i-1][0] + word)
CheckSentence = True
elif(i > 0 and (tagged[i-1][1] in ('Noun') and not(tagged[i-1][0] in ('개','꺼져','뭐')))) :
adjectives.append(tagged[i-1][0] + word)
CheckSentence = True
elif(i > 1 and (tagged[i-1][1] in ('Josa') and (tagged[i-2][1] in ('Noun')))) :
adjectives.append(tagged[i-2][0] + tagged[i-1][0] + word)
CheckSentence = True
else :
adjectives.append(word)
CheckSentence = True
if(i < len(tagged)-2 and tagged[i+1][1] in ('Verb') and tagged[i+2][1] in ('Verb')) :
adjectives.append(tagged[i+1][0] + tagged[i+2][0])
elif(i < len(tagged)-1 and tagged[i+1][1] in ('Verb')) :
adjectives.append(tagged[i+1][0])
elif(i == 0 and tag == 'Verb'):
if(i < len(tagged)-1 and tagged[i+1][1] in ('Verb')):
adjectives.append(word + tagged[i+1][0])
else :
adjectives.append(word)
if tag == 'Eomi' :
if( i > 0 and (tagged[i-1][1] in ('Noun'))) :
adjectives.append(tagged[i-1][0] + word)
CheckSentence = True
joined_string = ''.join(adjectives)
if(joined_string !='') :
adjectives_r.append(joined_string)
print(adjectives_r)
count = len(adjectives_r)
print(count)
if(CheckSentence == True):
client_id = "y31l8r0q5p"
client_secret = "6VBpXzn7aDXU1jgLRGobWFnl85C3CP7sIPKrdPQH"
url = "https://naveropenapi.apigw.ntruss.com/sentiment-analysis/v1/analyze"
headers = {
"X-NCP-APIGW-API-KEY-ID": client_id,
"X-NCP-APIGW-API-KEY": client_secret,
"Content-Type": "application/json"
}
negative_scores = []
positive_scores = []
neutral_scores = []
negative_scores_p = []
positive_scores_p = []
neutral_scores_p = []
for verb in adjectives_r:
data = {
"content": verb
}
response = requests.post(url, data=json.dumps(data), headers=headers)
rescode = response.status_code
if rescode == 200:
jsonD = response.json()
print(f"감정 분석 결과 for '{verb}': ", jsonD)
if((jsonD['document']['confidence']['neutral']) < 2) :
negative_scores.append(jsonD['document']['confidence']['negative'])
positive_scores.append(jsonD['document']['confidence']['positive'])
neutral_scores.append(jsonD['document']['confidence']['neutral'])
else :
negative_scores_p.append(jsonD['document']['confidence']['negative'])
positive_scores_p.append(jsonD['document']['confidence']['positive'])
neutral_scores_p.append(jsonD['document']['confidence']['neutral'])
print()
else:
print("Error: " + response.text)
if(len(negative_scores) != 0 ):
average_negative = np.mean(negative_scores)
average_positive = np.mean(positive_scores)
average_neutral = np.mean(neutral_scores)
else :
average_negative = np.mean(negative_scores_p)
average_positive = np.mean(positive_scores_p)
average_neutral = np.mean(neutral_scores_p)
print(f"Negative scores average: {average_negative}")
print(f"Positive scores average: {average_positive}")
print(f"Neutral scores average: {average_neutral}")
print()
else :
client_id = "y31l8r0q5p"
client_secret = "6VBpXzn7aDXU1jgLRGobWFnl85C3CP7sIPKrdPQH"
url = "https://naveropenapi.apigw.ntruss.com/sentiment-analysis/v1/analyze"
headers = {
"X-NCP-APIGW-API-KEY-ID": client_id,
"X-NCP-APIGW-API-KEY": client_secret,
"Content-Type": "application/json"
}
data = {
"content": content
}
response = requests.post(url, data=json.dumps(data), headers=headers)
rescode = response.status_code
if rescode == 200:
jsonD = response.json()
average_negative = (jsonD['document']['confidence']['negative'])
average_positive = (jsonD['document']['confidence']['positive'])
average_neutral = (jsonD['document']['confidence']['neutral'])
print(jsonD)
else:
print("Error: " + response.text)
sentiment_store(content,average_negative,average_positive,average_neutral,id)
def extract_keywords(text,id):
key_path = 'service_account_key.json'
time = Utils.call_time()
# 서비스 계정 키를 사용하여 Credentials 객체 생성
credentials = service_account.Credentials.from_service_account_file(key_path)
client = language_v1.LanguageServiceClient(credentials=credentials)
document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
response = client.analyze_entities(request={'document': document})
entity_names = [entity.name for entity in response.entities]
top_keywords = Counter(entity_names).most_common(5)
for keyword, count in top_keywords:
Data.key_word.append({"keyword":keyword , "count":count, "date":time,"username":id})
def translate_text(text, source_language, target_language):
translator = Translator(service_urls=['translate.google.com'])
translation = translator.translate(text, src=source_language, dest=target_language)
return translation.text
def classify_document(text,id):
key_path = 'C:/Users/yechan/gpt-3/service_account_key.json'
time = Utils.call_time()
# 한국어 문장을 영어로 번역
translated_text = translate_text(text, 'ko', 'en')
# 서비스 계정 키를 사용하여 Credentials 객체 생성
credentials = service_account.Credentials.from_service_account_file(key_path)
client = language_v1.LanguageServiceClient(credentials=credentials)
document = language_v1.Document(content=translated_text, type_=language_v1.Document.Type.PLAIN_TEXT)
response = client.classify_text(request={'document': document})
categories = response.categories
if categories:
for category in categories:
name = translate_text(category.name,'en','ko')
name = name.replace('/','')
print(f'category: {name}, confidence: {category.confidence}')
Data.Category.append({"category":name , "confidence":category.confidence,"date":time, "username":id})
else:
print('No categories found.')