Skip to content

Commit 13c25fc

Browse files
jmdobryShabirmean
authored andcommitted
samples: Add NL quickstart sample. Fix some other quickstarts. (#438)
1 parent 13912b8 commit 13c25fc

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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+
// [START language_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.language.spi.v1.LanguageServiceClient;
22+
23+
import com.google.cloud.language.v1.Document;
24+
import com.google.cloud.language.v1.Document.Type;
25+
import com.google.cloud.language.v1.Sentiment;
26+
27+
public class QuickstartSample {
28+
public static void main(String... args) throws Exception {
29+
// Instantiates a client
30+
LanguageServiceClient language = LanguageServiceClient.create();
31+
32+
// The text to analyze
33+
String text = "Hello, world!";
34+
Document doc = Document.newBuilder()
35+
.setContent(text).setType(Type.PLAIN_TEXT).build();
36+
37+
// Detects the sentiment of the text
38+
Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
39+
40+
System.out.printf("Text: %s%n", text);
41+
System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude());
42+
}
43+
}
44+
// [END language_quickstart]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2016, Google, Inc.
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 org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for quickstart sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class QuickstartSampleIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testQuickstart() throws Exception {
53+
// Act
54+
QuickstartSample.main();
55+
56+
// Assert
57+
String got = bout.toString();
58+
assertThat(got).contains("Text: Hello, world!");
59+
assertThat(got).contains("Sentiment: ");
60+
}
61+
}

0 commit comments

Comments
 (0)