Пример #1
0
        /// <summary>Generates a feature.</summary>
        /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception>
        /// <param name="type">The type.</param>
        /// <param name="name">The name.</param>
        /// <returns>The feature.</returns>
        public static Property GenerateFeature(Type type, string name)
        {
            Property p;
            if (type == typeof(string))
                p = new StringProperty();
            else if (type == typeof(DateTime))
                p = new DateTimeProperty();
            else if (type.GetInterfaces().Contains(typeof(IEnumerable)))
                throw new InvalidOperationException(
                    string.Format("Property {0} needs to be labeled as an EnumerableFeature", name));
            else
                p = new Property();


            p.Discrete = type.BaseType == typeof(Enum) ||
                         type == typeof(bool) ||
                         type == typeof(string) ||
                         type == typeof(char) ||
                         type == typeof(DateTime);

            p.Type = type;
            p.Name = name;

            return p;
        }
Пример #2
0
 /// <summary>
 /// Adds the default string property to descriptor with previously chained name.
 /// </summary>
 /// <returns>descriptor with added property.</returns>
 public Descriptor AsString()
 {
     StringProperty p = new StringProperty();
     p.Name = _name;
     p.AsEnum = _label;
     AddProperty(p);
     return _descriptor;
 }
Пример #3
0
 /// <summary>Adds string property to descriptor with previously chained name.</summary>
 /// <param name="splitType">How to split string.</param>
 /// <param name="separator">(Optional) Separator to use.</param>
 /// <param name="exclusions">(Optional) file describing strings to exclude.</param>
 /// <returns>descriptor with added property.</returns>
 public Descriptor AsString(StringSplitType splitType, string separator = " ", string exclusions = null)
 {
     StringProperty p = new StringProperty();
     p.Name = _name;
     p.SplitType = splitType;
     p.Separator = separator;
     p.ImportExclusions(exclusions);
     p.AsEnum = _label;
     AddProperty(p);
     return _descriptor;
 }
Пример #4
0
        public void String_Property_Save_And_Load()
        {
            StringProperty p = new StringProperty();
            p.Name = "MyProp";
            p.Start = 5;

            p.Dictionary = new string[] { "ONE", "TWO", "THREE", "FOUR" };
            p.Exclude = new string[] { "FIVE", "SIX", "SEVEN" };

            Serialize(p);
            var property = Deserialize<StringProperty>();
            Assert.Equal(p, property);
        }
Пример #5
0
        public void String_Property_Save_And_Load()
        {
            StringProperty p = new StringProperty();
            p.Name = "MyProp";
            p.Start = 5;

            p.Dictionary = new string[] { "ONE", "TWO", "THREE", "FOUR" };
            p.Exclude = new string[] { "FIVE", "SIX", "SEVEN" };

            Serialize(p);

            var po = Deserialize<StringProperty>();

            Assert.AreEqual(p.Name, po.Name);
            Assert.AreEqual(p.Type, po.Type);
            Assert.AreEqual(p.Discrete, po.Discrete);
            Assert.AreEqual(p.Start, po.Start);
            Assert.AreEqual(p.Dictionary, po.Dictionary);
            Assert.AreEqual(p.Exclude, po.Exclude);
            Assert.AreEqual(p.AsEnum, po.AsEnum);
            Assert.AreEqual(p.SplitType, po.SplitType);
        }
Пример #6
0
        /// <summary>Generates a property.</summary>
        /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception>
        /// <param name="property">The property.</param>
        /// <returns>The property.</returns>
        public override Property GenerateProperty(PropertyInfo property)
        {
            if (property.PropertyType != typeof(string))
            {
                throw new InvalidOperationException("Must use a string property.");
            }

            var sp = new StringProperty
            {
                Name      = property.Name,
                SplitType = SplitType,
                Separator = Separator,
                AsEnum    = AsEnum,
                Discrete  = true
            };

            if (!string.IsNullOrWhiteSpace(ExclusionFile))
            {
                sp.ImportExclusions(ExclusionFile);
            }


            return(sp);
        }
Пример #7
0
        public override Property GenerateProperty(PropertyInfo property)
        {
            if (property.PropertyType != typeof(string))
                throw new InvalidOperationException("Must use a string property.");

            var sp = new StringProperty
            {
                Name = property.Name,
                SplitType = SplitType,
                Separator = Separator,
                AsEnum = AsEnum,
                Discrete = true
            };

            sp.ImportExclusions(ExclusionFile);

            return sp;
        }
Пример #8
0
        public static double[] GetWordCount(string item, StringProperty property)
        {
            double[] counts = new double[property.Dictionary.Length];
            var d = new Dictionary<string, int>();

            for (int i = 0; i < counts.Length; i++)
            {
                counts[i] = 0;
                // for quick index lookup
                d.Add(property.Dictionary[i], i);
            }

            // get list of words (or chars) from source
            IEnumerable<string> words = property.SplitType == StringSplitType.Character ?
                                                 GetChars(item) :
                                                 GetWords(item, property.Separator);

            // TODO: this is not too efficient. Perhaps reconsider how to do this
            foreach (var s in words)
                if (property.Dictionary.Contains(s))
                    counts[d[s]]++;

            return counts;
        }
Пример #9
0
        /// <summary>Gets word count.</summary>
        /// <param name="item">The item.</param>
        /// <param name="property">The property.</param>
        /// <returns>An array of double.</returns>
        public static double[] GetWordCount(string item, StringProperty property)
        {
            // get list of words (or chars) from source
            IEnumerable<string> words = property.SplitType == StringSplitType.Character ?
                                                 GetChars(item) :
                                                 GetWords(item, property.Separator);

            var groupedWords = words.ToLookup(w => w);
            return property.Dictionary.Select(d => (double)groupedWords[d].Count()).ToArray();
        }
Пример #10
0
 /// <summary>Adds string property to descriptor with previously chained name.</summary>
 /// <returns>descriptor with added property.</returns>
 public Descriptor AsStringEnum()
 {
     var p = new StringProperty();
     p.Name = this._name;
     p.AsEnum = true;
     this.AddProperty(p);
     return this._descriptor;
 }