/// <summary>
        /// Constructor which takes a <see cref="PerceptronModel"/> and a <see cref="Stream"/> and prepares
        /// itself to write the model to that writer.
        /// </summary>
        /// <param name="model">The <seealso cref="PerceptronModel"/> which is to be persisted.</param>
        /// <param name="outputStream">The output stream.</param>
        /// <exception cref="System.ArgumentNullException">outputStream</exception>
        /// <exception cref="System.ArgumentException">Stream was not writable.</exception>
        public PlainTextPerceptronModelWriter(AbstractModel model, Stream outputStream) : base(model) {
            if (outputStream == null)
                throw new ArgumentNullException("outputStream");

            if (!outputStream.CanWrite)
                throw new ArgumentException(@"Stream was not writable.", "outputStream");

            writer = new StreamWriter(outputStream);
        }
Exemplo n.º 2
0
        /// <para>
        ///   <c>index 0</c>: <see cref="T:Context[]"/> containing the model parameters.
        ///   <c>index 1</c>: <see cref="T:IndexHashTable{string}"/> containing the mapping of model predicates to unique integers.
        ///   <c>index 2</c>: <see cref="T:string[]"/> containing the names of the outcomes, stored in the index of the array which represents their unique ids in the model.
        ///   <c>index 3</c>: <see cref="T:double"/> containing the value of the models correction constant.
        ///   <c>index 4</c>: <see cref="T:double"/> containing the value of the models correction parameter.
        /// </para>
        /// 
        protected GISModelWriter(AbstractModel model) {
            var data = model.GetDataStructures();

            PARAMS = (Context[]) data[0];
            var map = (IndexHashTable<string>) data[1];
            OUTCOME_LABELS = (string[]) data[2];
            CORRECTION_CONSTANT = (int) data[3]; // string[] ??????????
            CORRECTION_PARAM = (Double) data[4];

            PRED_LABELS = new String[map.Size];
            map.ToArray(PRED_LABELS);
        }
Exemplo n.º 3
0
        public static void WriteModel(AbstractModel model, Stream outStream) {
            if (model == null)
                throw new ArgumentNullException("model");

            if (outStream == null)
                throw new ArgumentNullException("outStream");

            if (!outStream.CanWrite)
                throw new ArgumentException("Stream was not writeable.");

            var writer = new GenericModelWriter(model, outStream);

            writer.Persist();
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GenericModelWriter" /> class.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="outputStream">The output stream.</param>
 /// <exception cref="System.InvalidOperationException">Invalid model type.</exception>
 public GenericModelWriter(AbstractModel model, Stream outputStream) {
     switch (model.ModelType) {
         case ModelType.Maxent:
             writer = new BinaryGISModelWriter(model, outputStream);
             break;
         case ModelType.MaxentQn:
             writer = new BinaryQNModelWriter(model, outputStream);
             break;
         case ModelType.Perceptron:
             writer = new BinaryPerceptronModelWriter(model, outputStream);
             break;
         default:
             throw new InvalidOperationException("Invalid model type");
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GenericModelWriter" /> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="outputStream">The output stream.</param>
        /// <exception cref="System.InvalidOperationException">Invalid model type.</exception>
        public GenericModelWriter(AbstractModel model, Stream outputStream)
        {
            switch (model.ModelType)
            {
            case ModelType.Maxent:
                writer = new BinaryGISModelWriter(model, outputStream);
                break;

            case ModelType.MaxentQn:
                writer = new BinaryQNModelWriter(model, outputStream);
                break;

            case ModelType.Perceptron:
                writer = new BinaryPerceptronModelWriter(model, outputStream);
                break;

            default:
                throw new InvalidOperationException("Invalid model type");
            }
        }
 /// <summary>
 /// Constructor which takes a <see cref="PerceptronModel"/> and a File and prepares itself to
 /// write the model to that file. Detects whether the file is GZipped or not based on whether
 /// the suffix contains ".gz".
 /// </summary>
 /// <param name="model">The PerceptronModel which is to be persisted.</param>
 /// <param name="fileName">The filename in which the model is to be persisted.</param>
 public PlainTextPerceptronModelWriter(AbstractModel model, string fileName) : base(model) {
     writer = fileName.EndsWith(".gz") 
         ? new StreamWriter(new GZipStream(new FileStream(fileName, FileMode.Create), CompressionMode.Compress)) 
         : new StreamWriter(new FileStream(fileName, FileMode.Create));
 }
Exemplo n.º 7
0
 protected void ValidatePOSDictionary(POSDictionary posDic, AbstractModel posModel) {
     if (!posModel.ContainsOutcomes(posDic)) {
         throw new InvalidFormatException("Tag dictionary contains tags which are unknown by the model!");
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Creates a new event array based on the outcomes predicted by the specified parameters for the specified sequence.
        /// </summary>
        /// <param name="sequence">The sequence to be evaluated.</param>
        /// <param name="model">The model.</param>
        /// <returns>The event array.</returns>
        public Event[] UpdateContext(Sequence sequence, AbstractModel model) {
            var tagger =
                new NameFinderME(
                    new TokenNameFinderModel("x-unspecified", model, new Dictionary<string, object>(), null));

            var sentence = sequence.GetSource<NameSample>().Sentence;

            var tags = seqCodec.Encode(tagger.Find(sentence), sentence.Length);

            return NameFinderEventStream.GenerateEvents(sentence, tags, pcg).ToArray();
        }
Exemplo n.º 9
0
 /// <summary>
 /// Creates a new event array based on the outcomes predicted by the specified parameters for the specified sequence.
 /// </summary>
 /// <param name="sequence">The sequence to be evaluated.</param>
 /// <param name="model">The model.</param>
 /// <returns>The event array.</returns>
 /// <remarks>Always return null.</remarks>
 public Event[] UpdateContext(Sequence sequence, AbstractModel model) {
     // TODO: Should be implemented for Perceptron sequence learning ...
     return null;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BinaryGISModelWriter"/> class.
 /// </summary>
 /// <param name="model">The GIS model.</param>
 /// <param name="outputStream">The output stream.</param>
 public BinaryGISModelWriter(AbstractModel model, Stream outputStream) : base(model) {
     writer = new BinaryFileDataWriter(outputStream);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a new event array based on the outcomes predicted by the specified parameters for the specified sequence.
        /// </summary>
        /// <param name="sequence">The sequence to be evaluated.</param>
        /// <param name="model">The model.</param>
        /// <returns>The event array.</returns>
        public Event[] UpdateContext(Sequence sequence, AbstractModel model) {
            var tagger = new POSTaggerME(new POSModel("x-unspecified", model, null, new POSTaggerFactory()));
            var sample = sequence.GetSource<POSSample>();
            var tags = tagger.Tag(sample.Sentence);

            return POSSampleEventStream.GenerateEvents(
                sample.Sentence,
                tags,
                Array.ConvertAll(sample.AdditionalContext, input => (object) input),
                contextGenerator).ToArray();
        }
Exemplo n.º 12
0
 public BinaryPerceptronModelWriter(AbstractModel model, Stream outStream) : base(model) {
     writer = new BinaryFileDataWriter(outStream);
 }