Package: perspective.py
perspective.pyThe perspective.py library is an easy-to-use API wrapper written in Python forPerspective API, an API that uses machine learning to identify "toxic" comments.InstallationDownload & install git fromhttps://git-scm.com/and run below command:python-mpipinstall--upgradegit+https://github.com/Yilmaz4/perspective.py.gitOr alternatively, you can install from PyPI (Python Package Index):pyhton-mpipinstall--upgradeperspective.pyGetting an API keyYou need to get an API key from Google to use Perspective API. Instructions are explained inthis article.Command argumentstext: strThe text to analyzerequestedAttributes: list[str]A list of attributes to analyze the text for.language: strThe language of text.You can use eitherAttributes.TOXICITYobject or simply"TOXICITY"as a string forrequestedAttributesargument.You can specify the language of the text by usinglanguageargument.languageargument 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 setlanguageargument toNone, language will be automatically detected.You can find a list of all attributes and languages each attribute supports inthis article.Example usagefromperspectiveimportClient,Attributes,utils# Create the Client object which we will use to make requests with the API keyAPI_KEY="your_api_key"client=Client(token=API_KEY)# Make a request to Perspective API with a text to analyze and the attributes that you want the text to be analyzed forresponse=client.analyze(text="Hey! How are you?",requestedAttributes=[Attributes.TOXICITY,Attributes.INSULT])# Print the response as a dictionaryprint(response)# Print the score value of TOXICITY attributeprint(response["TOXICITY"]print("  ")# Iterate over the responseforattribute,resultinresponse.items():print(attribute.capitalize()+": "+"%.2f"%result+"%")print("  ")# Or alternatively, use utils.format_response to print a formatted text of the response which would return almost the same result as the above codeprint(utils.format_response(response,align_right=True))Output{'TOXICITY':7.019685000000001,'INSULT':3.9963423999999996}7.019685000000001Toxicity:7.02%Insult:4.00%Toxicity:7.02%Insult:4.00%As you can see in the output,Client.analyzereturns a dictionary with requested attributes and their analysis results as percents. You can get percents of each attribute, or iterate over dictionary.Example usage for creating a bar chartfromperspectiveimportClient,Attributes,utils# Create the Client object which we will use to make requests with the API keyAPI_KEY="your_api_key"client=Client(token=API_KEY)# Make a request to Perspective API with a text to analyze and the attributes that you want the text to be analyzed forresponse=client.analyze(text="Hey! How are you?",requestedAttributes=Attributes.Production)# Attributes.Production includes all production-ready attributes# Create a horizontal bar chart and show it by popping up a windowutils.show_graph(response=response,title="Sample graph")# Or alternatively, you can save the chart to an image fileutils.save_graph(response=response,filename="my_chart.png",title="Sample graph")OutputLicenseMIT LicenseCopyright (c) 2021-2022 Yilmaz AlpaslanPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.