diff --git a/languagetool-language-modules/en/src/main/java/org/languagetool/rules/en/AbstractEnglishSpellerRule.java b/languagetool-language-modules/en/src/main/java/org/languagetool/rules/en/AbstractEnglishSpellerRule.java index 6ac0fdc0bad1..6617f71231ba 100644 --- a/languagetool-language-modules/en/src/main/java/org/languagetool/rules/en/AbstractEnglishSpellerRule.java +++ b/languagetool-language-modules/en/src/main/java/org/languagetool/rules/en/AbstractEnglishSpellerRule.java @@ -149,6 +149,30 @@ public AbstractEnglishSpellerRule(ResourceBundle messages, Language language) th this(messages, language, null, Collections.emptyList()); } + //CS304 Issue link: https://github.com/languagetool-org/languagetool/issues/4704 + /** + * Reorder the suggestions of English word. + * To match the firstUpperWord, and swap it with the word behind it according to its frequency. + * @param suggestions old suggestions order + * @param word target word + * @return reordered suggestions + */ + @Override + protected List orderSuggestions(List suggestions, String word) { + final int commonProperNounFrequency = 10; + String lowerWord = word.toLowerCase(); + String firstUpperWord = Character.toUpperCase(lowerWord.charAt(0)) + lowerWord.substring(1); + for (int i = 0; i < suggestions.size(); i++) { + if (i != suggestions.size()-1 && + firstUpperWord.equals(suggestions.get(i).getReplacement()) && + speller1.getFrequency(firstUpperWord) < commonProperNounFrequency) { + Collections.swap(suggestions, i, i+1); + break; + } + } + return suggestions; + } + @Override public RuleMatch[] match(AnalyzedSentence sentence) throws IOException { RuleMatch[] matches = super.match(sentence); diff --git a/languagetool-language-modules/en/src/test/java/org/languagetool/rules/en/AbstractEnglishSpellerRuleTest.java b/languagetool-language-modules/en/src/test/java/org/languagetool/rules/en/AbstractEnglishSpellerRuleTest.java index 0ab8fdbfcc56..a1c14ba46822 100644 --- a/languagetool-language-modules/en/src/test/java/org/languagetool/rules/en/AbstractEnglishSpellerRuleTest.java +++ b/languagetool-language-modules/en/src/test/java/org/languagetool/rules/en/AbstractEnglishSpellerRuleTest.java @@ -26,8 +26,10 @@ import java.io.IOException; import java.util.List; +import org.junit.Test; import org.languagetool.JLanguageTool; import org.languagetool.Language; +import org.languagetool.language.BritishEnglish; import org.languagetool.rules.Rule; import org.languagetool.rules.RuleMatch; @@ -122,4 +124,37 @@ protected void assertAllMatches(JLanguageTool lt, Rule rule, String text, String i++; } } + + + //CS304 (manually written) Issue link: https://github.com/languagetool-org/languagetool/issues/4704 + @Test + public void testAbstractEnglishSpellerSuggestionOrder1() throws IOException { + JLanguageTool lt = new JLanguageTool(new BritishEnglish()); + // comment in to use statistical ngram data: + //lt.activateLanguageModelRules(new File("/data/google-ngram-data")); + List matches = lt.check("I want to handel it."); + for (RuleMatch match : matches) { + System.out.println("Potential error at characters " + + match.getFromPos() + "-" + match.getToPos() + ": " + + match.getMessage()); + System.out.println("Suggested correction(s): " + + match.getSuggestedReplacements()); + } + } + + //CS304 (manually written) Issue link: https://github.com/languagetool-org/languagetool/issues/4704 + @Test + public void testAbstractEnglishSpellerSuggestionOrder2() throws IOException { + JLanguageTool lt = new JLanguageTool(new BritishEnglish()); + // comment in to use statistical ngram data: + //lt.activateLanguageModelRules(new File("/data/google-ngram-data")); + List matches = lt.check("Each monday is hard!"); + for (RuleMatch match : matches) { + System.out.println("Potential error at characters " + + match.getFromPos() + "-" + match.getToPos() + ": " + + match.getMessage()); + System.out.println("Suggested correction(s): " + + match.getSuggestedReplacements()); + } + } }