1 from googleapiclient import discovery
4 from pathlib import Path
8 from itertools import islice
10 API_KEY = open('perspective_api_key').read()
12 client = discovery.build("commentanalyzer","v1alpha",developerKey=API_KEY,discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",static_discovery=False,)
14 csvreader = csv.DictReader(open("all_data.csv"))
16 outfile = Path("perspective_results.json")
17 already_scored = set()
19 already_scored = set([json.loads(l)['id'] for l in open(str(outfile),'r')])
21 with open("perspective_results.json",'w') as of:
22 for line in csvreader:
23 if line['id'] not in already_scored:
24 analyze_request = {'comment':{'text':line['comment_text']},
26 'requestedAttributes':{'TOXICITY':{},
32 response = client.comments().analyze(body=analyze_request).execute()
33 response['id'] = line['id']
34 result = json.dumps(response)
35 of.write(result + '\n')