Skip to content

Commit

Permalink
#11 started implementation of bulk detection
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jul 2, 2021
1 parent 6d585e8 commit 7554342
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/main/java/io/imagineobjects/linguinai/LinguinAi.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.imagineobjects.linguinai;

import java.util.List;

/**
* Linguin AI in Java.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #8:60min Continue implementing the API, with the bulk detection
* endpoint. Allow user to specify encoding for each text.
*/
public interface LinguinAi {

Expand All @@ -26,4 +26,10 @@ public interface LinguinAi {
*/
Languages detect(final String text);

/**
* Detect the possible languages for more texts.
* @param texts Texts to detect.
* @return List of Languages.
*/
List<Languages> bulkDetect(final String... texts);
}
47 changes: 43 additions & 4 deletions src/main/java/io/imagineobjects/linguinai/RestLinguinAi.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package io.imagineobjects.linguinai;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;
import javax.json.*;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Linguin AI REST client entry point.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #11:60min Finish the implementation of method bulkDetect. It should
* return an instance of BulkDetection (extends Iterable of Languages).
*/
public final class RestLinguinAi implements LinguinAi {

Expand Down Expand Up @@ -131,4 +131,43 @@ public double confidence() {
);
}
}

@Override
public List<Languages> bulkDetect(final String... texts) {
try {
final StringBuilder query = new StringBuilder();
for(int i=0; i < texts.length; i++) {
query.append("q[]=").append(
URLEncoder.encode(
texts[i],
StandardCharsets.UTF_8.toString()
)
);
if(i < texts.length -1) {
query.append("&");
}
}
final Resource resource = this.resources.post(
URI.create(
this.baseUri + "/bulk?" + query.toString()
),
Json.createObjectBuilder().build()
);
if (resource.statusCode() != HttpURLConnection.HTTP_OK) {
throw new IllegalStateException(
"Call to /bulk returned status code "
+ resource.statusCode() + ", instead of 200 OK"
);
}
System.out.println(resource.asJsonObject());
} catch (final UnsupportedEncodingException ex) {
throw new IllegalStateException(
"UnsupportedEncodingException when trying to detect language "
+ "of texts " + Arrays.toString(texts) + " with Charset ["
+ StandardCharsets.UTF_8.toString() + "].",
ex
);
}
throw new UnsupportedOperationException("Not yet finished.");
}
}

2 comments on commit 7554342

@zoeself
Copy link
Collaborator

@zoeself zoeself commented on 7554342 Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil I've opened the Issues [#15] for the newly added to-dos.

The to-dos may have been added in an earlier commit, but I've found them just now.

@zoeself
Copy link
Collaborator

@zoeself zoeself commented on 7554342 Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil I've closed the Issues [#11] since their to-dos disappeared from the code.

The to-dos may have been removed in an earlier commit, but I've found it just now.

Please sign in to comment.