18
18
import edu .illinois .cs .cogcomp .core .datastructures .textannotation .Relation ;
19
19
import edu .illinois .cs .cogcomp .core .datastructures .textannotation .TextAnnotation ;
20
20
import edu .illinois .cs .cogcomp .core .datastructures .textannotation .View ;
21
+ import edu .illinois .cs .cogcomp .core .io .IOUtils ;
21
22
import edu .illinois .cs .cogcomp .core .utilities .DummyTextAnnotationGenerator ;
22
23
import edu .illinois .cs .cogcomp .core .utilities .JsonSerializer ;
23
24
import edu .illinois .cs .cogcomp .core .utilities .SerializationHelper ;
26
27
import org .slf4j .Logger ;
27
28
import org .slf4j .LoggerFactory ;
28
29
30
+ import java .io .IOException ;
29
31
import java .util .Arrays ;
30
32
import java .util .HashMap ;
31
33
import java .util .Map ;
32
34
import java .util .TreeMap ;
33
35
34
- import static org .junit .Assert .assertEquals ;
35
- import static org .junit .Assert .assertNotNull ;
36
+ import static org .junit .Assert .*;
36
37
37
38
/**
38
39
* Simple sanity tests for JsonSerializer.
39
40
*
40
41
* @author mssammon
41
42
*/
42
43
public class JsonSerializerTest {
44
+ private static final String RHYME_VIEW_NAME = "rhyme" ;
43
45
private static Logger logger = LoggerFactory .getLogger (JsonSerializerTest .class );
44
46
45
47
TextAnnotation ta = DummyTextAnnotationGenerator .generateAnnotatedTextAnnotation (new String [] {
@@ -122,7 +124,19 @@ public static void verifyDeserializedJsonString(String json, TextAnnotation ta)
122
124
123
125
@ Test
124
126
public void testSerializerWithCharOffsets () {
125
- View rhymeView = new View ("rhyme" , "test" , ta , 0.4 );
127
+
128
+ addRhymeViewToTa (ta );
129
+
130
+ String taJson = SerializationHelper .serializeToJson (ta , true );
131
+ logger .info (taJson );
132
+
133
+ JsonObject jobj = (JsonObject ) new JsonParser ().parse (taJson );
134
+ JsonSerializerTest .verifySerializedJSONObject (jobj , ta );
135
+ }
136
+
137
+
138
+ private static void addRhymeViewToTa (TextAnnotation someTa ) {
139
+ View rhymeView = new View (RHYME_VIEW_NAME , "test" , someTa , 0.4 );
126
140
127
141
Map < String , Double > newLabelsToScores = new TreeMap < String , Double >();
128
142
String [] labels = { "eeny" , "meeny" , "miny" , "mo" };
@@ -131,32 +145,26 @@ public void testSerializerWithCharOffsets() {
131
145
for ( int i = 0 ; i < labels .length ; ++i )
132
146
newLabelsToScores .put (labels [i ], scores [i ]);
133
147
134
- Constituent first = new Constituent ( newLabelsToScores , "rhyme" , ta , 2 , 4 );
148
+ Constituent first = new Constituent (newLabelsToScores , RHYME_VIEW_NAME , someTa , 2 , 4 );
135
149
rhymeView .addConstituent (first );
136
150
137
151
/**
138
152
* no constraint on scores -- don't have to sum to 1.0
139
153
*/
140
154
for ( int i = labels .length -1 ; i > 0 ; --i )
141
- newLabelsToScores .put ( labels [i ], scores [3 -i ] );
155
+ newLabelsToScores .put (labels [i ], scores [3 -i ]);
142
156
143
- Constituent second = new Constituent ( newLabelsToScores , "rhyme" , ta , 2 , 4 );
157
+ Constituent second = new Constituent (newLabelsToScores , RHYME_VIEW_NAME , someTa , 2 , 4 );
144
158
rhymeView .addConstituent (second );
145
159
146
160
Map <String , Double > relLabelsToScores = new TreeMap <>();
147
- relLabelsToScores .put ( "Yes" , 0.8 );
148
- relLabelsToScores .put ( "No" , 0.2 );
161
+ relLabelsToScores .put ("Yes" , 0.8 );
162
+ relLabelsToScores .put ("No" , 0.2 );
149
163
150
164
Relation rel = new Relation ( relLabelsToScores , first , second );
151
165
rhymeView .addRelation (rel );
152
166
153
- ta .addView ("rhyme" , rhymeView );
154
-
155
- String taJson = SerializationHelper .serializeToJson (ta , true );
156
- logger .info (taJson );
157
-
158
- JsonObject jobj = (JsonObject ) new JsonParser ().parse (taJson );
159
- JsonSerializerTest .verifySerializedJSONObject (jobj , ta );
167
+ someTa .addView (RHYME_VIEW_NAME , rhymeView );
160
168
}
161
169
162
170
@ Test
@@ -171,4 +179,71 @@ public void testJsonSerializabilityWithOffsets() throws Exception {
171
179
172
180
JsonSerializerTest .verifyDeserializedJsonString (json , ta );
173
181
}
182
+
183
+ /**
184
+ * make sure that if an already serialized TextAnnotation object is modified and reserialized,
185
+ * (and written to the same target file), that the file is updated correctly
186
+ */
187
+ @ Test
188
+ public void testJsonSerializedTaUpdate () {
189
+
190
+ // make sure we aren't using a TA already updated with "rhyme" view
191
+ TextAnnotation localTa = DummyTextAnnotationGenerator .generateAnnotatedTextAnnotation (new String [] {
192
+ ViewNames .POS , ViewNames .NER_CONLL , ViewNames .SRL_VERB }, false , 3 ); // no noise
193
+
194
+ String serTestDir = "serTestDir" ;
195
+ if (!IOUtils .exists (serTestDir ))
196
+ IOUtils .mkdir (serTestDir );
197
+ else if (IOUtils .isFile (serTestDir ))
198
+ throw new IllegalStateException ("ERROR: test directory " + serTestDir + " already exists as file." );
199
+ else
200
+ try {
201
+ IOUtils .cleanDir (serTestDir );
202
+ } catch (IOException e ) {
203
+ e .printStackTrace ();
204
+ throw new IllegalStateException ("ERROR: test directory " + serTestDir + " could not be cleaned. Permissions?" );
205
+ }
206
+ if (!IOUtils .getListOfFilesInDir (serTestDir ).isEmpty ())
207
+ throw new IllegalStateException ("ERROR: test directory " + serTestDir + " already contains files even after cleaning." );
208
+
209
+ String fileName = serTestDir + "/arbitrary.json" ;
210
+ boolean forceOverwrite = true ;
211
+ boolean useJson = true ;
212
+ try {
213
+ SerializationHelper .serializeTextAnnotationToFile (localTa , fileName , forceOverwrite , useJson );
214
+ } catch (IOException e ) {
215
+ e .printStackTrace ();
216
+ fail ("error trying to serialize json file " + fileName + "." );
217
+ }
218
+
219
+ TextAnnotation taDeser = null ;
220
+ try {
221
+ taDeser = SerializationHelper .deserializeTextAnnotationFromFile (fileName , useJson );
222
+ } catch (Exception e ) {
223
+ e .printStackTrace ();
224
+ fail ("error trying to deserialize json file " + fileName + "." );
225
+ }
226
+ assertTrue (taDeser .hasView (ViewNames .SRL_VERB ));
227
+ assertFalse (taDeser .hasView (RHYME_VIEW_NAME ));
228
+ addRhymeViewToTa (taDeser );
229
+ assertTrue (taDeser .hasView (RHYME_VIEW_NAME ));
230
+
231
+ try {
232
+ SerializationHelper .serializeTextAnnotationToFile (taDeser , fileName , forceOverwrite , useJson );
233
+ } catch (IOException e ) {
234
+ e .printStackTrace ();
235
+ fail ("error trying to serialize json file " + fileName + " for second time." );
236
+ }
237
+
238
+ TextAnnotation taDeserDeser = null ;
239
+ try {
240
+ taDeserDeser = SerializationHelper .deserializeTextAnnotationFromFile (fileName , useJson );
241
+ } catch (Exception e ) {
242
+ e .printStackTrace ();
243
+ fail ("error trying to deserialize json file " + fileName + " for second time." );
244
+ }
245
+
246
+ assertTrue (taDeserDeser .hasView (RHYME_VIEW_NAME ));
247
+ assertTrue (taDeserDeser .getView (RHYME_VIEW_NAME ).getConstituents ().size () > 0 );
248
+ }
174
249
}
0 commit comments