Skip to content

Commit

Permalink
[en] small code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Mar 31, 2015
1 parent 4e884a8 commit 1b8f961
Showing 1 changed file with 29 additions and 20 deletions.
Expand Up @@ -18,6 +18,7 @@
*/
package org.languagetool.rules.en;

import org.jetbrains.annotations.Nullable;
import org.languagetool.AnalyzedSentence;
import org.languagetool.AnalyzedTokenReadings;
import org.languagetool.rules.Category;
Expand Down Expand Up @@ -79,7 +80,7 @@ public RuleMatch[] match(final AnalyzedSentence sentence) {
if (parts.length >= 1 && !parts[0].equalsIgnoreCase("a")) { // avoid false alarm on "A-levels are..."
token = parts[0];
}
if (tokens[i].isWhitespaceBefore() || !"-".equals(token)) { //e.g., 'a- or anti- are prefixes'
if (tokens[i].isWhitespaceBefore() || !"-".equals(token)) { // e.g., 'a- or anti- are prefixes'
token = token.replaceAll("[^αa-zA-Z0-9\\.;,:']", ""); // e.g. >>an "industry party"<<
if (StringTools.isEmpty(token)) {
continue;
Expand Down Expand Up @@ -114,25 +115,9 @@ public RuleMatch[] match(final AnalyzedSentence sentence) {
doesRequireA = true;
}
}
String msg = null;
if (prevToken.equalsIgnoreCase("a") && doesRequireAn) {
String replacement = "an";
if (prevToken.equals("A")) {
replacement = "An";
}
msg = "Use <suggestion>" + replacement + "</suggestion> instead of '" + prevToken + "' if the following "+
"word starts with a vowel sound, e.g. 'an article', 'an hour'";
} else if (prevToken.equalsIgnoreCase("an") && doesRequireA) {
String replacement = "a";
if (prevToken.equals("An")) {
replacement = "A";
}
msg = "Use <suggestion>" + replacement + "</suggestion> instead of '" + prevToken + "' if the following "+
"word doesn't start with a vowel sound, e.g. 'a sentence', 'a university'";
}
if (msg != null) {
final RuleMatch ruleMatch = new RuleMatch(this, prevPos, prevPos + prevToken.length(), msg, "Wrong article");
ruleMatches.add(ruleMatch);
RuleMatch match = getRuleMatch(prevToken, prevPos, doesRequireA, doesRequireAn);
if (match != null) {
ruleMatches.add(match);
}
if (tokens[i].hasPosTag("DT")) {
prevToken = token;
Expand All @@ -144,6 +129,30 @@ public RuleMatch[] match(final AnalyzedSentence sentence) {
return toRuleMatchArray(ruleMatches);
}

@Nullable
private RuleMatch getRuleMatch(String prevToken, int prevPos, boolean doesRequireA, boolean doesRequireAn) {
String msg = null;
if (prevToken.equalsIgnoreCase("a") && doesRequireAn) {
String replacement = "an";
if (prevToken.equals("A")) {
replacement = "An";
}
msg = "Use <suggestion>" + replacement + "</suggestion> instead of '" + prevToken + "' if the following "+
"word starts with a vowel sound, e.g. 'an article', 'an hour'";
} else if (prevToken.equalsIgnoreCase("an") && doesRequireA) {
String replacement = "a";
if (prevToken.equals("An")) {
replacement = "A";
}
msg = "Use <suggestion>" + replacement + "</suggestion> instead of '" + prevToken + "' if the following "+
"word doesn't start with a vowel sound, e.g. 'a sentence', 'a university'";
}
if (msg != null) {
return new RuleMatch(this, prevPos, prevPos + prevToken.length(), msg, "Wrong article");
}
return null;
}

/**
* Adds "a" or "an" to the English noun. Used for suggesting the proper form of the indefinite article.
* @param noun Word that needs an article.
Expand Down

0 comments on commit 1b8f961

Please sign in to comment.