summaryrefslogtreecommitdiffstats
path: root/JLanguageTool/src/test/de/danielnaber/languagetool/rules/bitext/BitextPatternRuleTest.java
blob: 01e4f6a07cc01c42e39094600132e6b65361abed (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* LanguageTool, a natural language style checker 
 * Copyright (C) 2010 Marcin Miłkowski (www.languagetool.org)
 * 
 * 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.bitext;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import de.danielnaber.languagetool.AnalyzedSentence;
import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.bitext.StringPair;
import de.danielnaber.languagetool.rules.Rule;
import de.danielnaber.languagetool.rules.RuleMatch;
import de.danielnaber.languagetool.rules.patterns.PatternRule;
import de.danielnaber.languagetool.rules.patterns.bitext.BitextPatternRule;
import de.danielnaber.languagetool.rules.patterns.bitext.BitextPatternRuleLoader;
import junit.framework.TestCase;

public class BitextPatternRuleTest extends TestCase {

  public void testBitextRulesFromXML() throws IOException {
    testBitextRulesFromXML(null, false);
  }
  
  private void testBitextRulesFromXML(final Set<Language> ignoredLanguages,
      final boolean verbose) throws IOException {
     for (final Language lang : Language.LANGUAGES) {
      if (ignoredLanguages != null && ignoredLanguages.contains(lang)) {
        if (verbose) {
          System.out.println("Ignoring tests for " + lang.getName());
        }
        continue;
      }                  
      final BitextPatternRuleLoader ruleLoader = new BitextPatternRuleLoader();
      final String name = "/" + lang.getShortName() + "/bitext.xml";
      final InputStream is = JLanguageTool.getDataBroker().getFromRulesDirAsStream(name);
      if (is != null) {
        if (verbose) {
          System.out.println("Running tests for " + lang.getName() + "...");
        }
        final JLanguageTool languageTool = new JLanguageTool(lang);
        final List<BitextPatternRule> rules = ruleLoader.getRules(is, name);
        testBitextRulesFromXML(rules, languageTool, Language.POLISH);
      }                           
    }
  }
  
  private void testBitextRulesFromXML(final List<BitextPatternRule> rules,
      final JLanguageTool languageTool, final Language lang) throws IOException {    
    final HashMap<String, PatternRule> complexRules = new HashMap<String, PatternRule>();
    for (final BitextPatternRule rule : rules) {
      testBitextRule(rule, lang, languageTool);      
    }
    /*
    if (!complexRules.isEmpty()) {
      final Set<String> set = complexRules.keySet();
      final List<PatternRule> badRules = new ArrayList<PatternRule>();
      final Iterator<String> iter = set.iterator();
      while (iter.hasNext()) {
        final PatternRule badRule = complexRules.get(iter.next());
        if (badRule != null) {
          badRule.notComplexPhrase();
          badRule
              .setMessage("The rule contains a phrase that never matched any incorrect example.");
          badRules.add(badRule);
        }
      }
      if (!badRules.isEmpty()) {
        testGrammarRulesFromXML(badRules, languageTool, lang);
      }
    }
    */
  }

  private String cleanSentence(String str) {
    return cleanXML(str.replaceAll("[\\n\\t]+", ""));    
  }
  
  private void testMarker(int expectedMatchStart,
      int expectedMatchEnd, Rule rule, Language lang) {
    if (expectedMatchStart == -1 || expectedMatchEnd == -1) {
      fail(lang
          + ": No error position markup ('<marker>...</marker>') in bad example in rule "
          + rule);
    }

  }
  
  private void testBadSentence(final String origBadSentence, 
      final List<String> suggestedCorrection, final int expectedMatchStart,
      final int expectedMatchEnd, final PatternRule rule, 
      final Language lang,
      final JLanguageTool languageTool) throws IOException {
    final String badSentence = cleanXML(origBadSentence);
    assertTrue(badSentence.trim().length() > 0);
    RuleMatch[] matches = getMatches(rule, badSentence, languageTool);
//    if (!rule.isWithComplexPhrase()) {
      assertTrue(lang + ": Did expect one error in: \"" + badSentence
          + "\" (Rule: " + rule + "), got " + matches.length
          + ". Additional info:" + rule.getMessage(), matches.length == 1);
      assertEquals(lang
          + ": Incorrect match position markup (start) for rule " + rule,
          expectedMatchStart, matches[0].getFromPos());
      assertEquals(lang
          + ": Incorrect match position markup (end) for rule " + rule,
          expectedMatchEnd, matches[0].getToPos());
      // make sure suggestion is what we expect it to be
      if (suggestedCorrection != null && suggestedCorrection.size() > 0) {
        assertTrue("You specified a correction but your message has no suggestions in rule " + rule,
          rule.getMessage().contains("<suggestion>")    
        );
        assertTrue(lang + ": Incorrect suggestions: "
            + suggestedCorrection.toString() + " != "
            + matches[0].getSuggestedReplacements() + " for rule " + rule,
            suggestedCorrection.equals(matches[0]
                .getSuggestedReplacements()));
//      }
      // make sure the suggested correction doesn't produce an error:
      if (matches[0].getSuggestedReplacements().size() > 0) {
        final int fromPos = matches[0].getFromPos();
        final int toPos = matches[0].getToPos();
        for (final String repl : matches[0].getSuggestedReplacements()) {
          final String fixedSentence = badSentence.substring(0, fromPos)
              + repl + badSentence.substring(toPos);
          matches = getMatches(rule, fixedSentence, languageTool);
          if (matches.length > 0) {
              fail("Incorrect input:\n"
                      + "  " + badSentence
                    + "\nCorrected sentence:\n"
                      + "  " + fixedSentence
                      + "\nBy Rule:\n"
                      + "  " + rule
                      + "\nThe correction triggered an error itself:\n"
                      + "  " + matches[0] + "\n");
          }
        }       
      }
      }
  }
  
  private void testBitextRule(final BitextPatternRule rule, final Language lang, 
      final JLanguageTool languageTool) throws IOException {
    JLanguageTool srcTool = new JLanguageTool(rule.getSourceLang());
    //int noSuggestionCount = 0;
    final List<StringPair> goodSentences = rule.getCorrectBitextExamples();
    for (StringPair goodSentence : goodSentences) {
      assertTrue(cleanSentence(goodSentence.getSource()).trim().length() > 0);
      assertTrue(cleanSentence(goodSentence.getTarget()).trim().length() > 0);
      assertFalse(lang + ": Did not expect error in: " + goodSentence
          + " (Rule: " + rule + ")", 
          match(rule, goodSentence.getSource(), goodSentence.getTarget(), 
              srcTool, languageTool));
    }
    final List<IncorrectBitextExample> badSentences = rule.getIncorrectBitextExamples();
    for (IncorrectBitextExample origBadExample : badSentences) {
      // enable indentation use
      String origBadSrcSentence = origBadExample.getExample().getSource().replaceAll(
          "[\\n\\t]+", "");
      String origBadTrgSentence = origBadExample.getExample().getTarget().replaceAll(
          "[\\n\\t]+", "");
      final List<String> suggestedCorrection = origBadExample
          .getCorrections();
      final int expectedSrcMatchStart = origBadSrcSentence.indexOf("<marker>");
      final int expectedSrcMatchEnd = origBadSrcSentence.indexOf("</marker>")
          - "<marker>".length();
      testMarker(expectedSrcMatchStart, expectedSrcMatchEnd, rule, lang);
      final int expectedTrgMatchStart = origBadTrgSentence.indexOf("<marker>");
      final int expectedTrgMatchEnd = origBadTrgSentence.indexOf("</marker>")
          - "<marker>".length();
      testMarker(expectedTrgMatchStart, expectedTrgMatchEnd, rule, lang);          
      
      testBadSentence(origBadSrcSentence, 
          suggestedCorrection, expectedSrcMatchStart,
          expectedSrcMatchEnd, rule.getSrcRule(), 
          lang,
          srcTool);
      
      testBadSentence(origBadTrgSentence, 
          suggestedCorrection, expectedTrgMatchStart,
          expectedTrgMatchEnd, rule.getTrgRule(), 
          lang,
          languageTool);
      
    }
      
      /*      } else { // for multiple rules created with complex phrases

        matches = getMatches(rule, badSentence, languageTool);
        if (matches.length == 0
            && !complexRules.containsKey(rule.getId() + badSentence)) {
          complexRules.put(rule.getId() + badSentence, rule);
        }

        if (matches.length != 0) {
          complexRules.put(rule.getId() + badSentence, null);
          assertTrue(lang + ": Did expect one error in: \"" + badSentence
              + "\" (Rule: " + rule + "), got " + matches.length,
              matches.length == 1);
          assertEquals(lang
              + ": Incorrect match position markup (start) for rule " + rule,
              expectedMatchStart, matches[0].getFromPos());
          assertEquals(lang
              + ": Incorrect match position markup (end) for rule " + rule,
              expectedMatchEnd, matches[0].getToPos());
          // make sure suggestion is what we expect it to be
          if (suggestedCorrection != null && suggestedCorrection.size() > 0) {
            assertTrue(
                lang + ": Incorrect suggestions: "
                    + suggestedCorrection.toString() + " != "
                    + matches[0].getSuggestedReplacements() + " for rule "
                    + rule, suggestedCorrection.equals(matches[0]
                    .getSuggestedReplacements()));
          }
          // make sure the suggested correction doesn't produce an error:
          if (matches[0].getSuggestedReplacements().size() > 0) {
            final int fromPos = matches[0].getFromPos();
            final int toPos = matches[0].getToPos();
            for (final String repl : matches[0].getSuggestedReplacements()) {
              final String fixedSentence = badSentence.substring(0, fromPos)
                  + repl + badSentence.substring(toPos);
              matches = getMatches(rule, fixedSentence, languageTool);
              assertEquals("Corrected sentence for rule " + rule
                  + " triggered error: " + fixedSentence, 0, matches.length);
            }
          } else {
            noSuggestionCount++;
          }
        } */
      } 

  
    
    
  protected String cleanXML(final String str) {
    return str.replaceAll("<([^<].*?)>", "");
  }
  
  private boolean match(final BitextPatternRule rule, final String src, final String trg,
      final JLanguageTool srcLanguageTool,
      final JLanguageTool trgLanguageTool) throws IOException {
    final AnalyzedSentence srcText = srcLanguageTool.getAnalyzedSentence(src);
    final AnalyzedSentence trgText = trgLanguageTool.getAnalyzedSentence(trg);
    final RuleMatch[] matches = rule.match(srcText, trgText);
    return matches.length > 0;
  }

  
  private RuleMatch[] getMatches(final Rule rule, final String sentence,
      final JLanguageTool languageTool) throws IOException {
    final AnalyzedSentence text = languageTool.getAnalyzedSentence(sentence);
    final RuleMatch[] matches = rule.match(text);
    return matches;
  }

  /**
   * Test XML patterns, as a help for people developing rules that are not
   * programmers.
   */
  public static void main(final String[] args) throws IOException {
    final BitextPatternRuleTest prt = new BitextPatternRuleTest();
    System.out.println("Running XML bitext pattern tests...");   
    prt.testBitextRulesFromXML();        
    System.out.println("Tests successful.");
  }

  
  
}