Skip to content

Commit

Permalink
[ar] cleanup filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaibafifi authored and danielnaber committed May 24, 2023
1 parent d77346d commit 4cc75e7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public void testGetDayOfWeek1() {
@Test
public void testGetDayOfWeek2() {
Calendar calendar = Calendar.getInstance();
calendar.set(2022, 3 - 1, 25);
calendar.set(2022, Calendar.MARCH, 25);
assertThat(filter.getDayOfWeek(calendar), is("الجمعة"));
calendar.set(2022, 3 - 1, 26);

calendar.set(2022, Calendar.MARCH, 26);
assertThat(filter.getDayOfWeek(calendar), is("السبت"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import org.languagetool.rules.FakeRule;
import org.languagetool.rules.RuleMatch;
import org.languagetool.rules.SimpleReplaceDataLoader;
import org.languagetool.rules.ar.ArabicWordinessRule;
import org.languagetool.rules.patterns.RuleFilter;
import org.languagetool.tagging.ar.ArabicTagger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -49,22 +47,23 @@ public class ArabicMasdarToVerbFilterTest {

@Test
public void testFilter() throws IOException {
assertSuggestion("عمل", "يعمل", false);
assertSuggestion("إعمال", "يعمل", false);
assertSuggestion("سؤال", "يسأل", false);
assertSuggestion("أكل", "يأكل", false);
assertSuggestion("عمل", "يعمل");
assertSuggestion("إعمال", "يعمل");
assertSuggestion("سؤال", "يسأل");
assertSuggestion("أكل", "يأكل");
}

private void assertSuggestion(String word, String expectedSuggestion, boolean debug) throws IOException {
private void assertSuggestion(String word, String expectedSuggestion) throws IOException {
String word1 = "يقوم";
String word2 = "بال" + word;
Map<String, String> args = new HashMap<>();
args.put("verb", word1);
args.put("noun", word2);
List<AnalyzedTokenReadings> patternTokens = tagger.tag(asList(word1, word2));
AnalyzedTokenReadings[] patternTokensArray = patternTokens.stream().toArray(AnalyzedTokenReadings[]::new);
AnalyzedTokenReadings[] patternTokensArray = patternTokens.toArray(new AnalyzedTokenReadings[0]);
RuleMatch ruleMatch = filter.acceptRuleMatch(match, args, -1, patternTokensArray);

assertThat(ruleMatch, notNullValue());
assertThat(ruleMatch.getSuggestedReplacements().size(), is(1));
assertThat(ruleMatch.getSuggestedReplacements().get(0), is(expectedSuggestion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;

public class ArabicNumberPhraseFilterTest {

Expand Down Expand Up @@ -106,13 +107,15 @@ private void assertSuggestion(String phrase, String expectedSuggestion, String p

List<AnalyzedTokenReadings> patternTokens = tagger.tag(tokens);

AnalyzedTokenReadings[] patternTokensArray = patternTokens.stream().toArray(AnalyzedTokenReadings[]::new);
AnalyzedTokenReadings[] patternTokensArray = patternTokens.toArray(new AnalyzedTokenReadings[0]);
RuleMatch ruleMatch = filter.acceptRuleMatch(match, args, -1, patternTokensArray);
if (!debug) {
int expectedSize = expectedSuggestion.split("\\|").length;
assertThat(ruleMatch, notNullValue());
assertThat(ruleMatch.getSuggestedReplacements().size(), is(expectedSize));
} else { // debug is true
String suggestion = "";
assertThat(ruleMatch, notNullValue());
if (!ruleMatch.getSuggestedReplacements().isEmpty()) {
suggestion = ruleMatch.getSuggestedReplacements().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,33 @@
import java.util.Map;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class ArabicVerbToMasdarFilterTest {

private final RuleMatch match = new RuleMatch(new FakeRule(), null, 0, 10, "message");
private final RuleFilter filter = new ArabicVerbToMafoulMutlaqFilter();
private final ArabicTagger tagger = new ArabicTagger();
private static final String FILE_NAME = "/ar/arabic_verb_masdar.txt";
final boolean debug = false;

@Test
public void testFilter() throws IOException {
assertSuggestion("يعمل", "يعمل إعمالًا جيدًا|يعمل عملةً جيدةً|يعمل عملًا جيدًا", true);
assertSuggestion("يعمل", "يعمل عملًا جيدًا");
}

private void assertSuggestion(String word, String expectedSuggestion, boolean debug) throws IOException {
private void assertSuggestion(String word, String expectedSuggestion) throws IOException {
String word2 = "بأسلوب";
String word3 = "جيد";
Map<String, String> args = new HashMap<>();
args.put("verb", word);
args.put("adj", word3);
List<AnalyzedTokenReadings> patternTokens = tagger.tag(asList(word, word2, word3));
AnalyzedTokenReadings[] patternTokensArray = patternTokens.stream().toArray(AnalyzedTokenReadings[]::new);
AnalyzedTokenReadings[] patternTokensArray = patternTokens.toArray(new AnalyzedTokenReadings[0]);
RuleMatch ruleMatch = filter.acceptRuleMatch(match, args, -1, patternTokensArray);
if (!debug) {
assertThat(ruleMatch.getSuggestedReplacements().size(), is(3));
assertThat(ruleMatch.getSuggestedReplacements().get(0), is(expectedSuggestion));
} else { // debug is true
String suggestion = "";
if (!ruleMatch.getSuggestedReplacements().isEmpty()) {
suggestion = ruleMatch.getSuggestedReplacements().toString();
}
// show only no suggestion cases
System.out.println("مثال: " + word + " " + word2 + " " + word3 + " مقترح:" + suggestion);
}
assertThat(ruleMatch, notNullValue());
assertThat(ruleMatch.getSuggestedReplacements(), hasItem(expectedSuggestion));
}


@Test
public void testRule() throws IOException {
// errors:
Map<String, List<String>> verb2masdarList = loadFromPath(FILE_NAME);
}


protected static Map<String, List<String>> loadFromPath(String path) {
return new SimpleReplaceDataLoader().loadWords(path);
}
Expand Down

0 comments on commit 4cc75e7

Please sign in to comment.