11package reverso ;
22
3+ import com .google .gson .Gson ;
4+ import com .google .gson .GsonBuilder ;
35import org .jsoup .Connection ;
46import org .jsoup .Jsoup ;
57import org .jsoup .nodes .Document ;
68import reverso .data .parser .HtmlParser ;
9+ import reverso .data .request .SpellCheckRequest ;
710import reverso .data .response .impl .*;
811import reverso .supportedLanguages .Language ;
912import 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 {
0 commit comments