-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
69 lines (50 loc) · 1.71 KB
/
model.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
from expertai.nlapi.cloud.client import ExpertAiClient
import os
from dotenv import load_dotenv
load_dotenv()
username = os.getenv("EAI_USERNAME")
password = os.getenv("EAI_PASSWORD")
class Model:
def __init__(self) -> None:
self.client = ExpertAiClient()
def hate_speech(self, text) -> dict:
"""Classify input text as hate if detected.
Args:
text (string): input text or string.
Returns:
dict: {string: string}
"""
detector = "hate-speech"
language = "en"
output = self.client.detection(
body={"document": {"text": text}},
params={"detector": detector, "language": language},
)
speech = {}
for category in output.categories:
speech_type = category.hierarchy
speech["speech_type"] = speech_type[0]
speech["category_id"] = category.id_
speech["category_hierarchy"] = category.hierarchy
i = 1
for extraction in output.extractions:
for field in extraction.fields:
speech[field.name] = field.value
i = i + 1
return speech
def sentiment_analysis(self, text):
"""Classify input text's sentiments.
Args:
text (string): input text or string.
Returns:
dict: {string : string}
"""
language = "en"
sentiment = {}
output = self.client.specific_resource_analysis(
body={"document": {"text": text}},
params={"language": language, "resource": "sentiment"},
)
# Output overall sentiment
sentiment["score"] = output.sentiment.overall
return sentiment