@@ -425,11 +425,99 @@ public void checkJSON() throws IOException, FTAException {
425425 final String [] inputsRE = {
426426 "2345:AQ" , "5993:FG" , "3898:WW" , "5543:NH" , "1992:WW" , "4002:CS" , "5982:KG" , "1090:DD" , "3030:XX" , "1088:TR" ,
427427 "2547:DE" , "6587:DS" , "3215:QQ" , "7745:VD" , "4562:DD" , "4582:SS" , "2257:WE" , "3578:HT" , "4568:FB" , "1587:SW" ,
428- "4573:LF" , "3574:SS" , "8122:GK" , "4523:EW" , "7128:RT" , "2548:RF" , "6873:HH" , "4837:NR" , "2358:EE" , "3731:HY"
428+ "4573:LF" , "3574:SS" , "8122:GK" , "4523:EW" , "7128:RT" , "2548:RF" , "6873:HH" , "4837:NR" , "2358:EE" , "3731:HY" ,
429+ "0010:AA" , "0011:DB" , "0012:FT" , "0013:KG" , "0014:EM" , "0015:RP" , "0016:TA" , "0017:AP" , "0018:AA" , "0019:AA" ,
430+ "0020:QA" , "0021:UT" , "0022:AA" , "0023:AA" , "0024:AQ" , "0025:PA" , "0026:AA" , "0027:AA" , "0028:AQ" , "0029:AG" ,
431+ "0030:OA" , "0031:AA" , "0032:AA" , "0033:AA" , "0034:AA" , "0035:NA" , "0036:AI" , "0037:AI" , "0038:AA" , "0039:FA" ,
432+ "0040:AH" , "0041:AL" , "0042:AL" , "0043:EA" , "0044:AA" , "0045:WA" , "0046:AA" , "0047:QA" , "0048:AS" , "0049:XA" ,
433+ "0050:AS" , "0051:ZA" , "0052:AA" , "0053:BS" , "0054:JA" , "0055:MA" , "0056:AA" , "0057:AV" , "0058:AC" , "0059:AA" ,
434+ "0060:AA" , "0061:AC" , "0062:AA" , "0063:AD" , "0064:AF" , "0065:AA" , "0066:AZ" , "0067:AA" , "0068:AK" , "0069:AI" ,
435+ "0070:AA" , "0071:AA" , "0072:AA" , "0073:AA" , "0074:AF" , "0075:AG" , "0076:AY" , "0077:AI" , "0078:AU" , "0079:AA"
429436 };
430437
438+ final String [] primaryColors = {
439+ "Red" , "Green" , "Green" , "Red" , "Red" , "Blue" , "Green" , "Green" , "Green" , "Blue" ,
440+ "Green" , "Blue" , "Blue" , "Red" , "Red" , "Blue" , "Green" , "Blue" , "Blue" , "Red" ,
441+ "Blue" , "Green" , "Blue"
442+ };
443+
444+ @ Test (groups = { TestGroups .ALL , TestGroups .RANDOM })
445+ public void issue155_serialize_pre () throws FTAPluginException , FTAUnsupportedLocaleException , FTAMergeException {
446+ // Load our new plugins from a file and test the new Regular Expression Semantic Type
447+ TextAnalyzer analysis = new TextAnalyzer ("ID" );
448+ analysis .setLocale (Locale .forLanguageTag ("en-US" ));
449+
450+ // Register our sample list and regex plugins from a JSON definition file (before the built-in plugins have been registered)
451+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (TextAnalyzer .class .getResourceAsStream ("/custom_id.json" ), StandardCharsets .UTF_8 ))) {
452+ analysis .getPlugins ().registerPlugins (reader , analysis .getConfig (), true );
453+ } catch (FTAPluginException e ) {
454+ System .err .println ("ERROR: Failed to register plugin: " + (e .getCause () != null ? e .getCause ().getMessage () : e .getMessage ()));
455+ } catch (IOException e ) {
456+ System .err .println ("ERROR: Failed to register plugin: " + e .getMessage ());
457+ }
458+
459+ TextAnalyzer hydrated = TextAnalyzer .deserialize (analysis .serialize ());
460+
461+ for (final String input : inputsRE )
462+ hydrated .train (input );
463+
464+ TextAnalysisResult result = hydrated .getResult ();
465+
466+ assertEquals (result .getSemanticType (), "CUSTOM.DIGIT_ALPHA_ID" );
467+ }
468+
469+ @ Test (groups = { TestGroups .ALL , TestGroups .RANDOM })
470+ public void issue155_serialize_color_pre () throws FTAPluginException , FTAUnsupportedLocaleException , FTAMergeException {
471+ // Load our new plugins from a file and test the new Regular Expression Semantic Type
472+ TextAnalyzer analysis = new TextAnalyzer ("Color" );
473+ analysis .setLocale (Locale .forLanguageTag ("en-US" ));
474+
475+ // Register our sample list and regex plugins from a JSON definition file (before the built-in plugins have been registered)
476+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (TextAnalyzer .class .getResourceAsStream ("/custom_id.json" ), StandardCharsets .UTF_8 ))) {
477+ analysis .getPlugins ().registerPlugins (reader , analysis .getConfig (), true );
478+ } catch (FTAPluginException e ) {
479+ System .err .println ("ERROR: Failed to register plugin: " + (e .getCause () != null ? e .getCause ().getMessage () : e .getMessage ()));
480+ } catch (IOException e ) {
481+ System .err .println ("ERROR: Failed to register plugin: " + e .getMessage ());
482+ }
483+
484+ TextAnalyzer hydrated = TextAnalyzer .deserialize (analysis .serialize ());
485+
486+ for (final String input : primaryColors )
487+ hydrated .train (input );
488+
489+ TextAnalysisResult result = hydrated .getResult ();
490+
491+ assertEquals (result .getSemanticType (), "CUSTOM.PRIMARY_COLOR" );
492+ }
493+
494+ @ Test (groups = { TestGroups .ALL , TestGroups .RANDOM })
495+ public void issue155_serialize_color_post () throws FTAPluginException , FTAUnsupportedLocaleException , FTAMergeException {
496+ // Load our new plugins from a file and test the new Regular Expression Semantic Type
497+ TextAnalyzer analysis = new TextAnalyzer ("Color" );
498+ analysis .setLocale (Locale .forLanguageTag ("en-US" ));
499+
500+ // Register our sample list and regex plugins from a JSON definition file (before the built-in plugins have been registered)
501+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (TextAnalyzer .class .getResourceAsStream ("/custom_id.json" ), StandardCharsets .UTF_8 ))) {
502+ analysis .getPlugins ().registerPlugins (reader , analysis .getConfig (), false );
503+ } catch (FTAPluginException e ) {
504+ System .err .println ("ERROR: Failed to register plugin: " + (e .getCause () != null ? e .getCause ().getMessage () : e .getMessage ()));
505+ } catch (IOException e ) {
506+ System .err .println ("ERROR: Failed to register plugin: " + e .getMessage ());
507+ }
508+
509+ TextAnalyzer hydrated = TextAnalyzer .deserialize (analysis .serialize ());
510+
511+ for (final String input : primaryColors )
512+ hydrated .train (input );
513+
514+ TextAnalysisResult result = hydrated .getResult ();
515+
516+ assertEquals (result .getSemanticType (), "COLOR.TEXT_EN" );
517+ }
518+
431519 @ Test (groups = { TestGroups .ALL , TestGroups .RANDOM })
432- public void issue155 () throws FTAPluginException , FTAUnsupportedLocaleException , FTAMergeException {
520+ public void issue155_merge () throws FTAPluginException , FTAUnsupportedLocaleException , FTAMergeException {
433521 // Load our new plugins from a file and test the new Regular Expression Semantic Type
434522 TextAnalyzer shardOne = new TextAnalyzer ("ID" );
435523 shardOne .setLocale (Locale .forLanguageTag ("en-US" ));
@@ -457,7 +545,7 @@ public void issue155() throws FTAPluginException, FTAUnsupportedLocaleException,
457545 shardTwo .train (input );
458546 }
459547
460- TextAnalyzer merged = TextAnalyzer .merge (shardOne , shardTwo );
548+ TextAnalyzer merged = TextAnalyzer .merge (TextAnalyzer . deserialize ( shardOne . serialize ()), TextAnalyzer . deserialize ( shardTwo . serialize ()) );
461549 TextAnalysisResult result = merged .getResult ();
462550
463551 assertEquals (result .getSemanticType (), "CUSTOM.DIGIT_ALPHA_ID" );
0 commit comments