Skip to content

Commit 12679ae

Browse files
authored
Merge pull request #1130 from watson-developer-cloud/sdk-passage-patch
Sdk passage patch
2 parents ecd0529 + b66e5d4 commit 12679ae

File tree

5 files changed

+189
-16
lines changed

5 files changed

+189
-16
lines changed

discovery/src/main/java/com/ibm/watson/discovery/v2/model/QueryResponse.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class QueryResponse extends GenericModel {
3737
@SerializedName("table_results")
3838
protected List<QueryTableResult> tableResults;
3939

40+
protected List<QueryResponsePassage> passages;
41+
4042
/**
4143
* Gets the matchingResults.
4244
*
@@ -113,4 +115,15 @@ public List<QuerySuggestedRefinement> getSuggestedRefinements() {
113115
public List<QueryTableResult> getTableResults() {
114116
return tableResults;
115117
}
118+
119+
/**
120+
* Gets the passages.
121+
*
122+
* <p>Passages returned by Discovery.
123+
*
124+
* @return the passages
125+
*/
126+
public List<QueryResponsePassage> getPassages() {
127+
return passages;
128+
}
116129
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* (C) Copyright IBM Corp. 2020.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.watson.discovery.v2.model;
14+
15+
import com.google.gson.annotations.SerializedName;
16+
import com.ibm.cloud.sdk.core.service.model.GenericModel;
17+
18+
/** A passage query response. */
19+
public class QueryResponsePassage extends GenericModel {
20+
21+
@SerializedName("passage_text")
22+
protected String passageText;
23+
24+
@SerializedName("passage_score")
25+
protected Double passageScore;
26+
27+
@SerializedName("document_id")
28+
protected String documentId;
29+
30+
@SerializedName("collection_id")
31+
protected String collectionId;
32+
33+
@SerializedName("start_offset")
34+
protected Long startOffset;
35+
36+
@SerializedName("end_offset")
37+
protected Long endOffset;
38+
39+
protected String field;
40+
41+
/**
42+
* Gets the passageText.
43+
*
44+
* <p>The content of the extracted passage.
45+
*
46+
* @return the passageText
47+
*/
48+
public String getPassageText() {
49+
return passageText;
50+
}
51+
52+
/**
53+
* Gets the passageScore.
54+
*
55+
* <p>The confidence score of the passages's analysis. A higher score indicates greater
56+
* confidence.
57+
*
58+
* @return the passageScore
59+
*/
60+
public Double getPassageScore() {
61+
return passageScore;
62+
}
63+
64+
/**
65+
* Gets the documentId.
66+
*
67+
* <p>The unique identifier of the ingested document.
68+
*
69+
* @return the documentId
70+
*/
71+
public String getDocumentId() {
72+
return documentId;
73+
}
74+
75+
/**
76+
* Gets the collectionId.
77+
*
78+
* <p>The unique identifier of the collection.
79+
*
80+
* @return the collectionId
81+
*/
82+
public String getCollectionId() {
83+
return collectionId;
84+
}
85+
86+
/**
87+
* Gets the startOffset.
88+
*
89+
* <p>The position of the first character of the extracted passage in the originating field.
90+
*
91+
* @return the startOffset
92+
*/
93+
public Long getStartOffset() {
94+
return startOffset;
95+
}
96+
97+
/**
98+
* Gets the endOffset.
99+
*
100+
* <p>The position of the last character of the extracted passage in the originating field.
101+
*
102+
* @return the endOffset
103+
*/
104+
public Long getEndOffset() {
105+
return endOffset;
106+
}
107+
108+
/**
109+
* Gets the field.
110+
*
111+
* <p>The label of the field from which the passage has been extracted.
112+
*
113+
* @return the field
114+
*/
115+
public String getField() {
116+
return field;
117+
}
118+
}

discovery/src/test/java/com/ibm/watson/discovery/v1/DiscoveryServiceIT.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424
import com.google.gson.internal.LazilyParsedNumber;
2525
import com.ibm.cloud.sdk.core.http.HttpConfigOptions;
2626
import com.ibm.cloud.sdk.core.http.HttpMediaType;
27+
import com.ibm.cloud.sdk.core.http.Response;
2728
import com.ibm.cloud.sdk.core.security.Authenticator;
2829
import com.ibm.cloud.sdk.core.security.BasicAuthenticator;
2930
import com.ibm.cloud.sdk.core.security.BearerTokenAuthenticator;
3031
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
31-
import com.ibm.cloud.sdk.core.service.exception.BadRequestException;
32-
import com.ibm.cloud.sdk.core.service.exception.ForbiddenException;
33-
import com.ibm.cloud.sdk.core.service.exception.InternalServerErrorException;
34-
import com.ibm.cloud.sdk.core.service.exception.NotFoundException;
32+
import com.ibm.cloud.sdk.core.service.exception.*;
3533
import com.ibm.cloud.sdk.core.util.GsonSingleton;
3634
import com.ibm.watson.common.RetryRunner;
3735
import com.ibm.watson.common.WaitFor;
@@ -40,6 +38,8 @@
4038
import com.ibm.watson.discovery.query.Operator;
4139
import com.ibm.watson.discovery.v1.model.*;
4240
import com.ibm.watson.discovery.v1.model.NormalizationOperation.Operation;
41+
42+
import java.awt.*;
4343
import java.io.ByteArrayInputStream;
4444
import java.io.FileInputStream;
4545
import java.io.FileNotFoundException;
@@ -354,7 +354,7 @@ public void pingIsSuccessful() {
354354
}
355355

356356
/** Bad credentials throws exception. */
357-
@Test(expected = ForbiddenException.class)
357+
@Test(expected = UnauthorizedException.class)
358358
public void badCredentialsThrowsException() {
359359
Discovery badService = new Discovery("2019-04-30", new BasicAuthenticator("foo", "bar"));
360360
badService.listEnvironments(null).execute().getResult();
@@ -2304,7 +2304,8 @@ private class WaitForCollectionAvailable implements WaitFor.Condition {
23042304
public boolean isSatisfied() {
23052305
GetCollectionOptions getOptions =
23062306
new GetCollectionOptions.Builder(environmentId, collectionId).build();
2307-
String status = discovery.getCollection(getOptions).execute().getResult().getStatus();
2307+
Collection collectionResult = discovery.getCollection(getOptions).execute().getResult();
2308+
String status = collectionResult.getStatus();
23082309
return status.equals(Collection.Status.ACTIVE);
23092310
}
23102311

discovery/src/test/java/com/ibm/watson/discovery/v2/DiscoveryIT.java

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.ibm.cloud.sdk.core.http.HttpMediaType;
2222
import com.ibm.cloud.sdk.core.http.Response;
2323
import com.ibm.cloud.sdk.core.security.Authenticator;
24-
import com.ibm.cloud.sdk.core.security.BasicAuthenticator;
24+
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
2525
import com.ibm.watson.common.RetryRunner;
2626
import com.ibm.watson.common.WatsonServiceTest;
2727
import com.ibm.watson.discovery.query.AggregationType;
@@ -32,21 +32,21 @@
3232
import java.io.IOException;
3333
import java.io.InputStream;
3434
import java.util.ArrayList;
35+
import java.util.List;
3536
import java.util.UUID;
36-
import org.junit.Assume;
3737
import org.junit.Before;
3838
import org.junit.Ignore;
3939
import org.junit.Test;
4040
import org.junit.runner.RunWith;
4141

4242
/** The Class DiscoveryIT. */
43-
@Ignore
4443
@RunWith(RetryRunner.class)
44+
@Ignore
4545
public class DiscoveryIT extends WatsonServiceTest {
4646
private static final String VERSION = "2019-11-22";
4747
private static final String RESOURCE = "src/test/resources/discovery/v2/";
48-
private static final String PROJECT_ID = "9558dc01-8554-4d18-b0a5-70196f9f2fe6";
49-
private static final String COLLECTION_ID = "161d1e47-9651-e657-0000-016e8e939caf";
48+
private static final String PROJECT_ID = "571ec4f7-c413-4928-b7f9-3c69045ed27a";
49+
private static final String COLLECTION_ID = "d02b9aa7-903e-2ea1-0000-017410e684bd";
5050

5151
private Discovery service;
5252

@@ -64,12 +64,11 @@ public class DiscoveryIT extends WatsonServiceTest {
6464
@Before
6565
public void setUp() throws Exception {
6666
super.setUp();
67-
String bearerToken = getProperty("discovery_v2.bearer_token");
68-
Assume.assumeFalse("config.properties doesn't have valid credentials.", (bearerToken == null));
6967

70-
// Authenticator authenticator = new BearerTokenAuthenticator(bearerToken);
7168
String apiKey = getProperty("discovery.apikey");
72-
Authenticator authenticator = new BasicAuthenticator("apikey", apiKey);
69+
Authenticator authenticator =
70+
new IamAuthenticator(
71+
apiKey, "https://iam.test.cloud.ibm.com/identity/token", null, null, false, null);
7372
service = new Discovery(VERSION, authenticator);
7473
service.setDefaultHeaders(getDefaultHeaders());
7574
service.setServiceUrl(getProperty("discovery_v2.url"));
@@ -1103,4 +1102,47 @@ public void testDeleteUserData() {
11031102
assertNotNull(deleteResponse);
11041103
assertTrue(deleteResponse.getStatusCode() == 204);
11051104
}
1105+
1106+
/** Test query passages per document true. */
1107+
@Test
1108+
public void TestQueryPassagesPerDocumentTrue() {
1109+
List<String> Ids = new ArrayList<>();
1110+
Ids.add(COLLECTION_ID);
1111+
QueryLargePassages queryLargePassages =
1112+
new QueryLargePassages.Builder().perDocument(true).build();
1113+
1114+
QueryOptions queryOptions =
1115+
new QueryOptions.Builder()
1116+
.projectId(PROJECT_ID)
1117+
.collectionIds(Ids)
1118+
.passages(queryLargePassages)
1119+
.query("text:IBM")
1120+
.count(2)
1121+
.build();
1122+
QueryResponse queryResult = service.query(queryOptions).execute().getResult();
1123+
assertNotNull(queryResult.getResults().get(0).getDocumentPassages().get(0).getPassageText());
1124+
}
1125+
1126+
/** Test query passages per document false. */
1127+
@Test
1128+
public void TestQueryPassagesPerDocumentFalse() {
1129+
List<String> Ids = new ArrayList<>();
1130+
Ids.add(COLLECTION_ID);
1131+
QueryLargePassages queryLargePassages =
1132+
new QueryLargePassages.Builder().perDocument(false).build();
1133+
1134+
QueryOptions queryOptions =
1135+
new QueryOptions.Builder()
1136+
.projectId(PROJECT_ID)
1137+
.collectionIds(Ids)
1138+
.passages(queryLargePassages)
1139+
.query("text:IBM")
1140+
.count(2)
1141+
.build();
1142+
QueryResponse queryResult = service.query(queryOptions).execute().getResult();
1143+
assertNotNull(queryResult);
1144+
assertNotNull(queryResult.getPassages().get(0).getCollectionId());
1145+
assertNotNull(queryResult.getPassages().get(0).getPassageText());
1146+
assertNotNull(queryResult.getPassages().get(0).getDocumentId());
1147+
}
11061148
}

language-translator/src/test/java/com/ibm/watson/language_translator/v3/LanguageTranslatorIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ public void testDocumentTranslation() throws FileNotFoundException, InterruptedE
230230
new TranslateDocumentOptions.Builder()
231231
.file(new File("src/test/resources/language_translation/document_to_translate.txt"))
232232
.fileContentType(HttpMediaType.TEXT_PLAIN)
233-
.filename("java-integration-test-file")
234233
.source("en")
235234
.target("es")
236235
.build();

0 commit comments

Comments
 (0)