primelib / perspective4j

Java Client for the Perspective API: https://www.perspectiveapi.com
MIT License
1 stars 0 forks source link
java library perspectiveapi

Perspective4J

Maven Central javadoc

A java library for the PerspectiveAPI.

Installation

implementation("io.github.primelib:perspective4j:<latestVersion>")

Click to view instructions for other build tools.

Usage

Consumer Specification Approach

PerspectiveConsumerApi client = PerspectiveFactory.create(spec -> {
    spec.api(PerspectiveConsumerApi.class);
    spec.apiKeyAuth(auth -> {
        auth.apiKey("<apiKey>");
    });
});

CommentAnalyzeResult result = client.analyzeCommentV1Alpha1(spec -> {
    spec.text("<text>");
    spec.languages(Collections.singleton("en"));
    spec.requestedAttributes(Collections.singleton(AttributeType.TOXICITY));
    // do not store for research purposes, if data being submitted is private (i.e. not publicly accessible), or if the data submitted contains content written by someone under 13 years old (or the relevant age determined by applicable law in my jurisdiction)
    spec.doNotStore(true);
    spec.spanAnnotations(true); // includes begin and end index for detected spans
});

Parameter Approach

PerspectiveApi client = PerspectiveFactory.create(spec -> {
    spec.api(PerspectiveApi.class);
    spec.apiKeyAuth(auth -> {
        auth.apiKey("<apiKey>");
    });
});

CommentAnalyzeResult result = client.analyzeCommentV1Alpha1(new CommentAnalyzeRequest(new Comment("<text>"), Collections.singletonMap(AttributeType.TOXICITY, null), null, null, true, null, null, null));

NOTE: The Parameter Approach can break if the API changes. The Consumer Specification Approach is more resilient to API changes.

Links

License

Released under the MIT License.