<jupyter_start><jupyter_text>Perspective API ExplorationFirst, we have a dataset of Wikipedia comments made available by Jigsaw, a subsidiary of Google that created the Perspective tool. The dataset includes a unique comment id, the text of the comment, and a series of binary labels applied by human raters: "toxic," "severe_toxic," "obscene," "threat," "insult," and "identity_hate." I have appended the "score" column, which represents the toxicity score assigned to the comment text by the live version of the Perspective API. The data is available under a CC0 license.<jupyter_code>import pandas as pd
import time

df = pd.read_csv('labeled_and_scored_comments.csv')
df.sort_values(['score'])
print(df['score'].mean())
df_toxic, df_nontoxic = [x for _, x in df.groupby(df['toxic'] < 1)]
df_nontoxic.sort_values(['score'])
print(df_nontoxic['score'].mean())
df_toxic.sort_values(['score'])
print(df_toxic['score'].mean())<jupyter_output>0.8336640688067462<jupyter_text>I've also included a function to make calls to the Perspective API for your own testing. You will need to generate your own API key according to the instructions in the assignment.<jupyter_code>from googleapiclient import discovery
import json

API_KEY = ''

client = discovery.build(
  "commentanalyzer",
  "v1alpha1",
  developerKey=API_KEY,
  discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
  static_discovery=False,
)

analyze_request = {
  'comment': { 'text': 'friendly greetings from python' },
  'requestedAttributes': {'TOXICITY': {}}
}

response = client.comments().analyze(body=analyze_request).execute()
print(json.dumps(response, indent=2))
from googleapiclient.discovery import build
import json

def get_toxicity_score(comment):
    
  API_KEY = ''
    
  client = build(
  "commentanalyzer",
  "v1alpha1",
  developerKey=API_KEY,
  discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
  static_discovery=False,
  )

  analyze_request = {
  'comment': { 'text': comment },
  'requestedAttributes': {'TOXICITY': {}}
  }
    
  response = client.comments().analyze(body=analyze_request).execute()
  toxicity_score = response["attributeScores"]["TOXICITY"]["summaryScore"]["value"]
    
  return toxicity_score<jupyter_output><empty_output><jupyter_text>We can call this function with original comments like this:<jupyter_code>get_toxicity_score("I think you are not correct")<jupyter_output><empty_output><jupyter_text>Due to the constraint of one request per second, we should insert a time.sleep() statement between consecutive API calls, which will pause execution for one second.<jupyter_code>comment_list = ['white woman', 'caucasian woman', 'black woman', 'hispanic woman', 'asian woman', 'latina woman', 'african american woman',
               'white man', 'caucasian man', 'black man', 'hispanic man', 'asian man', 'latino man', 'african american man',
                'white people','black people', 'hispanic people', 'asian people', 'latino people', 'african american people',
               'white scientist','black scientist', 'hispanic scientist', 'asian scientist', 'latino scientist', 'african american scientist',
               'white historian', 'black historian', 'hispanic historian', 'latino historian', 'african american historian',
               'white researcher', 'black researcher', 'hispanic researcher', 'asian researcher', 'latino researcher', 'african american researcher']

for comment in comment_list:
    score = get_toxicity_score(comment)
    print(comment, score)
    time.sleep(1)<jupyter_output>white woman 0.23009336
caucasian woman 0.14118251
black woman 0.36812845
hispanic woman 0.10568296
asian woman 0.13405651
latina woman 0.1647946
african american woman 0.19129816
white man 0.28593
caucasian man 0.18460794
black man 0.43249154
hispanic man 0.11973065
asian man 0.15583922
latino man 0.14942342
african american man 0.16419663
white people 0.30249438
black people 0.4596299
hispanic people 0.15037459
asian people 0.15464239
latino people 0.1406447
african american people 0.20994255
white scientist 0.15005042
black scientist 0.22996649
hispanic scientist 0.10225325
asian scientist 0.14634414
latino scientist 0.10336974
african american scientist 0.15856406
white historian 0.12803568
black historian 0.15799671
hispanic historian 0.12362919
latino historian 0.09875372
african american historian 0.24303061
white researcher 0.16106161
black researcher 0.26593867
hispanic researcher 0.14935419
asian researcher 0.19595867
latino researcher 0.11478338
african american researcher 0.[...]
{'fraction_non_alphanumeric': 0.10054171180931744, 'fraction_numerical': 0.07540628385698808, 'mean_word_length': 5.584878744650499, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '663329', 'license': 'MIT', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1384, 'n_words': 432, 'path': 'Main.ipynb', 'repo_name': 'anakarensalinas/HCDSAssignment2', 'size': '4615'}