-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaiapplied.py
57 lines (51 loc) · 1.84 KB
/
aiapplied.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
import json
import logging
from api import API
LOGGER = logging.getLogger('APICompare.AIApplied')
class AIApplied(API):
def __init__(self, api_key, language="en"):
self.name = "AIApplied"
self.api_key = api_key
self.url = "http://api.ai-applied.nl/api/sentiment_api/"
self.language_mapping = {
'en': 'eng',
'nl': 'nld',
'de': 'deu',
'fr': 'fra',
'es': 'spa',
'it': 'ita',
'ru': 'rus'
}
self.language = self.language_mapping[language]
def extract_label(self, original_label):
"""Given scores for pos, neg and neu, output the label
"""
if original_label == 'positive':
return '+'
elif original_label == 'negative':
return '-'
else:
return '0'
def analyse(self, text):
"""Assign the sentiment label for the text.
:return label: +, -, or 0
"""
request = {"data": {
"api_key": self.api_key,
"call": {
"return_original": False,
"classifier": "subjective",
"data": [
{
"text": text,
"language_iso": self.language,
"id": 0
}
]
}
}
}
params = {'request': json.dumps(request)}
data = self.get_data(params)
LOGGER.debug("Got response: %r" % data)
return self.extract_label(data["response"]["data"][0]['sentiment_class'])