Skip to content

Commit

Permalink
some small code cleanup, e.g. to make more values final/immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaber committed Feb 14, 2015
1 parent a46d551 commit bcc26b5
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 40 deletions.
Expand Up @@ -22,9 +22,8 @@
/** /**
* A convenience class to work with bitext strings. * A convenience class to work with bitext strings.
* @author Marcin Miłkowski * @author Marcin Miłkowski
*
*/ */
public class StringPair { public final class StringPair {


private final String sourceString; private final String sourceString;
private final String targetString; private final String targetString;
Expand Down
Expand Up @@ -21,6 +21,7 @@


import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;


import org.languagetool.AnalyzedSentence; import org.languagetool.AnalyzedSentence;
Expand All @@ -40,28 +41,30 @@
public abstract class BitextRule extends Rule { public abstract class BitextRule extends Rule {


public static List<Class<? extends BitextRule>> getRelevantRules() { public static List<Class<? extends BitextRule>> getRelevantRules() {
return Arrays.asList(DifferentLengthRule.class, SameTranslationRule.class, return Arrays.asList(
DifferentPunctuationRule.class); DifferentLengthRule.class,
SameTranslationRule.class,
DifferentPunctuationRule.class
);
} }


private List<StringPair> correctExamples;
private List<IncorrectBitextExample> incorrectExamples;

private Language sourceLanguage;

@Override @Override
public abstract String getDescription(); public abstract String getDescription();


public abstract String getMessage();

@Override @Override
public abstract String getId(); public abstract String getId();


@Override
public abstract void reset();

public abstract String getMessage();

public abstract RuleMatch[] match(AnalyzedSentence sourceText, public abstract RuleMatch[] match(AnalyzedSentence sourceText,
AnalyzedSentence targetText) throws IOException; AnalyzedSentence targetText) throws IOException;


@Override private List<StringPair> correctExamples;
public abstract void reset(); private List<IncorrectBitextExample> incorrectExamples;
private Language sourceLanguage;


/** /**
* This method makes no sense for bitext, thus it always returns {@code null}. * This method makes no sense for bitext, thus it always returns {@code null}.
Expand Down Expand Up @@ -103,7 +106,7 @@ public final List<StringPair> getCorrectBitextExamples() {
*/ */
public final void setIncorrectBitextExamples( public final void setIncorrectBitextExamples(
final List<IncorrectBitextExample> incorrectExamples) { final List<IncorrectBitextExample> incorrectExamples) {
this.incorrectExamples = incorrectExamples; this.incorrectExamples = Collections.unmodifiableList(incorrectExamples);
} }


/** /**
Expand Down
Expand Up @@ -19,6 +19,7 @@
package org.languagetool.rules.bitext; package org.languagetool.rules.bitext;


import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;


import org.languagetool.bitext.StringPair; import org.languagetool.bitext.StringPair;
Expand All @@ -35,14 +36,25 @@ public class IncorrectBitextExample {
private final List<String> corrections; private final List<String> corrections;


public IncorrectBitextExample(final StringPair example) { public IncorrectBitextExample(final StringPair example) {
this(example, null); this(example, (List<String>)null);
} }


/**
* @deprecated use {@link #IncorrectBitextExample(org.languagetool.bitext.StringPair, List<String>)} instead (deprecated since 2.9)
*/
public IncorrectBitextExample(final StringPair example, final String[] corrections) { public IncorrectBitextExample(final StringPair example, final String[] corrections) {
this.example = example; this.example = example;
this.corrections = Arrays.asList(corrections); this.corrections = Collections.unmodifiableList(Arrays.asList(corrections));
} }


/**
* @since 2.9
*/
public IncorrectBitextExample(final StringPair example, final List<String> corrections) {
this.example = example;
this.corrections = Collections.unmodifiableList(corrections);
}

/** /**
* Return the example that contains the error. * Return the example that contains the error.
*/ */
Expand Down
Expand Up @@ -34,6 +34,7 @@
public class ElementMatcher { public class ElementMatcher {


private final Element baseElement; private final Element baseElement;

private Element element; private Element element;
private List<ElementMatcher> andGroup; private List<ElementMatcher> andGroup;
private boolean[] andGroupCheck; private boolean[] andGroupCheck;
Expand Down
Expand Up @@ -43,24 +43,21 @@ public enum IncludeRange {
} }


private final String posTag; private final String posTag;
private boolean postagRegexp;
private final boolean suppressMisspelled; private final boolean suppressMisspelled;
private final String regexReplace; private final String regexReplace;
private final String posTagReplace; private final String posTagReplace;
private final CaseConversion caseConversionType; private final CaseConversion caseConversionType;
private final IncludeRange includeSkipped; private final IncludeRange includeSkipped;


/** private boolean postagRegexp;
* True if this match element formats a statically defined lemma which is
* enclosed by the element, e.g., <tt>&lt;match...&gt;word&lt;/match&gt;</tt>. // True if this match element formats a statically defined lemma which is
*/ // enclosed by the element, e.g., <match...>word</match>:
private boolean staticLemma; private boolean staticLemma;


private String lemma; private String lemma;


/** // True if this match element is used for formatting POS token:
* True if this match element is used for formatting POS token.
*/
private final boolean setPos; private final boolean setPos;


private int tokenRef; private int tokenRef;
Expand Down
Expand Up @@ -80,6 +80,7 @@ public class XMLRuleHandler extends DefaultHandler {
protected static final String MATCH = "match"; protected static final String MATCH = "match";
protected static final String UNIFICATION = "unification"; protected static final String UNIFICATION = "unification";
protected static final String RULE = "rule"; protected static final String RULE = "rule";
protected static final String RULES = "rules";
protected static final String RULEGROUP = "rulegroup"; protected static final String RULEGROUP = "rulegroup";
protected static final String NO = "no"; protected static final String NO = "no";
protected static final String PHRASES = "phrases"; protected static final String PHRASES = "phrases";
Expand Down
Expand Up @@ -65,6 +65,11 @@ public final List<BitextPatternRule> getRules(final InputStream is,


class BitextPatternRuleHandler extends PatternRuleHandler { class BitextPatternRuleHandler extends PatternRuleHandler {


private static final String SOURCE = "source";
private static final String TARGET = "target";
private static final String SRC_EXAMPLE = "srcExample";
private static final String TRG_EXAMPLE = "trgExample";

private PatternRule srcRule; private PatternRule srcRule;
private PatternRule trgRule; private PatternRule trgRule;


Expand All @@ -89,19 +94,19 @@ List<BitextPatternRule> getBitextRules() {
public void startElement(final String namespaceURI, final String lName, public void startElement(final String namespaceURI, final String lName,
final String qName, final Attributes attrs) throws SAXException { final String qName, final Attributes attrs) throws SAXException {
switch (qName) { switch (qName) {
case "rules": case RULES:
final String languageStr = attrs.getValue("targetLang"); final String languageStr = attrs.getValue("targetLang");
language = Language.getLanguageForShortName(languageStr); language = Language.getLanguageForShortName(languageStr);
break; break;
case "rule": case RULE:
super.startElement(namespaceURI, lName, qName, attrs); super.startElement(namespaceURI, lName, qName, attrs);
correctExamples = new ArrayList<>(); correctExamples = new ArrayList<>();
incorrectExamples = new ArrayList<>(); incorrectExamples = new ArrayList<>();
break; break;
case "target": case TARGET:
startPattern(attrs); startPattern(attrs);
break; break;
case "source": case SOURCE:
srcLang = Language.getLanguageForShortName(attrs.getValue("lang")); srcLang = Language.getLanguageForShortName(attrs.getValue("lang"));
break; break;
default: default:
Expand All @@ -114,7 +119,7 @@ public void startElement(final String namespaceURI, final String lName,
public void endElement(final String namespaceURI, final String sName, public void endElement(final String namespaceURI, final String sName,
final String qName) throws SAXException { final String qName) throws SAXException {
switch (qName) { switch (qName) {
case "rule": case RULE:
trgRule.setMessage(message.toString()); trgRule.setMessage(message.toString());
if (suggestionMatches != null) { if (suggestionMatches != null) {
for (final Match m : suggestionMatches) { for (final Match m : suggestionMatches) {
Expand All @@ -130,19 +135,19 @@ public void endElement(final String namespaceURI, final String sName,
bRule.setSourceLang(srcLang); bRule.setSourceLang(srcLang);
rules.add(bRule); rules.add(bRule);
break; break;
case "trgExample": case SRC_EXAMPLE:
trgExample = setExample();
break;
case "srcExample":
srcExample = setExample(); srcExample = setExample();
break; break;
case "source": case TRG_EXAMPLE:
trgExample = setExample();
break;
case SOURCE:
srcRule = finalizeRule(); srcRule = finalizeRule();
break; break;
case "target": case TARGET:
trgRule = finalizeRule(); trgRule = finalizeRule();
break; break;
case "example": case EXAMPLE:
if (inCorrectExample) { if (inCorrectExample) {
correctExamples.add(new StringPair(srcExample.getExample(), trgExample.getExample())); correctExamples.add(new StringPair(srcExample.getExample(), trgExample.getExample()));
} else if (inIncorrectExample) { } else if (inIncorrectExample) {
Expand All @@ -151,8 +156,7 @@ public void endElement(final String namespaceURI, final String sName,
incorrectExamples.add(new IncorrectBitextExample(examplePair)); incorrectExamples.add(new IncorrectBitextExample(examplePair));
} else { } else {
final List<String> corrections = trgExample.getCorrections(); final List<String> corrections = trgExample.getCorrections();
final String[] correctionArray = corrections.toArray(new String[corrections.size()]); incorrectExamples.add(new IncorrectBitextExample(examplePair, corrections));
incorrectExamples.add(new IncorrectBitextExample(examplePair, correctionArray));
} }
} }
inCorrectExample = false; inCorrectExample = false;
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;


import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;


Expand Down Expand Up @@ -53,7 +54,7 @@ public List<BitextPatternRule> getFalseFriendsAsBitext(
final List<PatternRule> rules2 = ruleLoader.getRules(JLanguageTool final List<PatternRule> rules2 = ruleLoader.getRules(JLanguageTool
.getDataBroker().getFromRulesDirAsStream(filename), .getDataBroker().getFromRulesDirAsStream(filename),
language, motherTongue); language, motherTongue);
final HashMap<String, PatternRule> srcRules = new HashMap<>(); final Map<String, PatternRule> srcRules = new HashMap<>();
for (PatternRule rule : rules1) { for (PatternRule rule : rules1) {
srcRules.put(rule.getId(), rule); srcRules.put(rule.getId(), rule);
} }
Expand Down

0 comments on commit bcc26b5

Please sign in to comment.