Skip to content

Commit 6d3f31a

Browse files
nnegreyShabirmean
authored andcommitted
samples: Set Endpoint Samples (#1644)
* Set Endpoint Samples * Update pom.xml
1 parent a1f6acd commit 6d3f31a

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Tests for Natural Language Set Endpoint */
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class SetEndpointIT {
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
40+
@Before
41+
public void setUp() {
42+
bout = new ByteArrayOutputStream();
43+
out = new PrintStream(bout);
44+
System.setOut(out);
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
System.setOut(null);
50+
}
51+
52+
53+
@Test
54+
public void testSetEndpoint() throws IOException {
55+
// Act
56+
SetEndpoint.setEndpoint();
57+
58+
// Assert
59+
String got = bout.toString();
60+
assertThat(got).contains("Sentiment");
61+
}
62+
}

0 commit comments

Comments
 (0)