Compare commits
No commits in common. "ab796dbae8677137d98172ca3578d19eb59b8b0c" and "a2af7bcdd84f5b1a35495f61db1faa1a87e14ccb" have entirely different histories.
ab796dbae8
...
a2af7bcdd8
@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group 'tech.nevets.deepj'
|
group 'tech.nevets.deepj'
|
||||||
version '1.1.0'
|
version '1.0'
|
||||||
|
|
||||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
||||||
|
|
||||||
|
@ -8,11 +8,11 @@ import java.net.http.HttpRequest;
|
|||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class Request {
|
public class DeepJ {
|
||||||
public Request() {
|
public DeepJ() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String get(String authKey, Enum<Languages> langEnum, String message) throws IOException, InterruptedException {
|
public String translate(String authKey, Enum<Languages> langEnum, String message) throws IOException, InterruptedException {
|
||||||
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
String lang;
|
String lang;
|
||||||
@ -43,4 +43,6 @@ public class Request {
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,36 +0,0 @@
|
|||||||
package tech.nevets.deepj;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Translator {
|
|
||||||
private final String authKey;
|
|
||||||
Request r = new Request();
|
|
||||||
|
|
||||||
public Translator(String authKey) {
|
|
||||||
this.authKey = authKey;
|
|
||||||
}
|
|
||||||
public String translate(Enum<Languages> lang, String sourceMessage) {
|
|
||||||
String response = "Error processing request";
|
|
||||||
try {
|
|
||||||
response = r.get(authKey, lang, sourceMessage);
|
|
||||||
} catch (IOException | InterruptedException e) {
|
|
||||||
System.out.println("Error processing request");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
String message = "Error Processing Request";
|
|
||||||
|
|
||||||
JSONObject jo = new JSONObject(response);
|
|
||||||
JSONArray ja = jo.getJSONArray("translations");
|
|
||||||
|
|
||||||
for (int i = 0; i < ja.length(); i++) {
|
|
||||||
JSONObject joo = ja.getJSONObject(i);
|
|
||||||
message = joo.getString("text");
|
|
||||||
}
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
}
|
|
37
src/main/java/tech/nevets/deepj/json/JsonExtractor.java
Normal file
37
src/main/java/tech/nevets/deepj/json/JsonExtractor.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package tech.nevets.deepj.json;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class JsonExtractor {
|
||||||
|
public JsonExtractor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public String extractMessage(String jsonResponse) {
|
||||||
|
String message = "Error Processing Request";
|
||||||
|
|
||||||
|
JSONObject jo = new JSONObject(jsonResponse);
|
||||||
|
JSONArray ja = jo.getJSONArray("translations");
|
||||||
|
|
||||||
|
for (int i = 0; i < ja.length(); i++) {
|
||||||
|
JSONObject joo = ja.getJSONObject(i);
|
||||||
|
message = joo.getString("text");
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String extractDetectedLang(String jsonResponse) {
|
||||||
|
String lang = "Error Processing Request";
|
||||||
|
|
||||||
|
JSONObject jo = new JSONObject(jsonResponse);
|
||||||
|
JSONArray ja = jo.getJSONArray("translations");
|
||||||
|
|
||||||
|
for (int i = 0; i < ja.length(); i++) {
|
||||||
|
JSONObject joo = ja.getJSONObject(i);
|
||||||
|
lang = joo.getString("detected_source_language");
|
||||||
|
}
|
||||||
|
|
||||||
|
return lang;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user