protected override void CreateArtifactSerializers() { base.CreateArtifactSerializers(); // note from OpenNLP (for future adaptations) // In 1.6.x the headrules artifact is serialized with the new API // which uses the Serializable interface // This change is not backward compatible with the 1.5.x models. // In order to load 1.5.x model the English headrules serializer must be // put on the serializer map. RegisterArtifactType(".headrules", (artifact, stream) => HeadRulesManager.Serialize(artifact as AbstractHeadRules, stream), stream => HeadRulesManager.Deserialize(Language, stream)); RegisterArtifactType(".postagger", (artifact, stream) => { var model = artifact as POSModel; if (model == null) { throw new InvalidOperationException(); } model.Serialize(stream); }, stream => new POSModel(stream)); RegisterArtifactType(".chunker", (artifact, stream) => { var model = artifact as ChunkerModel; if (model == null) { throw new InvalidOperationException(); } model.Serialize(stream); }, stream => new ChunkerModel(stream)); }
/// <summary> /// Registers all serializers for their artifact file name extensions. Override this method to register custom file extensions. /// </summary> /// <seealso href="https://msdn.microsoft.com/en-us/library/ms182331.aspx" /> /// <remarks>The subclasses should invoke the <see cref="ArtifactProvider.RegisterArtifactType" /> to register /// the proper serialization/deserialization methods for an new extension. /// Warning: This method is called in constructor of the base class!! Be aware that this method is ONLY designed to register serializers.</remarks> protected override void CreateArtifactSerializers() { base.CreateArtifactSerializers(); // note from OpenNLP (for future adaptations) // In 1.6.x the headrules artifact is serialized with the new API // which uses the Serializable interface // This change is not backward compatible with the 1.5.x models. // In order to load 1.5.x model the English headrules serializer must be // put on the serializer map. RegisterArtifactType(".headrules", (artifact, stream) => HeadRulesManager.Serialize(artifact as AbstractHeadRules, stream), stream => HeadRulesManager.Deserialize(Language, stream)); RegisterArtifactType(".postagger", (artifact, stream) => { var model = artifact as POSModel; if (model == null) { throw new InvalidOperationException(); } model.Serialize(stream); }, stream => { var model = new POSModel(stream); // The 1.6.x models write the non-default beam size into the model itself. // In 1.5.x the parser configured the beam size when the model was loaded, // this is not possible anymore with the new APIs if (model.Version.Major == 1 && model.Version.Minor == 5 && !model.Manifest.Contains(Parameters.BeamSize)) { return(new POSModel(model.Language, model.MaxentModel, 10, null, model.Factory)); } return(model); }); RegisterArtifactType(".chunker", (artifact, stream) => { var model = artifact as ChunkerModel; if (model == null) { throw new InvalidOperationException(); } model.Serialize(stream); }, stream => { var model = new ChunkerModel(stream); if (model.Version.Major == 1 && model.Version.Minor == 5) { return(new ChunkerModel(model.Language, model.MaxentModel, new ParserChunkerFactory())); } return(model); }); }
internal static AbstractHeadRules CreateTestHeadRules() { AbstractHeadRules rules; using (var file = Tests.OpenFile("/opennlp/tools/parser/en_head_rules")) { Assert.NotNull(file); rules = HeadRulesManager.Deserialize("en", file); Assert.NotNull(rules); } return(rules); }