Skip to content

Commit bd07872

Browse files
committed
Removes extraneous createService method, reduces line length in integration tests, and adds verbose comments on mock entities.
1 parent a5f7fc0 commit bd07872

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
5757
String command = args[0];
5858
String text = args[1];
5959

60-
Analyze app = new Analyze(createLanguageService());
60+
Analyze app = new Analyze(LanguageServiceClient.create());
6161

6262
if (command.equals("entities")) {
6363
printEntities(System.out, app.analyzeEntities(text));
@@ -140,12 +140,6 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
140140
}
141141
}
142142

143-
/**
144-
* Connects to the Natural Language API using Application Default Credentials.
145-
*/
146-
public static LanguageServiceClient createLanguageService() throws IOException{
147-
return LanguageServiceClient.create();
148-
}
149143

150144
private final LanguageServiceClient languageApi;
151145

@@ -160,10 +154,11 @@ public Analyze(LanguageServiceClient languageApi) {
160154
* Gets {@link Entity}s from the string {@code text}.
161155
*/
162156
public List<Entity> analyzeEntities(String text) throws IOException {
163-
AnalyzeEntitiesRequest request =
164-
AnalyzeEntitiesRequest.newBuilder()
165-
.setDocument(Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT))
166-
.setEncodingType(EncodingType.UTF16).build();
157+
Document doc = Document.newBuilder()
158+
.setContent(text).setType(Type.PLAIN_TEXT).build();
159+
AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
160+
.setDocument(doc)
161+
.setEncodingType(EncodingType.UTF16).build();
167162
AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request);
168163
return response.getEntitiesList();
169164
}
@@ -172,18 +167,21 @@ public List<Entity> analyzeEntities(String text) throws IOException {
172167
* Gets {@link Sentiment} from the string {@code text}.
173168
*/
174169
public Sentiment analyzeSentiment(String text) throws IOException {
175-
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(
176-
Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build());
170+
Document doc = Document.newBuilder()
171+
.setContent(text).setType(Type.PLAIN_TEXT).build();
172+
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc);
177173
return response.getDocumentSentiment();
178174
}
179175

180176
/**
181177
* Gets {@link Token}s from the string {@code text}.
182178
*/
183179
public List<Token> analyzeSyntax(String text) throws IOException {
180+
Document doc = Document.newBuilder()
181+
.setContent(text).setType(Type.PLAIN_TEXT).build();
184182
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
185-
.setDocument(Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build())
186-
.setEncodingType(EncodingType.UTF16).build();
183+
.setDocument(doc)
184+
.setEncodingType(EncodingType.UTF16).build();
187185
AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request);
188186
return response.getTokensList();
189187
}

language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.cloud.language.spi.v1.LanguageServiceClient;
2122
import com.google.cloud.language.v1.Entity;
2223
import com.google.cloud.language.v1.PartOfSpeech.Tag;
2324
import com.google.cloud.language.v1.Sentiment;
@@ -41,7 +42,7 @@ public class AnalyzeIT {
4142
private Analyze analyzeApp;
4243

4344
@Before public void setup() throws Exception {
44-
analyzeApp = new Analyze(Analyze.createLanguageService());
45+
analyzeApp = new Analyze(LanguageServiceClient.create());
4546
}
4647

4748
@Test public void analyzeEntities_withEntities_returnsLarryPage() throws Exception {

language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class AnalyzeTest {
6969
// Arrange
7070
ByteArrayOutputStream bout = new ByteArrayOutputStream();
7171
PrintStream out = new PrintStream(bout);
72+
73+
// Mock natural-language entities based on actual data.
7274
ImmutableList<Entity> entities =
7375
ImmutableList.of(
7476
Entity.newBuilder().setName("Larry Page")
@@ -88,10 +90,10 @@ public class AnalyzeTest {
8890
.build(),
8991
Entity.newBuilder().setName("something").build());
9092

91-
// Act
93+
// Act on sample code with mock data.
9294
Analyze.printEntities(out, entities);
9395

94-
// Assert
96+
// Assert output from sample matches expected output.
9597
String got = bout.toString();
9698
assertThat(got).contains("Found 3 entities.");
9799
assertThat(got).contains("Larry Page");

0 commit comments

Comments
 (0)