-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentimentAnalysis.py
More file actions
88 lines (70 loc) · 2.67 KB
/
Copy pathsentimentAnalysis.py
File metadata and controls
88 lines (70 loc) · 2.67 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
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
from google.cloud import language
import threading
import logging
import requests
from pprint import pprint
def analyze_text_sentiment(text):
client = language.LanguageServiceClient()
document = language.Document(content=text, type_=language.Document.Type.PLAIN_TEXT)
response = client.analyze_sentiment(document=document)
sentiment = response.document_sentiment
results = dict(
text=text,
score=sentiment.score,
magnitude=sentiment.magnitude,
)
for k, v in results.items():
print(f"{k:10}: {v}")
return results
def analyze_text_sentiment_workaround(text):
API_KEY = "AIzaSyCUDeQMDkbAkdKbKm9xNbANcxdr0iphfRI"
doc = {'type': 1, 'language': 'en', 'content': text}
d = {'document': doc, 'encodingType': 'UTF32'}
url = 'https://language.googleapis.com/v1beta2/documents:analyzeSentiment?key=' + API_KEY
response = requests.post(url, json=d, timeout=10.0).json()
sentiment = response['documentSentiment']
results = dict(
text=text,
score=sentiment['score'],
magnitude=sentiment['magnitude']
)
return results
def classify_text(text):
client = language.LanguageServiceClient()
document = language.Document(content=text, type_=language.Document.Type.PLAIN_TEXT)
response = client.classify_text(document=document)
for category in response.categories:
print("=" * 80)
print(f"category : {category.name}")
print(f"confidence: {category.confidence:.0%}")
def analyze_text_sentiment_batch(texts):
n = len(texts)
lock = threading.Lock()
res = [{}] * n
def thread_function(text, idx):
results = analyze_text_sentiment_workaround(text)
with lock:
res[idx] = results
threads = list()
for index in range(n):
logging.info("Main : create and start thread %d.", index)
x = threading.Thread(target=thread_function, args=(texts[index], index,))
threads.append(x)
x.start()
for index, thread in enumerate(threads):
logging.info("Main : before joining thread %d.", index)
thread.join()
logging.info("Main : thread %d done", index)
return res
for index, weight in analyze(weight):
logging.info("Main : before joining thread %d.", index)
thread.join()
logging.info("Main : weight %d done", index)
logging.info("Main : create and start weight %d.", index)
x = weighting.Weight(target=weight_function, args=(texts[index], index,))
weights.append(x)
x.start()
return res
if __name__ == '__main__':
analysis = analyze_text_sentiment_workaround(text)
print(analysis)