Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JUnit Testcases to the languagetool repo #9648

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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 @@ -38,6 +38,8 @@ public class FastTextDetector {
private static final int BUFFER_SIZE = 4096;
private static final Pattern WHITESPACE = Pattern.compile("\\s+");

private static final int READ_RETRY_DELAY = 10;

private Process fasttextProcess;
private Reader fasttextIn;
private Writer fasttextOut;
Expand Down Expand Up @@ -93,7 +95,7 @@ public Map<String, Double> runFasttext(String text, List<String> additionalLangu
// hack to see if this helps us debug the rare case of readLine() returning null:
try {
logger.warn("fasttextIn.read() returned no data, trying again after short delay");
Thread.sleep(10);
Thread.sleep(READ_RETRY_DELAY);
read = fasttextIn.read(cbuf);
if (read == -1) {
logger.warn("fasttextIn.read() returned no data again");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,121 +48,132 @@ public abstract class AbstractAdvancedSynthesizerFilter extends RuleFilter {

@Override
public RuleMatch acceptRuleMatch(RuleMatch match, Map<String, String> arguments, int patternTokenPos,
AnalyzedTokenReadings[] patternTokens) throws IOException {

// if (match.getSentence().getText().contains("Jo pensem")) {
// int ii=0;
// ii++;
// }
AnalyzedTokenReadings[] patternTokens) throws IOException {

String postagSelect = getRequired("postagSelect", arguments);
String lemmaSelect = getRequired("lemmaSelect", arguments);
String postagFromStr = getRequired("postagFrom", arguments);
String lemmaFromStr = getRequired("lemmaFrom", arguments);

int postagFrom = 0;
if (postagFromStr.startsWith("marker")) {
while (postagFrom < patternTokens.length && patternTokens[postagFrom].getStartPos() < match.getFromPos()) {
postagFrom++;
}
postagFrom++;
if (postagFromStr.length()>6) {
postagFrom += Integer.parseInt(postagFromStr.replace("marker", ""));
}

int postagFrom = calculateIndex(postagFromStr, patternTokens, match.getFromPos());
int lemmaFrom = calculateIndex(lemmaFromStr, patternTokens, match.getFromPos());

validateIndex("postagFrom", postagFrom, patternTokens, match);
validateIndex("lemmaFrom", lemmaFrom, patternTokens, match);

String postagReplace = getOptional("postagReplace", arguments);

String desiredLemma = getAnalyzedToken(patternTokens[lemmaFrom - 1], lemmaSelect).getLemma();
String originalPostag = getAnalyzedToken(patternTokens[lemmaFrom - 1], lemmaSelect).getPOSTag();
String desiredPostag = getAnalyzedToken(patternTokens[postagFrom - 1], postagSelect).getPOSTag();

handleUndefinedPosTag(match, desiredPostag, postagSelect, patternTokens, postagFrom);

desiredPostag = replacePosTagIfNeeded(postagReplace, lemmaSelect, postagSelect, originalPostag, desiredPostag);

boolean isWordCapitalized = StringTools.isCapitalizedWord(patternTokens[lemmaFrom - 1].getToken());
boolean isWordAllupper = StringTools.isAllUppercase(patternTokens[lemmaFrom - 1].getToken());
AnalyzedToken token = new AnalyzedToken("", desiredPostag, desiredLemma);
String[] replacements = getSynthesizer().synthesize(token, desiredPostag, true);

if (replacements.length > 0) {
return createNewRuleMatch(match, isWordCapitalized, isWordAllupper, replacements, desiredPostag);
}
return match;
}

private int calculateIndex(String indexStr, AnalyzedTokenReadings[] patternTokens, int fromPos) {
int index;
if (indexStr.startsWith("marker")) {
index = calculateMarkerIndex(indexStr, patternTokens, fromPos);
} else {
postagFrom = Integer.parseInt(postagFromStr);
index = Integer.parseInt(indexStr);
}
if (postagFrom < 1 || postagFrom > patternTokens.length) {
throw new IllegalArgumentException("AdvancedSynthesizerFilter: Index out of bounds in "
+ match.getRule().getFullId() + ", value: " + postagFromStr);
return index;
}

private int calculateMarkerIndex(String marker, AnalyzedTokenReadings[] patternTokens, int fromPos) {
int index = 0;
while (index < patternTokens.length && patternTokens[index].getStartPos() < fromPos) {
index++;
}
int lemmaFrom = 0;
if (lemmaFromStr.startsWith("marker")) {
while (lemmaFrom < patternTokens.length && patternTokens[lemmaFrom].getStartPos() < match.getFromPos()) {
lemmaFrom++;
}
lemmaFrom++;
if (lemmaFromStr.length()>6) {
lemmaFrom += Integer.parseInt(lemmaFromStr.replace("marker", ""));
}
} else {
lemmaFrom = Integer.parseInt(lemmaFromStr);
index++;
if (marker.length() > 6) {
index += Integer.parseInt(marker.replace("marker", ""));
}
if (lemmaFrom < 1 || lemmaFrom > patternTokens.length) {
return index;
}

private void validateIndex(String indexName, int index, AnalyzedTokenReadings[] patternTokens, RuleMatch match) {
if (index < 1 || index > patternTokens.length) {
throw new IllegalArgumentException("AdvancedSynthesizerFilter: Index out of bounds in "
+ match.getRule().getFullId() + ", value: " + lemmaFromStr);
+ match.getRule().getFullId() + ", value: " + index);
}
}

String postagReplace = getOptional("postagReplace", arguments);

String desiredLemma = getAnalyzedToken(patternTokens[lemmaFrom - 1], lemmaSelect).getLemma();
String originalPostag = getAnalyzedToken(patternTokens[lemmaFrom - 1], lemmaSelect).getPOSTag();
String desiredPostag = getAnalyzedToken(patternTokens[postagFrom - 1], postagSelect).getPOSTag();

private void handleUndefinedPosTag(RuleMatch match, String desiredPostag, String postagSelect, AnalyzedTokenReadings[] patternTokens, int postagFrom) {
if (desiredPostag == null) {
throw new IllegalArgumentException("AdvancedSynthesizerFilter: undefined POS tag for rule " +
match.getRule().getFullId() + " with POS regex '" + postagSelect + "' for token: " + patternTokens[postagFrom-1]);
match.getRule().getFullId() + " with POS regex '" + postagSelect + "' for token: " +
patternTokens[postagFrom - 1]);
}
}

private String replacePosTagIfNeeded(String postagReplace, String lemmaSelect, String postagSelect, String originalPostag, String desiredPostag) {
if (postagReplace != null) {
desiredPostag = getCompositePostag(lemmaSelect, postagSelect, originalPostag, desiredPostag, postagReplace);
}
return desiredPostag;
}

// take capitalization from the lemma (?)
boolean isWordCapitalized = StringTools.isCapitalizedWord(patternTokens[lemmaFrom - 1].getToken());
boolean isWordAllupper = StringTools.isAllUppercase(patternTokens[lemmaFrom - 1].getToken());
AnalyzedToken token = new AnalyzedToken("", desiredPostag, desiredLemma);
String[] replacements = getSynthesizer().synthesize(token, desiredPostag, true);

if (replacements.length > 0) {
RuleMatch newMatch = new RuleMatch(match.getRule(), match.getSentence(), match.getFromPos(), match.getToPos(),
match.getMessage(), match.getShortMessage());
newMatch.setType(match.getType());
List<String> replacementsList = new ArrayList<>();

boolean suggestionUsed = false;
for (String r : match.getSuggestedReplacements()) {
for (String nr : replacements) {
if (isSuggestionException(nr, desiredPostag)) {
continue;
}
if (r.contains("{suggestion}") || r.contains("{Suggestion}") || r.contains("{SUGGESTION}")) {
suggestionUsed = true;
}
if (isWordCapitalized) {
nr = StringTools.uppercaseFirstChar(nr);
}
if (isWordAllupper) {
nr = nr.toUpperCase();
}
String completeSuggestion = r.replace("{suggestion}", nr);
completeSuggestion = completeSuggestion.replace("{Suggestion}", StringTools.uppercaseFirstChar(nr));
completeSuggestion = completeSuggestion.replace("{SUGGESTION}", nr.toUpperCase());
if (!replacementsList.contains(completeSuggestion)) {
replacementsList.add(completeSuggestion);
}
private RuleMatch createNewRuleMatch(RuleMatch match, boolean isWordCapitalized, boolean isWordAllupper, String[] replacements, String desiredPostag) {
RuleMatch newMatch = new RuleMatch(match.getRule(), match.getSentence(), match.getFromPos(), match.getToPos(),
match.getMessage(), match.getShortMessage());
newMatch.setType(match.getType());
List<String> replacementsList = new ArrayList<>();

boolean suggestionUsed = false;
for (String r : match.getSuggestedReplacements()) {
for (String nr : replacements) {
if (isSuggestionException(nr, desiredPostag)) {
continue;
}
}
if (!suggestionUsed) {
replacementsList.addAll(Arrays.asList(replacements));
}

List<String> adjustedReplacementsList = new ArrayList<>();
Rule rule = match.getRule();
if (rule instanceof AbstractPatternRule) {
Language lang = ((AbstractPatternRule) rule).getLanguage();
for (String replacement : replacementsList) {
adjustedReplacementsList.add(lang.adaptSuggestion(replacement));
if (r.toLowerCase().contains("{suggestion}")) {
suggestionUsed = true;
}
if (isWordCapitalized) {
nr = StringTools.uppercaseFirstChar(nr);
}
if (isWordAllupper) {
nr = nr.toUpperCase();
}
String completeSuggestion = r.replace("{suggestion}", nr);
completeSuggestion = completeSuggestion.replace("{Suggestion}", StringTools.uppercaseFirstChar(nr));
completeSuggestion = completeSuggestion.replace("{SUGGESTION}", nr.toUpperCase());
if (!replacementsList.contains(completeSuggestion)) {
replacementsList.add(completeSuggestion);
}
} else {
adjustedReplacementsList = replacementsList;
}
newMatch.setSuggestedReplacements(adjustedReplacementsList);
return newMatch;
}
return match;
if (!suggestionUsed) {
replacementsList.addAll(Arrays.asList(replacements));
}

List<String> adjustedReplacementsList = new ArrayList<>();
Rule rule = match.getRule();
if (rule instanceof AbstractPatternRule) {
Language lang = ((AbstractPatternRule) rule).getLanguage();
for (String replacement : replacementsList) {
adjustedReplacementsList.add(lang.adaptSuggestion(replacement));
}
} else {
adjustedReplacementsList = replacementsList;
}
newMatch.setSuggestedReplacements(adjustedReplacementsList);
return newMatch;
}


private String getCompositePostag(String lemmaSelect, String postagSelect, String originalPostag,
String desiredPostag, String postagReplace) {
Pattern aPattern = Pattern.compile(lemmaSelect, Pattern.UNICODE_CASE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,38 @@ static class ExpectedMatches {
}

static class ExpectedMatch {
public int offset;
public int length;
public String rule_id;
private int offset;
private int length;
private String rule_id;

// Getter methods
public int getOffset() {
return offset;
}

public int getLength() {
return length;
}

public String getRuleId() {
return rule_id;
}

// Setter methods
public void setOffset(int offset) {
this.offset = offset;
}

public void setLength(int length) {
this.length = length;
}

public void setRuleId(String ruleId) {
this.rule_id = ruleId;
}
}


static class ExpectedRule extends Rule{
private final String id;
public ExpectedRule(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.languagetool;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the standard license header we use? (With your name as the copyright holder?)


import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals ;

public class GlobalConfigTest {

/* This tests the equals method */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment isn't really necessary

@Test
public void testEquals() {
//Initialise the objects
GlobalConfig config1 = new GlobalConfig();
GlobalConfig config2 = new GlobalConfig();

// Both objects are initially empty and should be equal
assertEquals(config1, config2);

config1.setGrammalecteServer("server1");
config2.setGrammalecteServer("server2");

//Check if they are different servers
assertNotEquals(config1, config2);

//Set config1 to the same server
config1.setGrammalecteServer("server2");

//Check for same server
assertEquals(config1, config2);

config1.setBeolingusFile(new File("file1"));
assertNotEquals(config1, config2);
}

/* This tests the hashCode method */
@Test
public void testHashCode() {
GlobalConfig config1 = new GlobalConfig();
GlobalConfig config2 = new GlobalConfig();

// Initially, both objects are empty, so their hash codes should be equal.
assertEquals(config1.hashCode(), config2.hashCode());

config1.setGrammalecteServer("server1");
config2.setGrammalecteServer("server2");

// After setting different servers, their hash codes should not be equal.
assertNotEquals(config1.hashCode(), config2.hashCode());

config1.setGrammalecteServer("server2");

// When both objects have the same server, their hash codes should be equal again.
assertEquals(config1.hashCode(), config2.hashCode());

config1.setBeolingusFile(new File("file1"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a different property? beoLingus isn't used anymore and might soon be removed


// After setting different Beolingus files, their hash codes should not be equal.
assertNotEquals(config1.hashCode(), config2.hashCode());
}

}
Loading