@savasoglu/perspective-ts v0.0.6
Perspective.TS 🌟
Perspective.TS is a TypeScript library that simplifies integration with the Google Perspective API, enabling efficient content moderation and fostering a positive online environment. 😊
Installation 📦
You can easily install Perspective.TS using yarn or npm. Choose one of the following methods:
yarn:
yarn add @savasoglu/perspective-tsnpm:
npm install @savasoglu/perspective-tsGetting Started 🚀
- Sign up for the Google Perspective API.
- Install Perspective.TS using the instructions mentioned above.
- Import the library into your TypeScript project.
- Set up your Google Perspective API key.
- Start moderating content and creating a safer online space!
import PerspectiveClient, { AnalyzeCommentResponse } from '@savasoglu/perspective-ts';
const perspective = new PerspectiveClient('YOUR_API_KEY_HERE');
const comment = 'Hey, can you please review this comment?';
perspective.analyzeComment(comment).then(analysis => {
console.log(analysis.attributeScores.TOXICITY?.summaryScore.value);
});The analyzeComment method sends a request to the Google Perspective API to analyze the provided comment. It returns a Promise that resolves to an AnalyzeCommentResponse object containing the attribute scores and language information.
Attribute Scores
The AnalyzeCommentResponse object includes an attributeScores property, which contains attribute-specific scores for the analyzed comment. The supported attributes are:
TOXICITYSEVERE_TOXICITYIDENTITY_ATTACKINSULTPROFANITYTHREAT
Each attribute score includes the following properties:
spanScores: An array ofSpanScoreobjects, representing the scores for specific spans of the comment.summaryScore: A summary score indicating the overall attribute score for the entire comment.
const attributeScore = analysis.attributeScores.TOXICITY;
if (attributeScore) {
const { summaryScore, spanScores } = attributeScore;
console.log('Summary Score:', summaryScore.value);
spanScores.forEach(spanScore => {
console.log('Span:', comment.substring(spanScore.begin, spanScore.end));
console.log('Span Score:', spanScore.score.value);
});
}Error Handling
If an error occurs during the API request, an Error will be thrown with the corresponding error message.
perspective.analyzeComment(comment).then(analysis => {
console.log(analysis);
}).catch(error => {
console.error(error.message);
});Ensure that you replace YOUR_API_KEY_HERE with your actual Google Perspective API key.