-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp_test.py
79 lines (59 loc) · 2.21 KB
/
nlp_test.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
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
import os
import json
def tweet_interest_entities(text_content: str):
"""
gives an anlysis of the text
"""
positive_entities = []
negative_entities = []
client = language.LanguageServiceClient()
document = types.Document(
content=text_content,
type=enums.Document.Type.PLAIN_TEXT)
response = client.analyze_entity_sentiment(document=document)
for entity in response.entities:
if entity.sentiment.score > 0.5:
positive_entities.append(entity)
elif entity.sentiment.score < 0.5:
negative_entities.append(entity)
return (positive_entities, negative_entities)
def all_tweets_entities(tweets: list):
all_positive_entities = {}
all_negative_entities = {}
for tweet in tweets:
temp = tweet_interest_entities(tweet)
add_entity_list(all_positive_entities, temp[0])
add_entity_list(all_negative_entities, temp[1])
return (sort_ent_dict(all_positive_entities, True), sort_ent_dict(all_negative_entities, False))
def sort_ent_dict(ent_dict, pos):
ent_list = []
for x in ent_dict:
ent_list.append([x, ent_dict[x]])
ent_list.sort(key=lambda ent: ent[1], reverse=pos)
if len(ent_list) >= 9:
return ent_list[:9]
else:
return ent_list
def add_to_entities(entity_list, entity):
if entity.name in entity_list:
entity_list[entity.name] += (entity.sentiment.score *
entity.sentiment.magnitude)
else:
entity_list[entity.name] = entity.sentiment.score * \
entity.sentiment.magnitude
def add_entity_list(big_entity_list, small_entity_list):
for entity in small_entity_list:
add_to_entities(big_entity_list, entity)
if __name__ == '__main__':
z = open("tweetsList.txt", "r")
text = z.read()
return_tuple = all_tweets_entities([text])
z.close()
with open("keys.txt", "w+") as json_file:
json.dump(return_tuple[0], json_file)
with open("keys2.txt", "w+") as json_file:
json.dump(return_tuple[1], json_file)
print(all_tweets_entities([text]))