|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.language; |
| 18 | + |
| 19 | +import com.google.cloud.language.v1.Document; |
| 20 | +import com.google.cloud.language.v1.LanguageServiceClient; |
| 21 | +import com.google.cloud.language.v1.LanguageServiceSettings; |
| 22 | +import com.google.cloud.language.v1.Sentiment; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +class SetEndpoint { |
| 27 | + |
| 28 | + // Change your endpoint |
| 29 | + static void setEndpoint() throws IOException { |
| 30 | + // [START language_set_endpoint] |
| 31 | + LanguageServiceSettings settings = LanguageServiceSettings.newBuilder() |
| 32 | + .setEndpoint("eu-language.googleapis.com:443") |
| 33 | + .build(); |
| 34 | + |
| 35 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 36 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 37 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 38 | + LanguageServiceClient client = LanguageServiceClient.create(settings); |
| 39 | + // [END language_set_endpoint] |
| 40 | + |
| 41 | + // The text to analyze |
| 42 | + String text = "Hello, world!"; |
| 43 | + Document doc = Document.newBuilder() |
| 44 | + .setContent(text).setType(Document.Type.PLAIN_TEXT).build(); |
| 45 | + |
| 46 | + // Detects the sentiment of the text |
| 47 | + Sentiment sentiment = client.analyzeSentiment(doc).getDocumentSentiment(); |
| 48 | + |
| 49 | + System.out.printf("Text: %s%n", text); |
| 50 | + System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude()); |
| 51 | + client.close(); |
| 52 | + } |
| 53 | +} |
0 commit comments