Skip to content

Commit 64d961b

Browse files
committed
new feature(SpellCheck), Language structure changed, and much more
1 parent d4d8bfa commit 64d961b

File tree

15 files changed

+357
-86
lines changed

15 files changed

+357
-86
lines changed

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![logotype](https://i.imgur.com/Mxbpi3N.jpeg)
44

55
Java API wrapper to perform contextual translations,
6-
find synonyms, voice text, conjugate verbs, and much more.
6+
find synonyms, voice sourceText, conjugate verbs, and much more.
77

88
Unfortunately, complete information about the
99
services used by Reverso is not available, though some are known.
@@ -34,7 +34,7 @@ public abstract class Response {
3434

3535
private String sourceLanguage;
3636

37-
private String text;
37+
private String sourceText;
3838

3939
private String errorMessage;
4040

@@ -55,30 +55,29 @@ translation or synonym search. It is represented as an **enum** with a
5555
list of all supported languages.
5656
```java
5757
public enum Language {
58-
ARABIC("arabic", "ar", "ara", true),
59-
GERMAN("german", "de", "ger", true),
60-
ENGLISH("english", "en", "eng", true);
58+
ARABIC("ar", true, null),
59+
GERMAN("de", true, null),
60+
ENGLISH("en", true, "eng"),
6161
}
6262
```
6363
Each object in this enumeration has fields that help determine whether
6464
a specific language is supported for a given functionality. The fields
6565
are arranged in the following sequence:
6666

67-
1) `String fullName` <br>
68-
2) `String synonymName` <br>
69-
3) `String translateName` <br>
70-
4) `boolean conjugate`
67+
1) `String synonymName` <br>
68+
2) `boolean isConjugate` <br>
69+
3) `String spellCheckName`
7170

7271
If any of these fields are false or null, it indicates that the language
7372
does not support the respective functionality.
7473

7574
For example:
7675
```java
77-
KOREAN("korean", null, "kor", false);
76+
JAPANESE("ja", true, null);
7877
```
79-
Since the second and fourth fields are **null** and **false**,
80-
respectively, this means that **synonym** search and verb
81-
**conjugation** are not available for *korean* language.
78+
79+
The third field is null, this means that **spellCheck** are not available
80+
for *japanese* language.
8281

8382
## Usage
8483
The fundamental class, **Reverso**, manages all interactions with the API.
@@ -111,7 +110,7 @@ Here's the output :
111110
{
112111
"isOK": true,
113112
"sourceLanguage": "english",
114-
"text": "world",
113+
"sourceText": "world",
115114
"errorMessage": "null",
116115
"synonyms": {
117116
"Noun": [
@@ -130,7 +129,7 @@ arguments, such as the translation method, which is logical.
130129
## getVoiceStream
131130
However, there is a method that differs slightly from the others:
132131

133-
`VoiceResponse getVoiceStream(Voice voice, String text)`
132+
`VoiceResponse getVoiceStream(Voice voice, String sourceText)`
134133

135134
For this method, you should pass one of the objects from the Voice enum.
136135
The names of the objects and their fields can tell us the language they
@@ -145,7 +144,7 @@ GERMAN_ANDREAS("Andreas22k", "German", "M"),
145144
}
146145
```
147146
Among other information, the VoiceResponse contains a byte array that
148-
stores the audio file, which voices the required text. To get the audio
147+
stores the audio file, which voices the required sourceText. To get the audio
149148
data, you can use the following code:
150149
```java
151150
Reverso reverso = new Reverso();
@@ -169,7 +168,7 @@ Here's an example of a successful response:
169168
{
170169
"isOK": true,
171170
"sourceLanguage": "english",
172-
"text": "hello",
171+
"sourceText": "hello",
173172
"targetLanguage": "russian",
174173
"translation": "привет",
175174
"contextTranslations": {
@@ -211,7 +210,7 @@ And console output:
211210
"isOK": false,
212211
"errorMessage": "Synonyms cannot be obtained for this language. A list of available languages can be found here: https://synonyms.reverso.net/synonym/",
213212
"sourceLanguage": "swedish",
214-
"text": "Skön"
213+
"sourceText": "Skön"
215214
}
216215
```
217216
## Credits

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<name>Reverso API</name>
1313
<description>Utilize our API wrapper to perform contextual translations,
14-
find synonyms, voice text, conjugate verbs, and much more.</description>
14+
find synonyms, voice sourceText, conjugate verbs, and much more.</description>
1515
<url>https://github.com/Anton3413/Reverso-Java-API</url>
1616

1717
<licenses>

src/main/java/reverso/Main.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package reverso;
22

33

4-
4+
import reverso.data.response.impl.SpellCheckResponse;
5+
import reverso.supportedLanguages.Language;
56

67
public class Main {
78
public static void main(String[] args) {
89
Reverso reverso = new Reverso();
910

10-
reverso.getSpellCheck();
11+
SpellCheckResponse response = reverso.getSpellCheck(Language.ITALIAN,"here ve have errores, we shuld to fix it");
12+
13+
System.out.println(response.toJson());
1114

15+
System.out.println(response.getStats().toJson());
1216
}
1317
}

src/main/java/reverso/Reverso.java

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package reverso;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
35
import org.jsoup.Connection;
46
import org.jsoup.Jsoup;
57
import org.jsoup.nodes.Document;
68
import reverso.data.parser.HtmlParser;
9+
import reverso.data.request.SpellCheckRequest;
710
import reverso.data.response.impl.*;
811
import reverso.supportedLanguages.Language;
912
import reverso.supportedLanguages.Voice;
@@ -17,6 +20,7 @@ public class Reverso {
1720
private static final String CONTEXT_URL = "https://context.reverso.net/translation/";
1821
private static final String VOICE_URL = "https://voice.reverso.net/RestPronunciation.svc/v1/output=json/GetVoiceStream/";
1922
private static final String CONJUGATION_URL = "https://conjugator.reverso.net/conjugation";
23+
private static final String SPELLCHECK_URL = "https://orthographe.reverso.net/api/v1/Spelling";
2024

2125
{
2226
initializeProperties();
@@ -26,7 +30,7 @@ public class Reverso {
2630
public SynonymResponse getSynonyms(Language language, String word) {
2731
if (language.getSynonymName() == null) {
2832
String errorMessage = properties.getProperty("message.error.synonym.unSupportedLanguage");
29-
return new SynonymResponse(false, errorMessage, language.getFullName(), word);
33+
return new SynonymResponse(false, errorMessage, language.toString(), word);
3034
}
3135
String URL = SYNONYM_URL + language.getSynonymName() + "/" + word;
3236

@@ -38,30 +42,30 @@ public SynonymResponse getSynonyms(Language language, String word) {
3842
.execute();
3943
} catch (IOException e) {
4044
String errorMessage = properties.getProperty("message.error.connection");
41-
return new SynonymResponse(false, errorMessage, language.getFullName(), word);
45+
return new SynonymResponse(false, errorMessage, language.toString(), word);
4246
}
4347
if (response.statusCode() == 404) {
4448
String errorMessage = properties.getProperty("message.error.synonym.noResults");
45-
return new SynonymResponse(false, errorMessage, language.getFullName(), word);
49+
return new SynonymResponse(false, errorMessage, language.toString(), word);
4650
}
4751
synonymsMap = parser.parseSynonymsPage(response);
4852
if (synonymsMap.isEmpty()) {
4953
String message = properties.getProperty("message.error.synonym.noResults");
50-
return new SynonymResponse(false, message, language.getFullName(), word);
54+
return new SynonymResponse(false, message, language.toString(), word);
5155
}
52-
return new SynonymResponse(true, language.getFullName(), word, synonymsMap);
56+
return new SynonymResponse(true, language.toString(), word, synonymsMap);
5357
}
5458

5559
public ContextResponse getContext(Language sourceLanguage, Language targetLanguage, String word) {
5660

57-
ContextResponse contextResponse = new ContextResponse(false, null, sourceLanguage.getFullName(),
58-
targetLanguage.getFullName(), word);
61+
ContextResponse contextResponse = new ContextResponse(false, null, sourceLanguage.toString(),
62+
targetLanguage.toString(), word);
5963

6064
if (sourceLanguage.equals(targetLanguage)) {
6165
contextResponse.setErrorMessage(properties.getProperty("message.error.context.sameLanguage"));
6266
return contextResponse;
6367
}
64-
String URL = CONTEXT_URL + sourceLanguage.getFullName() + "-" + targetLanguage.getFullName() + "/" + word;
68+
String URL = CONTEXT_URL + sourceLanguage.toString() + "-" + targetLanguage.toString() + "/" + word;
6569

6670
Document document;
6771
Map<String, String> contextMap;
@@ -127,13 +131,13 @@ public VoiceResponse getVoiceStream(Voice voice, String text) {
127131

128132
public ConjugationResponse getWordConjugation(Language language, String word) {
129133

130-
ConjugationResponse conjugationResponse = new ConjugationResponse(false, null, language.getFullName(), word);
134+
ConjugationResponse conjugationResponse = new ConjugationResponse(false, null, language.toString(), word);
131135

132136
if (!language.isConjugate()) {
133137
conjugationResponse.setErrorMessage(properties.getProperty("message.error.conjugation.invalidLanguage"));
134138
return conjugationResponse;
135139
}
136-
String URL = CONJUGATION_URL + "-" + language.getFullName() + "-" + "verb" + "-" + word + ".html";
140+
String URL = CONJUGATION_URL + "-" + language.toString() + "-" + "verb" + "-" + word + ".html";
137141

138142
Map<String, String[]> conjugationData;
139143
Connection.Response response;
@@ -156,6 +160,44 @@ public ConjugationResponse getWordConjugation(Language language, String word) {
156160
conjugationResponse.setOK(true);
157161
return conjugationResponse;
158162
}
163+
public SpellCheckResponse getSpellCheck(Language language, String text) {
164+
165+
if (language.getSpellCheckName() == null) {
166+
return new SpellCheckResponse(false, properties.getProperty("message.error.spellCheck.unsupportedLanguage"),
167+
language.toString(), text);
168+
}
169+
170+
String requestJson = SpellCheckRequest.builder().withLanguage(language.getSpellCheckName())
171+
.withText(text)
172+
.withCorrectionDetails(false)
173+
.toJson();
174+
175+
Connection.Response response;
176+
String responseData;
177+
try {
178+
response = Jsoup.connect(SPELLCHECK_URL)
179+
.header("Content-Type", "application/json")
180+
.ignoreContentType(true)
181+
.ignoreHttpErrors(true)
182+
.requestBody(requestJson)
183+
.method(Connection.Method.POST)
184+
.execute();
185+
responseData = response.body();
186+
} catch (IOException e) {
187+
return new SpellCheckResponse(false, properties.getProperty("message.error.connection"), language.toString(), text);
188+
}
189+
SpellCheckResponse spellCheckResponse = new Gson().fromJson(responseData, SpellCheckResponse.class);
190+
191+
if (spellCheckResponse.getCorrectedText().equals(text)) {
192+
return new SpellCheckResponse(false, properties.getProperty("message.error.spellCheck.noErrorsOrMismatchedLanguage"),
193+
language.toString(), text);
194+
}
195+
spellCheckResponse.setOK(true);
196+
spellCheckResponse.setSourceText(text);
197+
spellCheckResponse.setSourceLanguage(language.toString());
198+
199+
return spellCheckResponse;
200+
}
159201

160202
private void initializeProperties() {
161203
try {

src/main/java/reverso/data/parser/HtmlParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ private String extractTextWithEmphasis(Element element) {
9393
if (childElement.tagName().equals("em")) {
9494
sb.append("<em>").append(childElement.html()).append("</em>");
9595
} else {
96-
// Recursively handle nested elements
9796
sb.append(extractTextWithEmphasis(childElement));
9897
}
9998
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package reverso.data.request;
2+
3+
import com.google.gson.GsonBuilder;
4+
5+
public class SpellCheckRequest {
6+
private String language;
7+
private boolean correctionDetails;
8+
private String origin;
9+
private String text;
10+
11+
{
12+
this.origin = "interactive";
13+
}
14+
15+
private SpellCheckRequest() {
16+
}
17+
18+
19+
public String getLanguage() {
20+
return language;
21+
}
22+
23+
public String toJson() {
24+
return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(this);
25+
}
26+
27+
public static SpellCheckRequest builder() {
28+
return new SpellCheckRequest();
29+
}
30+
31+
public SpellCheckRequest withLanguage(String language) {
32+
this.language = language;
33+
return this;
34+
}
35+
36+
public SpellCheckRequest withCorrectionDetails(boolean correctionDetails) {
37+
this.correctionDetails= correctionDetails;
38+
return this;
39+
}
40+
41+
public SpellCheckRequest withOrigin(String origin) {
42+
this.origin = origin;
43+
return this;
44+
}
45+
46+
public SpellCheckRequest withText(String text) {
47+
this.text = text;
48+
return this;
49+
}
50+
}

src/main/java/reverso/data/response/Response.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,30 @@ public abstract class Response {
1111

1212
private String sourceLanguage;
1313

14-
private String text;
14+
private String sourceText;
1515

1616
private String errorMessage;
1717

18-
public Response(boolean isOK, String errorMessage, String sourceLanguage, String text) {
18+
public Response(boolean isOK, String errorMessage, String sourceLanguage, String sourceText) {
1919
this.isOK = isOK;
2020
this.errorMessage = errorMessage;
2121
this.sourceLanguage = sourceLanguage;
22-
this.text = text;
22+
this.sourceText = sourceText;
2323
}
2424

25-
public Response(boolean isOK, String sourceLanguage, String text) {
25+
public Response(boolean isOK, String sourceLanguage, String sourceText) {
2626
this.isOK = isOK;
2727
this.sourceLanguage = sourceLanguage;
28-
this.text = text;
28+
this.sourceText = sourceText;
2929
this.errorMessage=null;
3030
}
31+
3132
public String toJson() {
3233
Map<String, Object> jsonMap = new LinkedHashMap<>();
3334
jsonMap.put("isOK", isOK());
3435
jsonMap.put("errorMessage", getErrorMessage());
3536
jsonMap.put("sourceLanguage", getSourceLanguage());
36-
jsonMap.put("text", getText());
37+
jsonMap.put("sourceText", getSourceText());
3738
addCustomFields(jsonMap);
3839
return new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(jsonMap);
3940
}
@@ -56,12 +57,12 @@ public void setErrorMessage(String errorMessage) {
5657
this.errorMessage = errorMessage;
5758
}
5859

59-
public String getText() {
60-
return text;
60+
public String getSourceText() {
61+
return sourceText;
6162
}
6263

63-
public void setText(String text) {
64-
this.text = text;
64+
public void setSourceText(String sourceText) {
65+
this.sourceText = sourceText;
6566
}
6667

6768
public String getSourceLanguage() {
@@ -77,7 +78,7 @@ public String toString() {
7778
return "{\n" +
7879
" \"isOK\": " + isOK + ",\n" +
7980
" \"sourceLanguage\": \"" + sourceLanguage + "\",\n" +
80-
" \"text\": \"" + text + "\",\n" +
81+
" \"sourceText\": \"" + sourceText + "\",\n" +
8182
" \"errorMessage\": \"" + errorMessage + "\"\n" +
8283
"}";
8384
}

0 commit comments

Comments
 (0)