0.0.6 • Published 11 months ago

@savasoglu/perspective-ts v0.0.6

Weekly downloads
-
License
MIT
Repository
-
Last release
11 months ago

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-ts

npm:

npm install @savasoglu/perspective-ts

Getting Started 🚀

  1. Sign up for the Google Perspective API.
  2. Install Perspective.TS using the instructions mentioned above.
  3. Import the library into your TypeScript project.
  4. Set up your Google Perspective API key.
  5. 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:

  • TOXICITY
  • SEVERE_TOXICITY
  • IDENTITY_ATTACK
  • INSULT
  • PROFANITY
  • THREAT

Each attribute score includes the following properties:

  • spanScores: An array of SpanScore objects, 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.

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago

0.0.0

11 months ago