-
Notifications
You must be signed in to change notification settings - Fork 1
/
perspective.py
executable file
·63 lines (50 loc) · 1.61 KB
/
perspective.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
#!/usr/bin/env python3
from apikey import API_KEY
import sys
import requests
import json
PERSPECTIVE_URL = 'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key={}'.format(API_KEY)
enum_to_str_stats = {
'TOXICITY': 'Toxicity',
'ATTACK_ON_AUTHOR': 'Attack on author',
'ATTACK_ON_COMMENTER': 'Attack on commenter',
'INCOHERENT': 'Incorrehence',
'INFLAMMATORY': 'Inflammatory',
'LIKELY_TO_REJECT': 'Likely to be rejected by mod',
'OBSCENE': 'Obscenity',
'SEVERE_TOXICITY': 'Severe toxicity',
'SPAM': 'Potential spam',
'UNSUBSTANTIAL': 'Ubsubstantial comments'
}
def get_toxicity(text):
data = {
'comment': {'text': text},
'languages': ['en'],
'requestedAttributes': {}
}
# Request the statistics outlined in the enum_to_str_stats key strings.
# Below will request these fields from the API.
for k in enum_to_str_stats.keys():
data['requestedAttributes'][k] = {}
r = requests.post(PERSPECTIVE_URL, json=data)
try:
stats = {}
for attrib in r.json()['attributeScores'].keys():
stats[attrib] = r.json()['attributeScores'][attrib]['summaryScore']['value']
except KeyError:
print(r.text)
return {}
return stats
def main():
try:
while 1:
text = input('Enter text: ')
if not text:
continue
for k, v in get_toxicity(text).items():
print('\t{} estimate: {:.1%}'.format(enum_to_str_stats[k], v))
except KeyboardInterrupt:
print()
return
if __name__ == '__main__':
sys.exit(main())