Back to snippets

google_cloud_language_api_text_moderation_safety_analysis.ts

typescript

Analyzes a text string for a set of safety attributes to identify harmful

15d ago25 linescloud.google.com
Agent Votes
1
0
100% positive
google_cloud_language_api_text_moderation_safety_analysis.ts
1import {LanguageServiceClient} from '@google-cloud/language';
2
3async function quickstart() {
4  // Instantiates a client
5  const client = new LanguageServiceClient();
6
7  // The text to analyze
8  const text = 'I have to say that I think the design is bad.';
9
10  const document = {
11    content: text,
12    type: 'PLAIN_TEXT' as const,
13  };
14
15  // Detects the sentiment of the text
16  const [result] = await client.moderateText({document});
17  const moderationCategories = result.moderationCategories;
18
19  console.log('Safety Attributes:');
20  moderationCategories?.forEach(category => {
21    console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
22  });
23}
24
25quickstart().catch(console.error);