public static void Main(string[] args)
        {
            Edu.Stanford.Nlp.Trees.CoordinationTransformer transformer = new Edu.Stanford.Nlp.Trees.CoordinationTransformer(null);
            Treebank   tb           = new MemoryTreebank();
            Properties props        = StringUtils.ArgsToProperties(args);
            string     treeFileName = props.GetProperty("treeFile");

            if (treeFileName != null)
            {
                try
                {
                    ITreeReader tr = new PennTreeReader(new BufferedReader(new InputStreamReader(new FileInputStream(treeFileName))), new LabeledScoredTreeFactory());
                    for (Tree t; (t = tr.ReadTree()) != null;)
                    {
                        tb.Add(t);
                    }
                }
                catch (IOException e)
                {
                    throw new Exception("File problem: " + e);
                }
            }
            foreach (Tree t_1 in tb)
            {
                System.Console.Out.WriteLine("Original tree");
                t_1.PennPrint();
                System.Console.Out.WriteLine();
                System.Console.Out.WriteLine("Tree transformed");
                Tree tree = transformer.TransformTree(t_1);
                tree.PennPrint();
                System.Console.Out.WriteLine();
                System.Console.Out.WriteLine("----------------------------");
            }
        }
Пример #2
0
        /// <summary>Loads treebank grammar from first argument and prints it.</summary>
        /// <remarks>
        /// Loads treebank grammar from first argument and prints it.
        /// Just a demonstration of functionality. <br />
        /// <code>usage: java MemoryTreebank treebankFilesPath</code>
        /// </remarks>
        /// <param name="args">array of command-line arguments</param>
        public static void Main(string[] args)
        {
            Timing.StartTime();
            Treebank treebank  = new DiskTreebank(null);
            Treebank treebank2 = new MemoryTreebank(null);

            treebank.LoadPath(args[0]);
            treebank2.LoadPath(args[0]);
            CompositeTreebank c = new CompositeTreebank(treebank, treebank2);

            Timing.EndTime();
            ITreeTransformer myTransformer  = new TransformingTreebank.MyTreeTransformer();
            ITreeTransformer myTransformer2 = new TransformingTreebank.MyTreeTransformer2();
            ITreeTransformer myTransformer3 = new TransformingTreebank.MyTreeTransformer3();
            Treebank         tf1            = c.Transform(myTransformer).Transform(myTransformer2).Transform(myTransformer3);
            Treebank         tf2            = new Edu.Stanford.Nlp.Trees.TransformingTreebank(new Edu.Stanford.Nlp.Trees.TransformingTreebank(new Edu.Stanford.Nlp.Trees.TransformingTreebank(c, myTransformer), myTransformer2), myTransformer3);

            ITreeTransformer[] tta = new ITreeTransformer[] { myTransformer, myTransformer2, myTransformer3 };
            ITreeTransformer   tt3 = new CompositeTreeTransformer(Arrays.AsList(tta));
            Treebank           tf3 = c.Transform(tt3);

            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("COMPOSITE (DISK THEN MEMORY REPEATED VERSION OF) INPUT TREEBANK");
            System.Console.Out.WriteLine(c);
            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("SLOWLY TRANSFORMED TREEBANK, USING TransformingTreebank() CONSTRUCTOR");
            Treebank tx1 = new Edu.Stanford.Nlp.Trees.TransformingTreebank(c, myTransformer);

            System.Console.Out.WriteLine(tx1);
            System.Console.Out.WriteLine("-----");
            Treebank tx2 = new Edu.Stanford.Nlp.Trees.TransformingTreebank(tx1, myTransformer2);

            System.Console.Out.WriteLine(tx2);
            System.Console.Out.WriteLine("-----");
            Treebank tx3 = new Edu.Stanford.Nlp.Trees.TransformingTreebank(tx2, myTransformer3);

            System.Console.Out.WriteLine(tx3);
            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("TRANSFORMED TREEBANK, USING Treebank.transform()");
            System.Console.Out.WriteLine(tf1);
            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("PRINTING AGAIN TRANSFORMED TREEBANK, USING Treebank.transform()");
            System.Console.Out.WriteLine(tf1);
            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("TRANSFORMED TREEBANK, USING TransformingTreebank() CONSTRUCTOR");
            System.Console.Out.WriteLine(tf2);
            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("TRANSFORMED TREEBANK, USING CompositeTreeTransformer");
            System.Console.Out.WriteLine(tf3);
            System.Console.Out.WriteLine("-------------------------");
            System.Console.Out.WriteLine("COMPOSITE (DISK THEN MEMORY REPEATED VERSION OF) INPUT TREEBANK");
            System.Console.Out.WriteLine(c);
            System.Console.Out.WriteLine("-------------------------");
        }
        /// <exception cref="System.IO.IOException"/>
        public static void Main(string[] args)
        {
            // Parse the arguments
            Properties props = StringUtils.ArgsToProperties(args);

            ArgumentParser.FillOptions(new Type[] { typeof(ArgumentParser), typeof(SplitTrainingSet) }, props);
            if (SplitNames.Length != SplitWeights.Length)
            {
                throw new ArgumentException("Name and weight arrays must be of the same length");
            }
            double totalWeight = 0.0;

            foreach (double weight in SplitWeights)
            {
                totalWeight += weight;
                if (weight < 0.0)
                {
                    throw new ArgumentException("Split weights cannot be negative");
                }
            }
            if (totalWeight <= 0.0)
            {
                throw new ArgumentException("Split weights must total to a positive weight");
            }
            IList <double> splitWeights = new List <double>();

            foreach (double weight_1 in SplitWeights)
            {
                splitWeights.Add(weight_1 / totalWeight);
            }
            logger.Info("Splitting into " + splitWeights.Count + " lists with weights " + splitWeights);
            if (Seed == 0L)
            {
                Seed = Runtime.NanoTime();
                logger.Info("Random seed not set by options, using " + Seed);
            }
            Random random = new Random(Seed);
            IList <IList <Tree> > splits = new List <IList <Tree> >();

            foreach (double d in splitWeights)
            {
                splits.Add(new List <Tree>());
            }
            Treebank treebank = new MemoryTreebank(null);

            treebank.LoadPath(Input);
            logger.Info("Splitting " + treebank.Count + " trees");
            foreach (Tree tree in treebank)
            {
                int index = WeightedIndex(splitWeights, random);
                splits[index].Add(tree);
            }
            for (int i = 0; i < splits.Count; ++i)
            {
                string       filename = Output + "." + SplitNames[i];
                IList <Tree> split    = splits[i];
                logger.Info("Writing " + split.Count + " trees to " + filename);
                FileWriter     fout = new FileWriter(filename);
                BufferedWriter bout = new BufferedWriter(fout);
                foreach (Tree tree_1 in split)
                {
                    bout.Write(tree_1.ToString());
                    bout.NewLine();
                }
                bout.Close();
                fout.Close();
            }
        }