ReadWriteMentionsTest.java
4.44 KB
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
package ipipan.clarin.tei.impl.io;
import static org.junit.Assert.assertEquals;
import ipipan.clarin.tei.api.entities.AnnotationLayer;
import ipipan.clarin.tei.api.entities.TEICorpusText;
import ipipan.clarin.tei.api.entities.TEIMention;
import ipipan.clarin.tei.api.entities.TEIMorph;
import ipipan.clarin.tei.api.entities.TEISentence;
import ipipan.clarin.tei.api.exceptions.TEIException;
import ipipan.clarin.tei.api.io.TEI_IO;
import ipipan.clarin.tei.api.io.TEI_IO.CompressionMethod;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
*
* @author mkopec
*/
public class ReadWriteMentionsTest {
private final static Logger logger = Logger
.getLogger(ReadWriteMentionsTest.class);
private final static File TEST_DIR = new File(
"src/test/resources/example_text_with_mentions");
@Rule
public TemporaryFolder TEST_TMP_DIR = new TemporaryFolder();
private final TEI_IO teiIO = TEI_IO.getInstance();
public ReadWriteMentionsTest() {
logger.info(System.getProperty("user.dir"));
}
@BeforeClass
public static void setUpClass() throws Exception {
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ALL);
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() throws IOException {
logger.info(System.getProperty("user.dir"));
}
@After
public void tearDown() {
}
@Test
public void testRead() throws TEIException {
logger.info("testWypluwkaInput");
TEICorpusText tei = teiIO.readFromNKJPDirectory(TEST_DIR);
logger.info("done read");
doTest(tei);
}
@Test
public void testWriteDir() throws TEIException {
logger.info("testWypluwkaInput");
TEICorpusText tei = teiIO.readFromNKJPDirectory(TEST_DIR);
logger.info("done read");
teiIO.writeToNKJPDirectory(tei, this.TEST_TMP_DIR.getRoot(),
CompressionMethod.NONE);
tei = teiIO.readFromNKJPDirectory(this.TEST_TMP_DIR.getRoot());
doTest(tei);
}
@Test
public void testWritePackage() throws IOException {
logger.info("testWypluwkaInput");
TEICorpusText tei = teiIO.readFromNKJPDirectory(TEST_DIR);
logger.info("done read");
File newFile = TEST_TMP_DIR.newFile();
teiIO.writeToPackage(newFile, tei);
tei = teiIO.readFromPackage(newFile);
doTest(tei);
}
private void doTest(TEICorpusText tei) {
assertEquals(EnumSet.copyOf(Arrays.asList(AnnotationLayer.TEXT,
AnnotationLayer.SEGMENTATION, AnnotationLayer.MORPHOSYNTAX,
AnnotationLayer.NAMES, AnnotationLayer.WORDS,
AnnotationLayer.GROUPS, AnnotationLayer.MENTIONS)),
tei.getAnnotationLayers());
doTestMentionsLayer(tei);
}
private void doTestMentionsLayer(TEICorpusText tei) {
TEISentence firstSentence = tei.getAllSentences().get(0);
TEISentence lastSentence = tei.getAllSentences().get(13);
// number of mentions in sentences
assertEquals(11, firstSentence.getAllMentions().size());
assertEquals(11, lastSentence.getAllMentions().size());
// zero subject mentions
assertEquals(firstSentence.getAllMentions().get(0).isZeroSubject(),
true);
assertEquals(firstSentence.getAllMentions().get(1).isZeroSubject(),
false);
// mentions
doTestMention(firstSentence, 0, "mention_0",
Arrays.asList("morph_1.1.2-seg"),
Arrays.asList("morph_1.1.2-seg"));
doTestMention(firstSentence, 7, "mention_7", Arrays.asList(
"morph_1.1.22-seg", "morph_1.1.23-seg", "morph_1.1.24-seg",
"morph_1.1.25-seg"), Arrays.asList("morph_1.1.22-seg"));
doTestMention(lastSentence, 8, "mention_90", Arrays.asList(
"morph_4.14.24-seg", "morph_4.14.25-seg", "morph_4.14.26-seg"),
Arrays.asList("morph_4.14.24-seg"));
}
private void doTestMention(TEISentence sentence, int mentionIdx,
String mentionId, List<String> morphsIds, List<String> headsIds) {
TEIMention teiMention = sentence.getAllMentions().get(mentionIdx);
assertEquals(mentionId, teiMention.getId());
List<String> ids = new ArrayList<String>();
for (TEIMorph m : teiMention.getMorphs())
ids.add(m.getId());
assertEquals(morphsIds, ids);
ids.clear();
for (TEIMorph m : teiMention.getHeadMorphs())
ids.add(m.getId());
assertEquals(headsIds, ids);
}
}