-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperspective.py
More file actions
59 lines (51 loc) · 1.95 KB
/
perspective.py
File metadata and controls
59 lines (51 loc) · 1.95 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
from googleapiclient import discovery
import json
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
import text2emotion as te
from hatesonar import Sonar
import sklearn.linear_model._logistic
# for environmental variable
from dotenv import load_dotenv
load_dotenv()
import os
token = os.environ.get("api_key")
"""
Purpose: Utilize python NLP packages to analyze text.
"""
def perspectiveAPI(text):
API_KEY = token
service = discovery.build('commentanalyzer', 'v1alpha1', developerKey=API_KEY)
analyze_request = {
'comment': { 'text': text },
'requestedAttributes': {'TOXICITY': {}, 'INSULT': {}, 'PROFANITY': {},'THREAT': {},'IDENTITY_ATTACK': {}}
}
response = service.comments().analyze(body=analyze_request).execute()
print(round(response['attributeScores']['THREAT']['summaryScore']['value'],4))
return {
"toxicity": round(response['attributeScores']['TOXICITY']['summaryScore']['value'],4),
"insult": round(response['attributeScores']['INSULT']['summaryScore']['value'],4),
"profanity": round(response['attributeScores']['PROFANITY']['summaryScore']['value'],4),
"threat": round(response['attributeScores']['THREAT']['summaryScore']['value'],4),
"identity_attack": round(response['attributeScores']['IDENTITY_ATTACK']['summaryScore']['value'], 4)
}
def spacyFunctions(text):
nlp = spacy.load('en_core_web_sm')
spacy_text_blob = SpacyTextBlob()
nlp.add_pipe(spacy_text_blob)
doc = nlp(text)
important_words = []
for i in doc._.sentiment.assessments:
for j in i[0]:
if j not in important_words:
important_words.append(j)
return {
'polarity': round(doc._.sentiment.polarity, 4),
'subjectivity': round(doc._.sentiment.subjectivity,4),
'assessments': important_words
}
def emotions(text):
return te.get_emotion(text)
def hateSonar(text):
sonar = Sonar()
print(sonar.ping(text))