summaryrefslogtreecommitdiff
path: root/morfologik-tools/src/test/java/morfologik/tools/MorphEncodingToolTest.java
blob: 496880fe2646402c60868dcb54a62d293101e11a (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
package morfologik.tools;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;

import morfologik.fsa.FSA;
import morfologik.stemming.Dictionary;
import morfologik.stemming.DictionaryLookup;
import morfologik.stemming.DictionaryMetadataBuilder;
import morfologik.stemming.EncoderType;
import morfologik.stemming.WordData;

import org.fest.assertions.api.Assertions;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.google.common.base.Charsets;
import com.google.common.io.Closer;

/*
 * 
 */
public class MorphEncodingToolTest extends RandomizedTest {
    private Closer closer = Closer.create();

    @After
    public void cleanup() throws IOException {
        closer.close();
    }
    
	@Test
	public void testTool() throws Exception {
		// Create a simple plain text file.
		File input = super.newTempFile();
		File output = super.newTempFile();

		// Populate the file with data.
		PrintWriter w = 
		    new PrintWriter(
		        new OutputStreamWriter(
		            closer.register(new FileOutputStream(input)), "UTF-8"));
		w.println("passagère\tpassager\ttag");
		w.println("nieduży\tduży\ttest");
		w.print("abcd\tabc\txyz");
		w.close();

		// suffix
		MorphEncodingTool.main(new String[] { 
				"--input", input.getAbsolutePath(), 
				"--output", output.getAbsolutePath(), 
				"--encoder", "suffix" });

		BufferedReader testOutput = 
		    new BufferedReader(
		        new InputStreamReader(
		            closer.register(new FileInputStream(output.getAbsolutePath())), "UTF-8"));
		Assert.assertEquals("passagère+Eer+tag", testOutput.readLine());
		Assert.assertEquals("nieduży+Iduży+test", testOutput.readLine());
		Assert.assertEquals("abcd+B+xyz", testOutput.readLine());

		// prefix
		MorphEncodingTool.main(new String[] { 
				"--input", input.getAbsolutePath(), 
				"--output", output.getAbsolutePath(), 
				"--encoder", "prefix" });

		testOutput = 
		    new BufferedReader(
		        new InputStreamReader(
		            closer.register(new FileInputStream(output.getAbsolutePath())), "UTF-8"));
		Assert.assertEquals("passagère+AEer+tag", testOutput.readLine());
		Assert.assertEquals("nieduży+DA+test", testOutput.readLine());
		Assert.assertEquals("abcd+AB+xyz", testOutput.readLine());

		// infix
		MorphEncodingTool.main(new String[] { 
				"--input", input.getAbsolutePath(), 
				"--output", output.getAbsolutePath(), 
				"--encoder", "infix" });

		testOutput = 
		    new BufferedReader(
		        new InputStreamReader(
		            closer.register(new FileInputStream(output.getAbsolutePath())), "UTF-8"));
		Assert.assertEquals("passagère+GDAr+tag", testOutput.readLine());
		Assert.assertEquals("nieduży+ADA+test", testOutput.readLine());
		Assert.assertEquals("abcd+AAB+xyz", testOutput.readLine());

		// custom annotation - test tabs
        MorphEncodingTool.main(new String[] {
                "--annotation", "\t",
                "--input", input.getAbsolutePath(), 
                "--output", output.getAbsolutePath(), 
                "--encoder", "infix" });

        testOutput = 
            new BufferedReader(
                new InputStreamReader(
                    closer.register(new FileInputStream(output.getAbsolutePath())), "UTF-8"));
        Assert.assertEquals("passagère\tGDAr\ttag", testOutput.readLine());
        Assert.assertEquals("nieduży\tADA\ttest", testOutput.readLine());
        Assert.assertEquals("abcd\tAAB\txyz", testOutput.readLine());
	}

	/* */
	@Test
	public void testStemmingFile() throws Exception {
		// Create a simple plain text file.
		File input = super.newTempFile();
		File output = super.newTempFile();

		PrintWriter w = 
		    new PrintWriter(
		        new OutputStreamWriter(
		            closer.register(new FileOutputStream(input)), "UTF-8"));
		w.println("passagère\tpassager");
		w.println("nieduży\tduży");
		w.println();
		w.println("abcd\tabc");
		w.close();

		MorphEncodingTool.main(new String[] { 
		    "--input", input.getAbsolutePath(), 
		    "--output", output.getAbsolutePath(),
		    "-e", "suffix" });

		BufferedReader testOutput = 
		    new BufferedReader(
		        new InputStreamReader(
		            closer.register(new FileInputStream(output.getAbsolutePath())), "UTF-8"));
		Assert.assertEquals("passagère+Eer+", testOutput.readLine());
		Assert.assertEquals("nieduży+Iduży+", testOutput.readLine());
		Assert.assertEquals("abcd+B+", testOutput.readLine());

		testOutput.close();
	}

    /* */
    @Test
    public void testZeroByteSeparator() throws Exception {
        // Create a simple plain text file.
        File input = newTempFile();
        File output = newTempFile();

        // Populate the file with data.
        PrintWriter w = 
            new PrintWriter(
                new OutputStreamWriter(
                    closer.register(new FileOutputStream(input)), "UTF-8"));
        w.println("passagère\tpassager\tTAG1");
        w.println("nieduży\tduży\tTAG2");
        w.println("abcd\tabc\tTAG3");
        w.close();

        MorphEncodingTool.main(new String[] { 
            "--input", input.getAbsolutePath(), 
            "--output", output.getAbsolutePath(),
            "-e", "suffix",
            "--annotation", "\u0000"});

        BufferedReader testOutput = 
            new BufferedReader(
                new InputStreamReader(
                    closer.register(new FileInputStream(output.getAbsolutePath())), "UTF-8"));

        Assert.assertEquals("passagère\u0000Eer\u0000TAG1", testOutput.readLine());
        Assert.assertEquals("nieduży\u0000Iduży\u0000TAG2", testOutput.readLine());
        Assert.assertEquals("abcd\u0000B\u0000TAG3", testOutput.readLine());
 
        File fsaFile = newTempFile();
        FSABuildTool.main(
            "--input", output.getAbsolutePath(),
            "--output", fsaFile.getAbsolutePath());

        FSA fsa = FSA.read(fsaFile);
        DictionaryLookup dl = new DictionaryLookup(
            new Dictionary(
                fsa, 
                new DictionaryMetadataBuilder()
                    .separator((char) 0)
                    .encoding(Charsets.UTF_8)
                    .encoder(EncoderType.SUFFIX)
                    .build()));

        checkEntry(dl, "passagère", "passager", "TAG1");
        checkEntry(dl, "nieduży", "duży", "TAG2");
        checkEntry(dl, "abcd", "abc", "TAG3");
    }

    /* */
    @Test
    public void testAnnotationCharacterInBaseOrDerivedWord() throws Exception {
        // Create a simple plain text file.
        File input = newTempFile();
        File output = newTempFile();

        // Populate the file with data.
        PrintWriter w = 
            new PrintWriter(
                new OutputStreamWriter(
                    closer.register(new FileOutputStream(input)), "UTF-8"));
        w.println("foo+\tbar-\tTAG1");
        w.close();

        PrintStream err = System.err;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        try {
            System.setErr(new PrintStream(baos, true, "UTF-8"));
            MorphEncodingTool.main(new String[] { 
                "--input", input.getAbsolutePath(), 
                "--output", output.getAbsolutePath(),
                "-e", "suffix",
                "--annotation", "+"});
        } finally {
            System.err.flush();
            System.setErr(err);
        }

        Assertions.assertThat(new String(baos.toByteArray(), Charsets.UTF_8))
            .contains("contain the annotation byte");
    }

    private void checkEntry(DictionaryLookup dl, String word, String base, String tag) {
        List<WordData> lookup = dl.lookup(word);
        Assertions.assertThat(lookup.size()).isEqualTo(1);
        WordData wordData = lookup.get(0);
        Assertions.assertThat(wordData.getWord().toString()).isEqualTo(word);
        Assertions.assertThat(wordData.getStem().toString()).isEqualTo(base);
        Assertions.assertThat(wordData.getTag().toString()).isEqualTo(tag);
    }
}