public virtual void TestCastStream()
 {
     NUnit.Framework.Assert.AreEqual(System.Console.Out, MetaClass.Cast("stdout", typeof(OutputStream)));
     NUnit.Framework.Assert.AreEqual(System.Console.Out, MetaClass.Cast("out", typeof(OutputStream)));
     NUnit.Framework.Assert.AreEqual(System.Console.Error, MetaClass.Cast("stderr", typeof(OutputStream)));
     NUnit.Framework.Assert.AreEqual(System.Console.Error, MetaClass.Cast("err", typeof(OutputStream)));
     NUnit.Framework.Assert.AreEqual(typeof(ObjectOutputStream), MetaClass.Cast("err", typeof(ObjectOutputStream)).GetType());
 }
示例#2
0
        public ParserAnnotator(string annotatorName, Properties props)
        {
            string model = props.GetProperty(annotatorName + ".model", LexicalizedParser.DefaultParserLoc);

            if (model == null)
            {
                throw new ArgumentException("No model specified for Parser annotator " + annotatorName);
            }
            this.Verbose = PropertiesUtils.GetBool(props, annotatorName + ".debug", false);
            string[] flags = ConvertFlagsToArray(props.GetProperty(annotatorName + ".flags"));
            this.parser            = LoadModel(model, Verbose, flags);
            this.maxSentenceLength = PropertiesUtils.GetInt(props, annotatorName + ".maxlen", -1);
            string treeMapClass = props.GetProperty(annotatorName + ".treemap");

            if (treeMapClass == null)
            {
                this.treeMap = null;
            }
            else
            {
                this.treeMap = ReflectionLoading.LoadByReflection(treeMapClass, props);
            }
            this.maxParseTime = PropertiesUtils.GetLong(props, annotatorName + ".maxtime", -1);
            this.kBest        = PropertiesUtils.GetInt(props, annotatorName + ".kbest", 1);
            this.keepPunct    = PropertiesUtils.GetBool(props, annotatorName + ".keepPunct", true);
            string buildGraphsProperty = annotatorName + ".buildgraphs";

            if (!this.parser.GetTLPParams().SupportsBasicDependencies())
            {
                if (PropertiesUtils.GetBool(props, buildGraphsProperty))
                {
                    log.Info("WARNING: " + buildGraphsProperty + " set to true, but " + this.parser.GetTLPParams().GetType() + " does not support dependencies");
                }
                this.BuildGraphs = false;
            }
            else
            {
                this.BuildGraphs = PropertiesUtils.GetBool(props, buildGraphsProperty, true);
            }
            if (this.BuildGraphs)
            {
                bool generateOriginalDependencies = PropertiesUtils.GetBool(props, annotatorName + ".originalDependencies", false);
                parser.GetTLPParams().SetGenerateOriginalDependencies(generateOriginalDependencies);
                ITreebankLanguagePack tlp         = parser.GetTLPParams().TreebankLanguagePack();
                IPredicate <string>   punctFilter = this.keepPunct ? Filters.AcceptFilter() : tlp.PunctuationWordRejectFilter();
                this.gsf = tlp.GrammaticalStructureFactory(punctFilter, parser.GetTLPParams().TypedDependencyHeadFinder());
            }
            else
            {
                this.gsf = null;
            }
            this.nThreads = PropertiesUtils.GetInt(props, annotatorName + ".nthreads", PropertiesUtils.GetInt(props, "nthreads", 1));
            bool usesBinary = StanfordCoreNLP.UsesBinaryTrees(props);

            this.saveBinaryTrees   = PropertiesUtils.GetBool(props, annotatorName + ".binaryTrees", usesBinary);
            this.noSquash          = PropertiesUtils.GetBool(props, annotatorName + ".nosquash", false);
            this.extraDependencies = MetaClass.Cast(props.GetProperty(annotatorName + ".extradependencies", "NONE"), typeof(GrammaticalStructure.Extras));
        }
 public static string[] GetStringArray(Properties props, string key, string[] defaults)
 {
     string[] results = MetaClass.Cast(props.GetProperty(key), typeof(string[]));
     if (results == null)
     {
         results = defaults;
     }
     return(results);
 }
示例#4
0
        public DependencyParseAnnotator(Properties properties)
        {
            string modelPath = PropertiesUtils.GetString(properties, "model", DependencyParser.DefaultModel);

            parser            = DependencyParser.LoadFromModelFile(modelPath, properties);
            nThreads          = PropertiesUtils.GetInt(properties, "testThreads", DefaultNthreads);
            maxTime           = PropertiesUtils.GetLong(properties, "sentenceTimeout", DefaultMaxtime);
            extraDependencies = MetaClass.Cast(properties.GetProperty("extradependencies", "NONE"), typeof(GrammaticalStructure.Extras));
        }
 public virtual void TestCastEnum()
 {
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.Apple, MetaClass.Cast("APPLE", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.Apple, MetaClass.Cast("apple", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.Apple, MetaClass.Cast("Apple", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.Apple, MetaClass.Cast("aPPlE", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.Orange, MetaClass.Cast("orange", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.grape, MetaClass.Cast("grape", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.grape, MetaClass.Cast("Grape", typeof(MetaClassTest.Fruits)));
     NUnit.Framework.Assert.AreEqual(MetaClassTest.Fruits.grape, MetaClass.Cast("GRAPE", typeof(MetaClassTest.Fruits)));
 }
        public virtual void TestCastStringArray()
        {
            string[] strings1 = MetaClass.Cast("[a,b,c]", typeof(string[]));
            Assert.AssertArrayEquals(new string[] { "a", "b", "c" }, strings1);
            string string1 = Files.CreateTempFile("TestCastString", "tmp").ToString();
            string string2 = Files.CreateTempFile("TestCastString", "tmp").ToString();

            string[] strings2 = MetaClass.Cast("['" + string1 + "','" + string2 + "']", typeof(string[]));
            Assert.AssertArrayEquals(new string[] { string1, string2 }, strings2);
            string[] strings3 = MetaClass.Cast("['a','b','c']", typeof(string[]));
            Assert.AssertArrayEquals(new string[] { "a", "b", "c" }, strings3);
        }
        /// <summary>Get the value of a property and automatically cast it to a specific type.</summary>
        /// <remarks>
        /// Get the value of a property and automatically cast it to a specific type.
        /// This differs from the original Properties.getProperty() method in that you
        /// need to specify the desired type (e.g. Double.class) and the default value
        /// is an object of that type, i.e. a double 0.0 instead of the String "0.0".
        /// </remarks>
        public static E Get <E>(Properties props, string key, E defaultValue, IType type)
        {
            string value = props.GetProperty(key);

            if (value == null)
            {
                return(defaultValue);
            }
            else
            {
                return((E)MetaClass.Cast(value, type));
            }
        }
 public virtual void TestCastSimple()
 {
     NUnit.Framework.Assert.AreEqual(1.0, MetaClass.Cast("1.0", typeof(double)));
     NUnit.Framework.Assert.AreEqual(1, MetaClass.Cast("1", typeof(int)));
     NUnit.Framework.Assert.AreEqual(1, MetaClass.Cast("1.0", typeof(int)));
     NUnit.Framework.Assert.AreEqual(1L, MetaClass.Cast("1.0", typeof(long)));
     NUnit.Framework.Assert.AreEqual((short)1, MetaClass.Cast("1.0", typeof(short)));
     NUnit.Framework.Assert.AreEqual(unchecked ((byte)1), MetaClass.Cast("1.0", typeof(byte)));
     NUnit.Framework.Assert.AreEqual("Hello", MetaClass.Cast("Hello", typeof(string)));
     NUnit.Framework.Assert.AreEqual(true, MetaClass.Cast("true", typeof(bool)));
     NUnit.Framework.Assert.AreEqual(true, MetaClass.Cast("1", typeof(bool)));
     NUnit.Framework.Assert.AreEqual(false, MetaClass.Cast("False", typeof(bool)));
     NUnit.Framework.Assert.AreEqual(new File("/path/to/file"), MetaClass.Cast("/path/to/file", typeof(File)));
 }
 public virtual void TestCastArray()
 {
     int[] ints1 = MetaClass.Cast("[1,2,3]", typeof(int[]));
     Assert.AssertArrayEquals(new int[] { 1, 2, 3 }, ints1);
     int[] ints2 = MetaClass.Cast("(1,2,3)", typeof(int[]));
     Assert.AssertArrayEquals(new int[] { 1, 2, 3 }, ints2);
     int[] ints3 = MetaClass.Cast("1, 2, 3", typeof(int[]));
     Assert.AssertArrayEquals(new int[] { 1, 2, 3 }, ints3);
     int[] ints4 = MetaClass.Cast("1 2 3", typeof(int[]));
     Assert.AssertArrayEquals(new int[] { 1, 2, 3 }, ints4);
     int[] ints5 = MetaClass.Cast("1   2   3", typeof(int[]));
     Assert.AssertArrayEquals(new int[] { 1, 2, 3 }, ints5);
     int[] ints6 = MetaClass.Cast("\n1 \n\n  2   3", typeof(int[]));
     Assert.AssertArrayEquals(new int[] { 1, 2, 3 }, ints6);
     int[] intsEmpty = MetaClass.Cast(string.Empty, typeof(int[]));
     Assert.AssertArrayEquals(new int[] {  }, intsEmpty);
 }
        public virtual void TestCastCollection()
        {
            ICollection <string> set = new HashSet <string>();

            set.Add("apple");
            set.Add("banana");
            ICollection <string> castedSet  = MetaClass.Cast("[apple, banana]", typeof(ISet));
            ICollection <string> castedSet2 = MetaClass.Cast("[apple ,    banana ]", typeof(ISet));
            ICollection <string> castedSet3 = MetaClass.Cast("{apple ,    banana }", typeof(ISet));

            NUnit.Framework.Assert.AreEqual(set, castedSet);
            NUnit.Framework.Assert.AreEqual(set, castedSet2);
            NUnit.Framework.Assert.AreEqual(set, castedSet3);
            IList <string> list = new LinkedList <string>();

            list.Add("apple");
            list.Add("banana");
            IList <string> castedList = MetaClass.Cast("[apple, banana]", typeof(IList));

            NUnit.Framework.Assert.AreEqual(list, castedList);
        }
        public virtual void TestCastMap()
        {
            IDictionary <string, string> a = MetaClass.Cast("{ a -> 1, b -> 2 }", typeof(IDictionary));

            NUnit.Framework.Assert.AreEqual(2, a.Count);
            NUnit.Framework.Assert.AreEqual("1", a["a"]);
            NUnit.Framework.Assert.AreEqual("2", a["b"]);
            IDictionary <string, string> b = MetaClass.Cast("a => 1, b -> 2", typeof(IDictionary));

            NUnit.Framework.Assert.AreEqual(2, b.Count);
            NUnit.Framework.Assert.AreEqual("1", b["a"]);
            NUnit.Framework.Assert.AreEqual("2", b["b"]);
            IDictionary <string, string> c = MetaClass.Cast("[a->1;b->2]", typeof(IDictionary));

            NUnit.Framework.Assert.AreEqual(2, c.Count);
            NUnit.Framework.Assert.AreEqual("1", c["a"]);
            NUnit.Framework.Assert.AreEqual("2", c["b"]);
            IDictionary <string, string> d = MetaClass.Cast("\n\na->\n1\n\n\nb->2", typeof(IDictionary));

            NUnit.Framework.Assert.AreEqual(2, d.Count);
            NUnit.Framework.Assert.AreEqual("1", d["a"]);
            NUnit.Framework.Assert.AreEqual("2", d["b"]);
        }
 /// <summary>Loads a comma-separated list of doubles from Properties.</summary>
 /// <remarks>Loads a comma-separated list of doubles from Properties.  The list cannot include any whitespace.</remarks>
 public static double[] GetDoubleArray(Properties props, string key)
 {
     double[] result = MetaClass.Cast(props.GetProperty(key), typeof(double[]));
     return(ArrayUtils.ToPrimitive(result));
 }
 /// <summary>Loads a comma-separated list of integers from Properties.</summary>
 /// <remarks>Loads a comma-separated list of integers from Properties.  The list cannot include any whitespace.</remarks>
 public static int[] GetIntArray(Properties props, string key)
 {
     int[] result = MetaClass.Cast(props.GetProperty(key), typeof(int[]));
     return(ArrayUtils.ToPrimitive(result));
 }
示例#14
0
 /*
  * ----------
  * OPTIONS
  * ----------
  */
 private static void FillField(object instance, FieldInfo f, string value)
 {
     //--Verbose
     if (verbose)
     {
         ArgumentParser.Option opt = f.GetAnnotation <ArgumentParser.Option>();
         StringBuilder         b   = new StringBuilder("setting ").Append(f.DeclaringType.FullName).Append('#').Append(f.Name).Append(' ');
         if (opt != null)
         {
             b.Append('[').Append(opt.Name()).Append("] ");
         }
         b.Append("to: ").Append(value);
         Redwood.Util.Log(b.ToString());
     }
     try
     {
         //--Permissions
         bool accessState = true;
         if (Modifier.IsFinal(f.GetModifiers()))
         {
             Redwood.Util.RuntimeException("Option cannot be final: " + f);
         }
         if (!f.IsAccessible())
         {
             accessState = false;
             f.SetAccessible(true);
         }
         //--Set Value
         object objVal = MetaClass.Cast(value, f.GetGenericType());
         if (objVal != null)
         {
             if (objVal.GetType().IsArray)
             {
                 //(case: array)
                 object[] array = (object[])objVal;
                 // error check
                 if (!f.GetType().IsArray)
                 {
                     Redwood.Util.RuntimeException("Setting an array to a non-array field. field: " + f + " value: " + Arrays.ToString(array) + " src: " + value);
                 }
                 // create specific array
                 object toSet = System.Array.CreateInstance(f.GetType().GetElementType(), array.Length);
                 for (int i = 0; i < array.Length; i++)
                 {
                     Sharpen.Runtime.SetArrayValue(toSet, i, array[i]);
                 }
                 // set value
                 f.SetValue(instance, toSet);
             }
             else
             {
                 //case: not array
                 f.SetValue(instance, objVal);
             }
         }
         else
         {
             Redwood.Util.RuntimeException("Cannot assign option field: " + f + " value: " + value + "; invalid type");
         }
         //--Permissions
         if (!accessState)
         {
             f.SetAccessible(false);
         }
     }
     catch (ArgumentException e)
     {
         Redwood.Util.Err(e);
         Redwood.Util.RuntimeException("Cannot assign option field: " + f.DeclaringType.GetCanonicalName() + '.' + f.Name + " value: " + value + " cause: " + e.Message);
     }
     catch (MemberAccessException e)
     {
         Redwood.Util.Err(e);
         Redwood.Util.RuntimeException("Cannot access option field: " + f.DeclaringType.GetCanonicalName() + '.' + f.Name);
     }
     catch (Exception e)
     {
         Redwood.Util.Err(e);
         Redwood.Util.RuntimeException("Cannot assign option field: " + f.DeclaringType.GetCanonicalName() + '.' + f.Name + " value: " + value + " cause: " + e.Message);
     }
 }
 public virtual void TestCastFromString()
 {
     NUnit.Framework.Assert.AreEqual(new MetaClassTest.FromStringable("foo"), MetaClass.Cast("foo", typeof(MetaClassTest.FromStringable)));
     NUnit.Framework.Assert.AreEqual(new MetaClassTest.FromStringable("bar"), MetaClass.Cast("bar", typeof(MetaClassTest.FromStringable)));
 }
 public virtual void TestCastRegression()
 {
     // Generics ordering (integer should go relatively early)
     MetaClassTest.Pointer <int> x1 = MetaClass.Cast("1", typeof(MetaClassTest.Pointer));
     NUnit.Framework.Assert.AreEqual(1, x1.value);
 }