summaryrefslogtreecommitdiffstats
path: root/JLanguageTool/src/test/de/danielnaber/languagetool/JLanguageToolTest.java
blob: 80afa8af9822b378663a4c545256bfe9a052c8dc (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
/* 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;

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

import junit.framework.TestCase;
import de.danielnaber.languagetool.JLanguageTool.paragraphHandling;
import de.danielnaber.languagetool.rules.RuleMatch;
import de.danielnaber.languagetool.rules.patterns.PatternRule;

/**
 * @author Daniel Naber
 */
public class JLanguageToolTest extends TestCase {

  // used on http://www.languagetool.org/usage/
  /*
  public void testDemo() throws IOException {
    JLanguageTool langTool = new JLanguageTool(Language.ENGLISH);
    langTool.activateDefaultPatternRules();
    List<RuleMatch> matches = langTool.check("A sentence " + 
        "with a error in the Hitchhiker's Guide tot he Galaxy");
    for (RuleMatch match : matches) {
      System.out.println("Potential error at line " +
          match.getEndLine() + ", column " +
          match.getColumn() + ": " + match.getMessage());
      System.out.println("Suggested correction: " +
          match.getSuggestedReplacements());
    }
  }
  */
  
  
  public void testEnglish() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.ENGLISH);
    List<RuleMatch> matches = tool.check("A test that should not give errors.");
    assertEquals(0, matches.size());
    matches = tool.check("A test test that should give errors.");
    assertEquals(1, matches.size());
    matches = tool.check("I can give you more a detailed description.");
    assertEquals(0, matches.size());
    assertEquals(8, tool.getAllRules().size());
    final List<PatternRule> rules = tool.loadPatternRules(JLanguageTool.getDataBroker().getRulesDir()
    		+ "/en/grammar.xml");
    for (PatternRule patternRule : rules) {
      tool.addRule(patternRule);
    }
    assertTrue(tool.getAllRules().size() > 3);
    matches = tool.check("I can give you more a detailed description.");
    assertEquals(1, matches.size());
    tool.disableRule("MORE_A_JJ");
    matches = tool.check("I can give you more a detailed description.");
    assertEquals(0, matches.size());
    tool.disableCategory("Possible Typos");
    matches = tool.check("I've go to go.");
    assertEquals(0, matches.size());
  }
  
  public void testGerman() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.GERMAN);
    List<RuleMatch> matches = tool.check("Ein Test, der keine Fehler geben sollte.");
    assertEquals(0, matches.size());
    matches = tool.check("Ein Test Test, der Fehler geben sollte.");
    assertEquals(1, matches.size());
    final List<PatternRule> rules = tool.loadPatternRules(JLanguageTool.getDataBroker().getRulesDir()
    		+ "/de/grammar.xml");
    for (PatternRule patternRule : rules) {
      tool.addRule(patternRule);
    }
    tool.setListUnknownWords(true);
    // German rule has no effect with English error:
    matches = tool.check("I can give you more a detailed description");
    assertEquals(0, matches.size());
    //test unknown words listing
    assertEquals("[I, can, detailed, give, more, you]", tool.getUnknownWords().toString());    
  }

  public void testDutch() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.DUTCH);
    final List<PatternRule> rules = tool.loadPatternRules(JLanguageTool.getDataBroker().getRulesDir()
    		+ "/nl/grammar.xml");
    for (PatternRule patternRule : rules) {
      tool.addRule(patternRule);
    }
    List<RuleMatch> matches = tool.check("Een test, die geen fouten mag geven.");
    assertEquals(0, matches.size());
    matches = tool.check("Een test test, die een fout moet geven.");
    assertEquals(1, matches.size());
    //test uppercasing rule:
    /*  
    matches = tool.check("De Afdeling Beheer kan het");
    assertEquals(1, matches.size());   
    assertEquals("Als Afdeling geen deel uitmaakt van de naam, dan is juist:<suggestion>afdeling</suggestion>", matches.get(0).getMessage());
     */
    // Dutch rule has no effect with English error:
    matches = tool.check("I can give you more a detailed description");
    assertEquals(0, matches.size());
  }
  
  public void testPolish() throws IOException {
    JLanguageTool tool = new JLanguageTool(Language.POLISH);
    assertEquals("[PL]", Arrays.toString(Language.POLISH.getCountryVariants()));
    List<RuleMatch> matches = tool.check("To jest całkowicie prawidłowe zdanie.");
    assertEquals(0, matches.size());
    matches = tool.check("To jest jest problem.");
    assertEquals(1, matches.size());
    //this rule is by default off
    matches = tool.check("Był on bowiem pięknym strzelcem bowiem.");
    assertEquals(0, matches.size());
    tool.enableDefaultOffRule("PL_WORD_REPEAT");
    matches = tool.check("Był on bowiem pięknym strzelcem bowiem.");
    assertEquals(1, matches.size());
    List<PatternRule> rules = tool.loadPatternRules(JLanguageTool.getDataBroker().getRulesDir()
    		+ "/pl/grammar.xml");
    for (final PatternRule rule : rules) {
      tool.addRule(rule);
    }
    matches = tool.check("Premier drapie się w ucho co i rusz.");
    assertEquals(1, matches.size());
    // Polish rule has no effect with English error:
    matches = tool.check("I can give you more a detailed description");
    assertEquals(0, matches.size());
    tool.setListUnknownWords(true);
    matches = tool.check("This is not a Polish text.");
    assertEquals("[Polish, This, is]", tool.getUnknownWords().toString());
    //check positions relative to sentence ends    
    matches = tool.check("To jest tekst.\nTest 1. To jest linia w której nie ma przecinka.");
    assertEquals(16, matches.get(0).getColumn());
    //with a space...
    matches = tool.check("To jest tekst. \nTest 1. To jest linia w której nie ma przecinka.");
    assertEquals(16, matches.get(0).getColumn());
    matches = tool.check("To jest tekst. Test 1. To jest linia w której nie ma przecinka.");
    assertEquals(30, matches.get(0).getColumn());
    //recheck with the -b mode...
    final Language lang = Language.POLISH;
    lang.getSentenceTokenizer().setSingleLineBreaksMarksParagraph(
        true);
    tool = new JLanguageTool(lang);
    rules = tool.loadPatternRules(JLanguageTool.getDataBroker().getRulesDir()
    		+ "/pl/grammar.xml");
    for (final PatternRule rule : rules) {
      tool.addRule(rule);
    }
    matches = tool.check("To jest tekst.\nTest 1. To jest linia w której nie ma przecinka.");
    assertEquals(16, matches.get(0).getColumn());
    //with a space...
    matches = tool.check("To jest tekst. \nTest 1. To jest linia w której nie ma przecinka.");
    assertEquals(16, matches.get(0).getColumn());
    matches = tool.check("To jest tekst. To jest linia w której nie ma przecinka.");
    assertEquals(23, matches.get(0).getColumn());
    
  }
  
  public void testSlovenian() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.SLOVENIAN);
    List<RuleMatch> matches = tool.check("Kupil je npr. jajca, moko in mleko.");
    assertEquals(0, matches.size());
  }
  
  public void testCountLines() {
    assertEquals(0, JLanguageTool.countLineBreaks(""));
    assertEquals(1, JLanguageTool.countLineBreaks("Hallo,\nnächste Zeile"));
    assertEquals(2, JLanguageTool.countLineBreaks("\nZweite\nDritte"));
    assertEquals(4, JLanguageTool.countLineBreaks("\nZweite\nDritte\n\n"));
  }
  
  
  public void testAnalyzedSentence() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.ENGLISH);
    //test soft-hyphen ignoring:
    assertEquals("<S> This[this/DT]  is[be/VBZ]  a[a/DT]  test­ed[tested/JJ,test/VBD,test/VBN,test­ed]  sentence[sentence/NN,sentence/VB,sentence/VBP].[./.,</S>]", tool.getAnalyzedSentence("This is a test\u00aded sentence.").toString());
    //test paragraph ends adding
    assertEquals("<S> </S><P/> ", tool.getAnalyzedSentence("\n").toString());
  }  
  
  public void testParaRules() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.ENGLISH);
    
    //run normally
    List<RuleMatch> matches = tool.check("(This is an quote.\n It ends in the second sentence.");
    assertEquals(2, matches.size());
    assertEquals(2, tool.getSentenceCount());
    
    //run in a sentence-only mode
    matches = tool.check("(This is an quote.\n It ends in the second sentence.", false, paragraphHandling.ONLYNONPARA);
    assertEquals(1, matches.size());
    assertEquals("EN_A_VS_AN", matches.get(0).getRule().getId());
    assertEquals(1, tool.getSentenceCount());
    
    //run in a paragraph mode - single sentence
    matches = tool.check("(This is an quote.\n It ends in the second sentence.", false, paragraphHandling.ONLYPARA);
    assertEquals(1, matches.size());
    assertEquals("EN_UNPAIRED_BRACKETS", matches.get(0).getRule().getId());
    assertEquals(1, tool.getSentenceCount());
    
    //run in a paragraph mode - many sentences
    matches = tool.check("(This is an quote.\n It ends in the second sentence.", true, paragraphHandling.ONLYPARA);
    assertEquals(1, matches.size());
    assertEquals("EN_UNPAIRED_BRACKETS", matches.get(0).getRule().getId());
    assertEquals(2, tool.getSentenceCount());
  }  
    
  public void testWhitespace() throws IOException {
    final JLanguageTool tool = new JLanguageTool(Language.ENGLISH);
    final AnalyzedSentence raw = tool.getRawAnalyzedSentence("Let's do a \"test\", do you understand?");
    final AnalyzedSentence cooked = tool.getAnalyzedSentence("Let's do a \"test\", do you understand?");
    //test if there was a change
    assertFalse(raw.equals(cooked));
    //see if nothing has been deleted
    assertEquals(raw.getTokens().length, cooked.getTokens().length);
    int i = 0;
    for (final AnalyzedTokenReadings atr : raw.getTokens()) {
      assertEquals(atr.isWhitespaceBefore(), 
          cooked.getTokens()[i].isWhitespaceBefore());
      i++;
    }
  }
  
}