GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add R&R method to rank based on an InputStream · einsnull/java-sdk@ea05d7f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • main/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1
      • RetrieveAndRank.java
    • test/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1
      • RetrieveAndRankIT.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/main/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1/RetrieveAndRank.java‎

Copy file name to clipboardExpand all lines: src/main/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1/RetrieveAndRank.java
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
package com.ibm.watson.developer_cloud.retrieve_and_rank.v1;
1515

16+
import java.io.ByteArrayOutputStream;
1617
import java.io.File;
18+
import java.io.IOException;
1719
import java.io.InputStream;
1820
import java.util.logging.Level;
1921
import java.util.logging.Logger;
@@ -319,6 +321,45 @@ public ServiceCall<Ranking> rank(final String rankerID, final File answers, Inte
319321
return createServiceCall(request, ResponseConverterUtils.getObject(Ranking.class));
320322
}
321323

324+
/**
325+
* Gets and returns the ranked answers.
326+
*
327+
* @param rankerID The ranker ID
328+
* @param answers The CSV input that contains the search results that you want to rank.
329+
* @param topAnswers The number of top answers needed, default is 10
330+
* @return the ranking of the answers
331+
*/
332+
public ServiceCall<Ranking> rank(final String rankerID, final InputStream answers,
333+
Integer topAnswers) {
334+
Validator.isTrue(rankerID != null && !rankerID.isEmpty(), "rankerID cannot be null or empty");
335+
Validator.notNull(answers, "answers file cannot be null");
336+
337+
final JsonObject contentJson = new JsonObject();
338+
contentJson.addProperty(ANSWERS, (topAnswers != null && topAnswers > 0) ? topAnswers : 10);
339+
340+
final ByteArrayOutputStream answersBuffer = new ByteArrayOutputStream();
341+
int bytesRead;
342+
final byte[] data = new byte[10000];
343+
try {
344+
while ((bytesRead = answers.read(data, 0, data.length)) != -1) {
345+
answersBuffer.write(data, 0, bytesRead);
346+
}
347+
} catch (final IOException e) {
348+
throw new RuntimeException("Error reading search results input", e);
349+
}
350+
351+
final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
352+
.addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"answer_data\""),
353+
RequestBody.create(HttpMediaType.BINARY_FILE, answersBuffer.toByteArray()))
354+
.addPart(Headers.of(HttpHeaders.CONTENT_DISPOSITION, "form-data; name=\"answer_metadata\""),
355+
RequestBody.create(HttpMediaType.TEXT, contentJson.toString()))
356+
.build();
357+
358+
final String path = String.format(PATH_RANK, rankerID);
359+
final Request request = RequestBuilder.post(path).body(body).build();
360+
return createServiceCall(request, ResponseConverterUtils.getObject(Ranking.class));
361+
}
362+
322363
/*
323364
* (non-Javadoc)
324365
*
Collapse file

‎src/test/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1/RetrieveAndRankIT.java‎

Copy file name to clipboardExpand all lines: src/test/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1/RetrieveAndRankIT.java
+15-6Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertTrue;
2020

2121
import java.io.File;
22+
import java.io.FileInputStream;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.util.List;
@@ -35,8 +36,8 @@
3536
import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.model.Ranking;
3637
import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.model.SolrCluster;
3738
import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.model.SolrCluster.Status;
38-
import com.ibm.watson.developer_cloud.service.exception.BadRequestException;
3939
import com.ibm.watson.developer_cloud.retrieve_and_rank.v1.model.SolrClusterOptions;
40+
import com.ibm.watson.developer_cloud.service.exception.BadRequestException;
4041

4142
/**
4243
* The Class RetrieveAndRankIT.
@@ -79,25 +80,33 @@ public void setUp() throws Exception {
7980
* Test create ranker and rank results.
8081
*
8182
* @throws InterruptedException the interrupted exception
83+
* @throws IOException
8284
*/
8385
@Test
84-
@Ignore
85-
public void testCreateRankerAndRankResults() throws InterruptedException {
86+
public void testCreateRankerAndRankResults() throws InterruptedException, IOException {
8687
final File trainingFile = new File(RESOURCE_PATH + "ranker_train.csv");
8788
final File testFile = new File(RESOURCE_PATH + "ranker_test.csv");
8889
final int numToRank = 5;
8990

9091
final Ranker ranker = service.createRanker(RANKER_NAME, trainingFile).execute();
9192
try {
9293
assertEquals(RANKER_NAME, ranker.getName());
93-
for (int x = 0; x < 20
94-
&& service.getRankerStatus(ranker.getId()).execute().getStatus() != Ranker.Status.AVAILABLE; x++) {
94+
for (int x = 0; x < 20 && service.getRankerStatus(ranker.getId()).execute()
95+
.getStatus() != Ranker.Status.AVAILABLE; x++) {
9596
Thread.sleep(10000);
9697
}
9798
assertEquals(Ranker.Status.AVAILABLE, service.getRankerStatus(ranker.getId()).execute().getStatus());
9899

99-
final Ranking ranking = service.rank(ranker.getId(), testFile, numToRank).execute();
100+
Ranking ranking = service.rank(ranker.getId(), testFile, numToRank).execute();
100101
assertTrue(!ranking.getAnswers().isEmpty());
102+
103+
final FileInputStream testInputStream = new FileInputStream(testFile);
104+
try {
105+
ranking = service.rank(ranker.getId(), testInputStream, numToRank).execute();
106+
assertTrue(!ranking.getAnswers().isEmpty());
107+
} finally {
108+
testInputStream.close();
109+
}
101110
} finally {
102111
service.deleteRanker(ranker.getId());
103112
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.