一个易于使用的 API 包装器,用于用 Python 编写的 Perspective API。
项目描述
透视图.py
perspective.py 库是用 Python 为Perspective API编写的易于使用的 API 包装器, Perspective API是一种使用机器学习来识别“有毒”评论的 API。
安装
从https://git-scm.com/下载并安装 git并运行以下命令:
python -m pip install --upgrade git+https://github.com/Yilmaz4/perspective.py.git
或者,您可以从 PyPI(Python 包索引)安装:
pyhton -m pip install --upgrade perspective.py
获取 API 密钥
您需要从 Google 获取 API 密钥才能使用 Perspective API。本文解释了说明。
命令参数
text: str要分析的文本
requestedAttributes: list[str]用于分析文本的属性列表。
language: str文本的语言。
您可以使用任何一个Attributes.TOXICITY对象或简单地"TOXICITY"作为requestedAttributes参数的字符串。
language您可以使用参数指定文本的语言。language参数接受语言代码(例如“en”或“es”)和语言名称(例如“English”或“Spanish”)。也可以接受语言名称中的小拼写错误(例如“Eglish”或“Spamish”)。如果将language参数设置为None,将自动检测语言。
您可以在本文中找到每个属性支持的所有属性和语言的列表。
示例用法
from perspective import Client, Attributes, utils
# Create the Client object which we will use to make requests 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 the attributes that you want the text to be analyzed for
response = client.analyze(text = "Hey! How are you?", requestedAttributes = [Attributes.TOXICITY, Attributes.INSULT])
# Print the response as a dictionary
print(response)
# Print the score value of TOXICITY attribute
print(response["TOXICITY"]
print(" ")
# Iterate over the response
for attribute, result in response.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 code
print(utils.format_response(response, align_right=True))
输出
{'TOXICITY': 7.019685000000001, 'INSULT': 3.9963423999999996}
7.019685000000001
Toxicity: 7.02%
Insult: 4.00%
Toxicity: 7.02%
Insult: 4.00%
正如您在输出中看到的,Client.analyze返回一个字典,其中包含请求的属性及其百分比分析结果。您可以获得每个属性的百分比,或遍历字典。
创建条形图的示例用法
from perspective import Client, Attributes, utils
# Create the Client object which we will use to make requests 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 the attributes that you want the text to be analyzed for
response = 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 window
utils.show_graph(response=response, title="Sample graph")
# Or alternatively, you can save the chart to an image file
utils.save_graph(response=response, filename="my_chart.png", title="Sample graph")
输出
执照
麻省理工学院许可证
版权所有 (c) 2021-2022 Yilmaz Alpaslan
特此免费授予任何人获得本软件和相关文档文件(“软件”)的副本,以不受限制地处理本软件,包括但不限于使用、复制、修改、合并的权利、发布、分发、再许可和/或出售本软件的副本,并允许向其提供本软件的人这样做,但须符合以下条件:
上述版权声明和本许可声明应包含在本软件的所有副本或大部分内容中。
本软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、特定用途适用性和非侵权保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担任何责任,无论是在合同、侵权或其他方面,由本软件或本软件的使用或其他交易引起或与之相关。软件。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。