Skip to main content

An easy-to-use API wrapper for Perspective API written in Python.

Project description

perspective.py

An easy-to-use API wrapper written in Python, for Perspective API, an API that uses machine learning to identify "toxic" comments.

Installation

Download & install git from https://git-scm.com/ and run below command:

python -m pip install --upgrade git+https://github.com/Yilmaz4/perspective.py.git

Or alternatively, you can install from PyPI (Python Package Index):

pyhton -m pip install --upgrade perspective.py

Getting an API key

You need to get an API key from Google to use Perspective API. Instructions are explained in this article.

Command arguments

text: str The text to analyze

requestedAttributes: list[str] A list of attributes to analyze the text for.

language: str The language of text.

You can use either Attributes.TOXICITY object or simply "TOXICITY" as a string for requestedAttributes argument.

You can specify the language of the text by using language argument. language argument accepts both language codes (such as "en" or "es") and language names (such as "English" or "Spanish"). Small spelling mistakes in language names can also be accepted (such as "Eglish" or "Spamish"). If you set language argument to None, language will be automatically detected.

You can find a list of all attributes and languages each attribute supports in this article.

Example usage

from perspective import Client, Attributes

# Creating the Client object with the API key
API_KEY = "your_api_key"
client = Client(token = API_KEY)

# Make a request to Perspective API with a text to analyze and requested attributes
response = client.analyze(text = "Hey! How are you?", requestedAttributes = [Attributes.TOXICITY, Attributes.INSULT])

# Print the response (dict)
print(response)

# Print the percent of TOXICITY attribute
print(response["TOXICITY"]

# Iterate over the response
for attribute, result in response.items():
    print(attribute.capitalize() + ": " + "%.2f" % result + "%")

Output

{'TOXICITY': 7.019685000000001, 'INSULT': 3.9963423999999996}
7.019685000000001
Toxicity: 7.02%
Insult: 4.00%

As you can see in the output, Client.analyze returns a dictionary with requested attributes and their analysis results as percents. You can get percents of each attribute, or iterate over dictionary.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

perspective.py-0.2.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

perspective.py-0.2.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file perspective.py-0.2.0.tar.gz.

File metadata

  • Download URL: perspective.py-0.2.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for perspective.py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e4f451804098f3d2e50cd5e7cd9b1e327c06da63b3a6c42e04777c62892a589d
MD5 8fa2ec22440939a3affa4ffc866977a6
BLAKE2b-256 acdc03d450b8b5bb84419fcd7c168c7c7aa266b44b256e242fc973677f7e42d8

See more details on using hashes here.

File details

Details for the file perspective.py-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: perspective.py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for perspective.py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 868d3bc4c546b6e1ebd7f01f878d46b7d8a20fde088325f15733bc3fc5a89a20
MD5 bd32131778076cc3e061f5c66c827da7
BLAKE2b-256 81311d5c57a0abdae7092e31a0e68a20c58b96f71b65d97ce0c6482a9f45930e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page