This repository was archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAnalyzeIT.java
More file actions
177 lines (154 loc) · 6.15 KB
/
AnalyzeIT.java
File metadata and controls
177 lines (154 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.language;
import static com.google.common.truth.Truth.assertThat;
import com.google.cloud.language.v1.PartOfSpeech.Tag;
import com.google.cloud.language.v1.Sentiment;
import com.google.cloud.language.v1.Token;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Integration (system) tests for {@link Analyze}. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class AnalyzeIT {
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;
@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}
@After
public void tearDown() {
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
}
@Test
public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception {
Analyze.classifyText(
"Android is a mobile operating system developed by Google, "
+ "based on the Linux kernel and designed primarily for touchscreen "
+ "mobile devices such as smartphones and tablets.");
String got = bout.toString();
assertThat(got).contains("Computers & Electronics");
}
@Test
public void analyzeCategoriesInFileReturnsExpectedResult() throws Exception {
String gcsFile = "gs://cloud-samples-data/language/android.txt";
Analyze.classifyFile(gcsFile);
String got = bout.toString();
assertThat(got).contains("Computers & Electronics");
}
@Test
public void analyzeEntities_withEntities_returnsLarryPage() throws Exception {
Analyze.analyzeEntitiesText(
"Larry Page, Google's co-founder, once described the 'perfect search engine' as"
+ " something that 'understands exactly what you mean and gives you back exactly what"
+ " you want.' Since he spoke those words Google has grown to offer products beyond"
+ " search, but the spirit of what he said remains.");
String got = bout.toString();
assertThat(got).contains("Larry Page");
}
@Test
public void analyzeEntities_withEntitiesFile_containsCalifornia() throws Exception {
Analyze.analyzeEntitiesFile("gs://cloud-samples-data/language/entity.txt");
String got = bout.toString();
assertThat(got).contains("California");
}
@Test
public void analyzeSentimentText_returnPositive() throws Exception {
Sentiment sentiment =
Analyze.analyzeSentimentText(
"Tom Cruise is one of the finest actors in hollywood and a great star!");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
@Test
public void analyzeSentimentFile_returnPositiveFile() throws Exception {
Sentiment sentiment =
Analyze.analyzeSentimentFile(
"gs://cloud-samples-data/language/" + "sentiment-positive.txt");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
@Test
public void analyzeSentimentText_returnNegative() throws Exception {
Sentiment sentiment =
Analyze.analyzeSentimentText("That was the worst performance I've seen in a while.");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isLessThan(0.0F);
}
@Test
public void analyzeSentiment_returnNegative() throws Exception {
Sentiment sentiment =
Analyze.analyzeSentimentFile(
"gs://cloud-samples-data/language/" + "sentiment-negative.txt");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isLessThan(0.0F);
}
@Test
public void analyzeSyntax_partOfSpeech() throws Exception {
List<Token> tokens =
Analyze.analyzeSyntaxText("President Obama was elected for the second term");
List<Tag> got =
tokens.stream().map(e -> e.getPartOfSpeech().getTag()).collect(Collectors.toList());
assertThat(got)
.containsExactly(
Tag.NOUN, Tag.NOUN, Tag.VERB, Tag.VERB, Tag.ADP, Tag.DET, Tag.ADJ, Tag.NOUN)
.inOrder();
}
@Test
public void analyzeSyntax_partOfSpeechFile() throws Exception {
List<Token> token =
Analyze.analyzeSyntaxFile("gs://cloud-samples-data/language/" + "syntax-sentence.txt");
List<Tag> got =
token.stream().map(e -> e.getPartOfSpeech().getTag()).collect(Collectors.toList());
assertThat(got)
.containsExactly(Tag.DET, Tag.VERB, Tag.DET, Tag.ADJ, Tag.NOUN, Tag.PUNCT)
.inOrder();
}
@Test
public void analyzeEntitySentimentTextReturnsExpectedResult() throws Exception {
Analyze.entitySentimentText(
"Oranges, grapes, and apples can be "
+ "found in the cafeterias located in Mountain View, Seattle, and London.");
String got = bout.toString();
assertThat(got).contains("Seattle");
}
@Test
public void analyzeEntitySentimentTextEncodedReturnsExpectedResult() throws Exception {
Analyze.entitySentimentText("foo→bar");
String got = bout.toString();
assertThat(got).contains("offset: 4");
}
@Test
public void analyzeEntitySentimenFileReturnsExpectedResult() throws Exception {
Analyze.entitySentimentFile("gs://cloud-samples-data/language/president.txt");
String got = bout.toString();
assertThat(got).contains("Kennedy");
}
}