Jlanguage does not implement the requested interface java.util.function.Supplier

1k views Asked by At

CODE: Same code with out Microservice(Run as main class) giving correct output but after making it @service class(converting into microservice), it fails. I still do not know which supplier I have to implement. getSuggestedReplacements() has supplier which is already implemented.

import org.languagetool.JLanguageTool;
import org.languagetool.language.AmericanEnglish;
import org.languagetool.rules.CategoryId;
import org.languagetool.rules.RuleMatch;
import org.languagetool.tools.Tools;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.*;

@Service
public class JlanguageSpellCheckerService {  
    public Map<String, String> jlanguageSpellService(String inputString) throws IOException {
        Map<String, String> response = new HashMap<>();
        JLanguageTool jLanguageTool = new JLanguageTool(new AmericanEnglish());

        Set<CategoryId> categorySet = new LinkedHashSet<>();
        categorySet.add(new CategoryId("CASING"));
        categorySet.add(new CategoryId("TYPOGRAPHY"));
        Tools.selectRules(jLanguageTool, categorySet, Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), false, false);

        List<RuleMatch> matches = jLanguageTool.check(inputString);
        String check = correctTextFromMatches(inputString,matches);
        response.put("InputString", inputString);
        return response;
    }
 
    public static String correctTextFromMatches(String contents, List<RuleMatch> matches) {
        StringBuilder sb = new StringBuilder(contents);
        List<String> errors = new ArrayList<>();
        for (RuleMatch rm : matches) {
            List<String> replacements = rm.getSuggestedReplacements();
            if (!replacements.isEmpty()) {
                errors.add(sb.substring(rm.getFromPos(), rm.getToPos()));
            }
        }
        int offset = 0;
        int counter = 0;
        for (RuleMatch rm : matches) {
            List<String> replacements = rm.getSuggestedReplacements();
            System.out.println("replacements ::"+ replacements);
            if (!replacements.isEmpty()) {
                if (rm.getFromPos()-offset >= 0 &&
                        rm.getToPos()-offset >= rm.getFromPos()-offset &&
                        errors.get(counter).equals(sb.substring(rm.getFromPos() - offset, rm.getToPos() - offset))) {
                    sb.replace(rm.getFromPos() - offset, rm.getToPos() - offset, replacements.get(0));
                    offset += rm.getToPos() - rm.getFromPos() - replacements.get(0).length();
                }
                counter++;
            }
        }
        return sb.toString();
    }
}

ERROR- It is throwing error at Supplier Interface. In this code no supplier has invoked explicitly.

2022-03-22 21:42:22.767 ERROR 22612 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.IncompatibleClassChangeError: Class com.google.common.base.Suppliers$NonSerializableMemoizingSupplier does not implement the requested interface java.util.function.Supplier] with root cause
    java.lang.IncompatibleClassChangeError: Class com.google.common.base.Suppliers$NonSerializableMemoizingSupplier does not implement the requested interface java.util.function.Supplier
        at org.languagetool.rules.en.EnglishRepeatedWordsRule.getAntiPatterns(EnglishRepeatedWordsRule.java:173) ~[language-en-5.6.jar:na]
        at org.languagetool.rules.Rule.getSentenceWithImmunization(Rule.java:196) ~[languagetool-core-5.6.jar:na]
        at org.languagetool.rules.AbstractRepeatedWordsRule.match(AbstractRepeatedWordsRule.java:92) ~[languagetool-core-5.6.jar:na]
        at org.languagetool.rules.TextLevelRule.match(TextLevelRule.java:39) ~[languagetool-core-5.6.jar:na]
        at org.languagetool.JLanguageTool$TextCheckCallable.getTextLevelRuleMatches(JLanguageTool.java:1929) ~[languagetool-core-5.6.jar:na]
1

There are 1 answers

2
Yashika Chandra On

For me after adding the guava dependency its working fine

<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>21.0</version>
</dependency>