Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<SuggestedReplacement> orderSuggestions(List<SuggestedReplacement> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<RuleMatch> 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<RuleMatch> 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());
}
}
}