summaryrefslogtreecommitdiffstats
path: root/JLanguageTool/src/test/de/danielnaber/languagetool/tools/ToolsTest.java
blob: 91c47ac0305f917dd0c9f259bc0cb1b01f760f67 (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
package de.danielnaber.languagetool.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import junit.framework.TestCase;
import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.bitext.BitextRule;

public class ToolsTest extends TestCase {

  private ByteArrayOutputStream out;
  private ByteArrayOutputStream err;
  private PrintStream stdout;
  private PrintStream stderr;

  public void setUp() throws Exception {
    super.setUp();
    this.stdout = System.out;
    this.stderr = System.err;
    this.out = new ByteArrayOutputStream();
    this.err = new ByteArrayOutputStream();      
    System.setOut(new PrintStream(this.out));
    System.setErr(new PrintStream(this.err));
  }

  public void tearDown() throws Exception {
    super.tearDown();
    System.setOut(this.stdout);
    System.setErr(this.stderr);
  }

  public void testCheck() throws IOException, ParserConfigurationException, SAXException {
    final JLanguageTool tool = new JLanguageTool(Language.POLISH);
    tool.activateDefaultPatternRules();
    tool.activateDefaultFalseFriendRules();
 
    int matches = Tools.checkText("To jest całkowicie prawidłowe zdanie.", tool);
    String output = new String(this.out.toByteArray());
    assertEquals(0, output.indexOf("Time:"));
    assertEquals(0, matches);

    matches = Tools.checkText("To jest jest problem.", tool);
    output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Rule ID: WORD_REPEAT_RULE") != -1);
    assertEquals(1, matches);
  }

  public void testCorrect() throws IOException, ParserConfigurationException, SAXException {
    JLanguageTool tool = new JLanguageTool(Language.POLISH);
    tool.activateDefaultPatternRules();
    tool.activateDefaultFalseFriendRules();

    String correct = Tools.correctText("To jest całkowicie prawidłowe zdanie.", tool);
    assertEquals("To jest całkowicie prawidłowe zdanie.", correct);
    correct = Tools.correctText("To jest jest problem.", tool);
    assertEquals("To jest problem.", correct);

    // more sentences, need to apply more suggestions > 1 in subsequent sentences
    correct = Tools.correctText("To jest jest problem. Ale to już już nie jest problem.", tool);
    assertEquals("To jest problem. Ale to już nie jest problem.", correct);
    correct = Tools.correctText("To jest jest problem. Ale to już już nie jest problem. Tak sie nie robi. W tym zdaniu brakuje przecinka bo go zapomniałem.", tool);
    assertEquals("To jest problem. Ale to już nie jest problem. Tak się nie robi. W tym zdaniu brakuje przecinka, bo go zapomniałem.", correct);
    
    //now English
    tool = new JLanguageTool(Language.ENGLISH);
    tool.activateDefaultPatternRules();
    tool.activateDefaultFalseFriendRules();

    assertEquals("This is a test.", Tools.correctText("This is an test.", tool));

  }
  
  public void testBitextCheck() throws IOException, ParserConfigurationException, SAXException {
    final JLanguageTool srcTool = new JLanguageTool(Language.ENGLISH);    
    final JLanguageTool trgTool = new JLanguageTool(Language.POLISH);    
    trgTool.activateDefaultPatternRules();
    
    final List<BitextRule> rules = Tools.getBitextRules(Language.ENGLISH, Language.POLISH);            
    
    int matches = Tools.checkBitext(
        "This is a perfectly good sentence.",
        "To jest całkowicie prawidłowe zdanie.", srcTool, trgTool, rules,
        false, StringTools.XmlPrintMode.NORMAL_XML);
    String output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Time:") == 0);
    assertEquals(0, matches);

    matches = Tools.checkBitext(
        "This is not actual.", 
        "To nie jest aktualne.", 
        srcTool, trgTool, 
        rules, false, StringTools.XmlPrintMode.NORMAL_XML);        
    output = new String(this.out.toByteArray());
    assertTrue(output.indexOf("Rule ID: ACTUAL") != -1);
    assertEquals(1, matches);
  }  
}