1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/* LanguageTool, a natural language style checker
* Copyright (C) 2005 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package de.danielnaber.languagetool.rules.ro;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.TestTools;
import de.danielnaber.languagetool.rules.RuleMatch;
/**
*
* Simple tests for rules/ro/SimpleReplaceRule class
*
* @author Ionuț Păduraru
*/
public class SimpleReplaceRuleTest extends TestCase {
private SimpleReplaceRule rule;
private JLanguageTool langTool;
protected void setUp() throws Exception {
super.setUp();
rule = new SimpleReplaceRule(TestTools.getMessages("ro"));
langTool = new JLanguageTool(Language.ROMANIAN);
}
/**
* Make sure that the suggested word is not the same as the wrong word
*/
public void testInvalidSuggestion() {
List<String> invalidSuggestions = new ArrayList<String>();
List<Map<String,String>> wrongWords = rule.getWrongWords();
for (Map<String, String> ruleEntry : wrongWords) {
for (String fromWord : ruleEntry.keySet()) {
String toWord = ruleEntry.get(fromWord);
if (toWord == null || fromWord.equals(toWord)) {
invalidSuggestions.add(toWord);
}
}
}
if (!invalidSuggestions.isEmpty()) {
fail("Invalid suggestions found for: " + Arrays.toString(invalidSuggestions.toArray(new String[]{})));
}
}
public void testRule() throws IOException {
// correct sentences:
assertEquals(0, rule.match(langTool.getAnalyzedSentence("Paisprezece case.")).length);
// incorrect sentences:
// at the beginning of a sentence (Romanian replace rule is case-sensitive)
checkSimpleReplaceRule("Patrusprezece case.", "Paisprezece");
// inside sentence
checkSimpleReplaceRule("Satul are patrusprezece case.", "paisprezece");
checkSimpleReplaceRule("Satul are (patrusprezece) case.", "paisprezece");
checkSimpleReplaceRule("Satul are «patrusprezece» case.", "paisprezece");
checkSimpleReplaceRule("El are șasesprezece ani.", "șaisprezece");
checkSimpleReplaceRule("El a luptat pentru întâiele cărți.", "întâile");
checkSimpleReplaceRule("El are cinsprezece cărți.", "cincisprezece");
checkSimpleReplaceRule("El a fost patruzecioptist.", "pașoptist");
checkSimpleReplaceRule("M-am adresat întâiei venite.", "întâii");
checkSimpleReplaceRule("M-am adresat întâielor venite.", "întâilor");
checkSimpleReplaceRule("A ajuns al douăzecelea.", "douăzecilea");
checkSimpleReplaceRule("A ajuns al zecilea.", "zecelea");
checkSimpleReplaceRule("A primit jumate de litru de lapte.", "jumătate");
// multiple words / compounds
// space-delimited
checkSimpleReplaceRule("aqua forte", "acvaforte");
checkSimpleReplaceRule("aqua forte.", "acvaforte");
checkSimpleReplaceRule("A folosit «aqua forte».", "acvaforte");
checkSimpleReplaceRule("Aqua forte.", "Acvaforte");
checkSimpleReplaceRule("este aqua forte", "acvaforte");
checkSimpleReplaceRule("este aqua forte.", "acvaforte");
checkSimpleReplaceRule("este Aqua Forte.", "Acvaforte");
checkSimpleReplaceRule("este AquA Forte.", "Acvaforte");
checkSimpleReplaceRule("A primit jumate de litru de lapte și este aqua forte.", "jumătate", "acvaforte");
checkSimpleReplaceRule("du-te vino", "du-te-vino");
// dash-delimited
checkSimpleReplaceRule("cou-boi", "cowboy");
checkSimpleReplaceRule("cow-boy", "cowboy");
checkSimpleReplaceRule("cau-boi", "cowboy");
checkSimpleReplaceRule("Cau-boi", "Cowboy");
checkSimpleReplaceRule("cowboy"); // correct, no replacement
checkSimpleReplaceRule("Iată un cau-boi", "cowboy");
checkSimpleReplaceRule("Iată un cau-boi.", "cowboy");
checkSimpleReplaceRule("Iată un (cau-boi).", "cowboy");
checkSimpleReplaceRule("văcar=cau-boi", "cowboy");
// multiple suggestions
checkSimpleReplaceRule("A fost adăogită o altă regulă.", "adăugită/adăugată");
checkSimpleReplaceRule("A venit adinioarea.", "adineaori/adineauri");
// words with multiple wrong forms
checkSimpleReplaceRule("A pus axterix.", "asterisc");
checkSimpleReplaceRule("A pus axterics.", "asterisc");
checkSimpleReplaceRule("A pus asterics.", "asterisc");
}
/**
* Check if a specific replace rule applies.
*
* @param sentence
* the sentence containing the incorrect/misspeled word.
* @param words
* the words that are correct (the suggested replacement). Use "/" to separate multiple forms.
* @throws IOException
*/
private void checkSimpleReplaceRule(String sentence, String... words)
throws IOException {
RuleMatch[] matches;
matches = rule.match(langTool.getAnalyzedSentence(sentence));
assertEquals("Invalid matches.length while checking sentence: "
+ sentence, words.length, matches.length);
for (int i = 0; i < words.length; i++) {
String word = words[i];
String[] replacements = word.split("\\/");
assertEquals("Invalid replacement count wile checking sentence: "
+ sentence, replacements.length, matches[i].getSuggestedReplacements().size());
for (int j = 0; j < replacements.length; j++) {
assertEquals("Invalid suggested replacement while checking sentence: "
+ sentence, replacements[j], matches[i].getSuggestedReplacements().get(j));
}
}
}
}
|