Skip to content

Commit

Permalink
Merge pull request #9526 from evan-defran-lt/ed-1
Browse files Browse the repository at this point in the history
[en] hyphenated compounds
  • Loading branch information
evan-defran-lt authored Nov 17, 2023
2 parents bbe278d + 714c364 commit 35cd47c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,36 @@ protected List<SuggestedReplacement> getAdditionalTopSuggestions(List<SuggestedR
return super.getAdditionalTopSuggestions(suggestions, word);
}

// trivial approach that assumes only one part of the hyphenated word has a typo...
private void addHyphenSuggestions(String[] parts, List<SuggestedReplacement> topSuggestions) throws IOException {
int i = 0;
for (String part : parts) {
if (isMisspelled(part)) {
List<String> partSuggestions = speller1.getSuggestions(part);
if (partSuggestions.size() == 0) {
partSuggestions = speller2.getSuggestions(part);
}
if (partSuggestions.size() > 0) {
String suggestion = getHyphenatedWordSuggestion(parts, i, partSuggestions.get(0));
topSuggestions.add(new SuggestedReplacement(suggestion));
}
}
i++;
}
}

private String getHyphenatedWordSuggestion(String[] parts, int currentPos, String currentPostSuggestion) {
List<String> newParts = new ArrayList<>();
for (int j = 0; j < parts.length; j++) {
if (currentPos == j) {
newParts.add(currentPostSuggestion);
} else {
newParts.add(parts[j]);
}
}
return String.join("-", newParts);
}

@Override
protected Translator getTranslator(GlobalConfig globalConfig) {
return translator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ protected List<SuggestedReplacement> getAdditionalTopSuggestions(List<SuggestedR
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ public void testNonVariantSpecificSuggestions(Rule rule, Language language) thro
// http://grammar.yourdictionary.com/spelling-and-word-lists/misspelled.html
// https://en.wikipedia.org/wiki/Commonly_misspelled_English_words#cite_note-YD-4
}
public void testHyphenatedWordSuggestions(Rule rule, Language language) throws IOException {
this.lt = new JLanguageTool(language);
this.rule = rule;

assertFirstMatch("long-trem", "long-term");
assertFirstMatch("self-sefficient", "self-sufficient");
assertFirstMatch("parple-people-eater", "purple-people-eater");
}
private void assertFirstMatch(String text, String... expectedSuggestions) throws IOException {
RuleMatch[] matches = rule.match(lt.getAnalyzedSentence(text));
assertTrue("Expected 1 match for '" + text + "', got " + matches.length, matches.length == 1);
Expand Down

0 comments on commit 35cd47c

Please sign in to comment.