Example #1
0
        /// <summary>Preprocess data set to create dictionary.</summary>
        /// <param name="examples">.</param>
        public override void PreProcess(IEnumerable <object> examples)
        {
            var q = from s in examples select Ject.Get(s, this.Name).ToString();

            if (this.AsEnum)
            {
                this.Dictionary = StringHelpers.BuildEnumDictionary(q).Select(kv => kv.Key).ToArray();
            }
            else
            {
                switch (this.SplitType)
                {
                case StringSplitType.Character:
                    this.Dictionary =
                        StringHelpers.BuildCharDictionary(q, this.Exclude).Select(kv => kv.Key).ToArray();
                    break;

                case StringSplitType.Word:
                    this.Dictionary =
                        StringHelpers.BuildWordDictionary(q, this.Separator, this.Exclude)
                        .Select(kv => kv.Key)
                        .ToArray();
                    break;
                }
            }
        }
        public void Test_Vector_Conversion_Simple_Numbers_And_Chars_As_Enum()
        {
            Descriptor d = new Descriptor();

            var dictionary = StringHelpers.BuildCharDictionary(ShortStrings)
                             .Select(k => k.Key)
                             .ToArray();

            d.Features = new Property[]
            {
                new Property {
                    Name = "Age"
                },
                new Property {
                    Name = "Height"
                },
                new StringProperty {
                    Name       = "Chars",
                    Dictionary = dictionary,
                    SplitType  = StringSplitType.Character,
                    AsEnum     = true
                },
                new Property {
                    Name = "Weight"
                },
                new Property {
                    Name = "Good"
                },
            };

            // ["O",  "N",  "E", "#SYM#", "T", "W", "#NUM#", "H", "R"]
            // [  2,    1,   0,        1,   0,   0,       1,   0,   3]

            var o = new { Age = 23, Height = 6.21d, Chars = "N", Weight = 220m, Good = false, };

            // array generated by descriptor ordering
            var truths = new double[] { 23, 6.21,
                                        /* BEGIN CHARS */
                                        1,
                                        /* END CHARS */
                                        220, -1 };

            var actual = d.Convert(o);

            Assert.AreEqual(truths, actual);


            o = new { Age = 23, Height = 6.21d, Chars = "!", Weight = 220m, Good = false, };

            // array generated by descriptor ordering
            truths = new double[] { 23, 6.21,
                                    /* BEGIN CHARS */
                                    3,
                                    /* END CHARS */
                                    220, -1 };

            actual = d.Convert(o);
            Assert.AreEqual(truths, actual);
        }
        public void Test_Char_Dictionary_Gen()
        {
            var d = StringHelpers.BuildCharDictionary(ShortStrings);
            // should have the following:
            // O(2), N(1), E(5), #SYM#(5), T(3), W(1), #NUM#(1), H(2), R(2)
            var dict = new Dictionary <string, double>
            {
                { "O", 2 },
                { "N", 1 },
                { "E", 5 },
                { "#SYM#", 5 },
                { "T", 3 },
                { "W", 1 },
                { "#NUM#", 1 },
                { "H", 2 },
                { "R", 2 },
            };

            Assert.AreEqual(dict, d);
        }