/// <summary>
        /// Creates the artifact serializers for the <see cref="BaseToolFactory.ArtifactProvider"/>.
        /// The subclasses should call the <see cref="SharpNL.Utility.Serialization.ArtifactProvider.RegisterArtifactType"/>
        /// </summary>
        /// <param name="provider">The provider.</param>
        public override void CreateArtifactSerializers(ArtifactProvider provider) {
            provider.RegisterArtifactType(Path.GetExtension(DummyDict), (artifact, stream) => {
                var dictionary = artifact as DummyDictionary;
                if (dictionary == null)
                    throw new InvalidOperationException();

                dictionary.Serialize(stream);
            }, stream => new DummyDictionary(stream));
        }
Example #2
0
        /// <summary>
        /// Creates the artifact serializers for the <see cref="BaseToolFactory.ArtifactProvider"/>.
        /// The subclasses should call the <see cref="SharpNL.Utility.Serialization.ArtifactProvider.RegisterArtifactType"/> method to register an new artifact type.
        /// </summary>
        /// <param name="provider">The artifact provider.</param>
        public override void CreateArtifactSerializers(ArtifactProvider provider)
        {
            base.CreateArtifactSerializers(provider);

            // TODO: the ITagDictionary is not extensive :(

            provider.RegisterArtifactType(".tagdict",
                                          (artifact, stream) => ((POSDictionary)artifact).Serialize(stream),
                                          stream => new POSDictionary(stream)
                                          );
        }
Example #3
0
        /// <summary>
        /// Gets the sequence validator.
        /// </summary>
        public override ISequenceValidator <string> GetSequenceValidator()
        {
            var model = ArtifactProvider.GetArtifact <IMaxentModel>("chunker.model");

            if (model == null)
            {
                throw new InvalidOperationException("Unable to retrieve the chunker.model artifact.");
            }

            return(new ParserChunkerSequenceValidator(model.GetOutcomes()));
        }
Example #4
0
        /// <summary>
        /// Creates the artifact serializers for the <see cref="BaseToolFactory.ArtifactProvider"/>.
        /// The subclasses should call the <see cref="SharpNL.Utility.Serialization.ArtifactProvider.RegisterArtifactType"/>
        /// </summary>
        /// <param name="provider">The provider.</param>
        public override void CreateArtifactSerializers(ArtifactProvider provider)
        {
            provider.RegisterArtifactType(Path.GetExtension(DummyDict), (artifact, stream) => {
                var dictionary = artifact as DummyDictionary;
                if (dictionary == null)
                {
                    throw new InvalidOperationException();
                }

                dictionary.Serialize(stream);
            }, stream => new DummyDictionary(stream));
        }
Example #5
0
        /// <summary>
        /// Validates the parsed artifacts.
        /// </summary>
        /// <exception cref="InvalidFormatException">Invalid artifact map.</exception>
        public override void ValidateArtifactMap()
        {
            // Ensure that the tag dictionary is compatible with the model

            var tagDicEntry = ArtifactProvider.GetArtifact <ITagDictionary>(TagDictionaryEntryName);

            if (tagDicEntry != null && !ArtifactProvider.IsLoadedFromSerialized)
            {
                ValidatePOSDictionary((POSDictionary)tagDicEntry, Model);
            }

            dictionary = ArtifactProvider.GetArtifact <Dictionary.Dictionary>(NgramDictionaryEntryName);
        }
        /// <summary>
        /// Creates the <see cref="IAdaptiveFeatureGenerator"/>.
        /// Usually this is a set of generators contained in the <see cref="AggregatedFeatureGenerator"/>.
        /// </summary>
        /// <returns>The feature generator or null if there is no descriptor in the model.</returns>
        public virtual IAdaptiveFeatureGenerator CreateFeatureGenerators()
        {
            byte[] descriptorBytes;
            if (FeatureGenerator == null && ArtifactProvider != null)
            {
                descriptorBytes = ArtifactProvider.GetArtifact <byte[]>(TokenNameFinderModel.GeneratorDescriptorEntry);
            }
            else
            {
                descriptorBytes = FeatureGenerator;
            }

            if (descriptorBytes != null)
            {
                var descriptorIn = new MemoryStream(descriptorBytes);

                try {
                    return(GeneratorFactory.Create(descriptorIn, identifier => {
                        try {
                            if (ArtifactProvider != null)
                            {
                                return ArtifactProvider.GetArtifact <object>(identifier);
                            }
                            if (Resources.ContainsKey(identifier))
                            {
                                return Resources[identifier];
                            }

                            return null;
                        } catch (Exception ex) {
                            throw new FeatureGeneratorException("A exception with the feature generator has occured.",
                                                                ex);
                        }
                    }));
                } catch (InvalidFormatException ex) {
                    throw new FeatureGeneratorException(
                              // It is assumed that the creation of the feature generation does not
                              // fail after it succeeded once during model loading.

                              // But it might still be possible that such an exception is thrown,
                              // in this case the caller should not be forced to handle the exception
                              // and a Runtime Exception is thrown instead.

                              // If the re-creation of the feature generation fails it is assumed
                              // that this can only be caused by a programming mistake and therefore
                              // throwing a Runtime Exception is reasonable
                              "An error during the creation or re-creation of a feature generator has occurred.", ex);
                }
            }
            return(null);
        }
        /// <summary>
        /// Validates the parsed artifacts.
        /// </summary>
        /// <exception cref="InvalidFormatException">Invalid artifact map.</exception>
        public override void ValidateArtifactMap()
        {
            if (ArtifactProvider.Manifest.Contains(USE_ALPHA_NUMERIC_OPTIMIZATION))
            {
                throw new InvalidFormatException(USE_ALPHA_NUMERIC_OPTIMIZATION + " is a mandatory property!");
            }

            var abbreviationsEntry = ArtifactProvider.GetArtifact <object>(ABBREVIATIONS_ENTRY_NAME);

            if (abbreviationsEntry != null && !(abbreviationsEntry is Dictionary.Dictionary))
            {
                throw new InvalidFormatException("Abbreviations dictionary is not an valid dictionary.");
            }
        }
Example #8
0
        /// <summary>
        /// Validates the parsed artifacts.
        /// </summary>
        /// <exception cref="InvalidFormatException">Invalid artifact map.</exception>
        public override void ValidateArtifactMap()
        {
            if (ArtifactProvider.Manifest[TOKEN_END_PROPERTY] == null)
            {
                throw new InvalidFormatException(TOKEN_END_PROPERTY + " is a mandatory property!");
            }

            var abbreviationsEntry = ArtifactProvider.GetArtifact <Dic>(ABBREVIATIONS_ENTRY_NAME);

            if (abbreviationsEntry == null)
            {
                throw new InvalidFormatException(
                          "Abbreviations dictionary has wrong type, needs to be of type Dictionary!");
            }
        }
Example #9
0
        /// <summary>
        /// Creates the artifact serializers for the <see cref="BaseToolFactory.ArtifactProvider"/>.
        /// The subclasses should call the <see cref="SharpNL.Utility.Serialization.ArtifactProvider.RegisterArtifactType"/> method to register an new artifact type.
        /// </summary>
        /// <param name="provider">The artifact provider.</param>
        public override void CreateArtifactSerializers(ArtifactProvider provider) {
            base.CreateArtifactSerializers(provider);

            // TODO: the ITagDictionary is not extensive :(

            provider.RegisterArtifactType(".tagdict", 
                (artifact, stream) => ((POSDictionary) artifact).Serialize(stream), 
                stream => new POSDictionary(stream)
                );

        }
Example #10
0
 /// <summary>
 /// Creates the artifact serializers for the <see cref="ArtifactProvider"/>.
 /// The subclasses should call the <see cref="Serialization.ArtifactProvider.RegisterArtifactType"/> method to register an new artifact type.
 /// </summary>
 /// <param name="provider">The artifact provider.</param>
 public virtual void CreateArtifactSerializers(ArtifactProvider provider)
 {
     // nothing to do here...
 }
Example #11
0
 /// <summary>
 /// Initializes the tool factory with the specified artifact provider.
 /// </summary>
 /// <param name="artifactProvider">The artifact provider.</param>
 internal void Initialize(ArtifactProvider artifactProvider)
 {
     ArtifactProvider = artifactProvider;
 }
Example #12
0
 /// <summary>
 /// Creates the artifact serializers for the <see cref="ArtifactProvider"/>.
 /// The subclasses should call the <see cref="Serialization.ArtifactProvider.RegisterArtifactType"/> method to register an new artifact type.
 /// </summary>
 /// <param name="provider">The artifact provider.</param>
 public virtual void CreateArtifactSerializers(ArtifactProvider provider) {
     // nothing to do here...
 }
Example #13
0
 /// <summary>
 /// Initializes the tool factory with the specified artifact provider.
 /// </summary>
 /// <param name="artifactProvider">The artifact provider.</param>
 internal void Initialize(ArtifactProvider artifactProvider) {
     ArtifactProvider = artifactProvider;
 }