Пример #1
0
        private static Atom Open(Atom args, Context context)
        {
            Atom path = args?.atom;

            if (path == null || !path.IsString())
            {
                throw new ArgumentException("Path must be string!");
            }
            string file = (string)path.value;

            FileAccess access = ArgUtils.GetEnum <FileAccess>(args?.next?.atom, 1, FileAccess.Read);
            FileMode   mode   = ArgUtils.GetEnum <FileMode>(args?.next?.next?.atom, 2, FileMode.Open);

            if (access == FileAccess.Read)
            {
                StreamReader reader = new StreamReader(File.Open(file, mode, access));

                return(new Atom(AtomType.Native, reader));
            }
            else if (access == FileAccess.Write)
            {
                StreamWriter writer = new StreamWriter(File.Open(file, mode, access));

                return(new Atom(AtomType.Native, writer));
            }

            return(null);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EntLibLoggerFactoryAdapter"/> class.
 /// </summary>
 /// <remarks>passed in values are not used, configuration is external to EntLib logging API</remarks>
 /// <param name="properties">The properties.</param>
 public EntLibLoggerFactoryAdapter(NameValueCollection properties)
     : this(ArgUtils.TryParse(EntLibLoggerSettings.DEFAULTPRIORITY, ArgUtils.GetValue(properties, "priority"))
            , ArgUtils.Coalesce(ArgUtils.GetValue(properties, "exceptionFormat"), EntLibLoggerSettings.DEFAULTEXCEPTIONFORMAT)
            , null
            )
 {
 }
Пример #3
0
 public UploadDetails(UploadId id, IEnumerable <FileDetails> files, UploadDescription description, IReadOnlyList <string> rejectedDuplicates)
 {
     Id                 = Guard.NotNull(id, nameof(id));
     Files              = ArgUtils.ToRoList(files, nameof(files));
     Description        = Guard.NotNull(description, nameof(description));
     RejectedDuplicates = Guard.NotNull(rejectedDuplicates, nameof(rejectedDuplicates));
 }
Пример #4
0
        /// <summary>
        /// Builds a <see cref="ILoggerFactoryAdapter"/> instance from the given <see cref="LogSetting"/>
        /// using <see cref="Activator"/>.
        /// </summary>
        /// <param name="setting"></param>
        /// <returns>the <see cref="ILoggerFactoryAdapter"/> instance. Is never <c>null</c></returns>
        private static ILoggerFactoryAdapter BuildLoggerFactoryAdapterFromLogSettings(LogSetting setting)
        {
            ArgUtils.AssertNotNull("setting", setting);
            // already ensured by LogSetting
            //            AssertArgIsAssignable<ILoggerFactoryAdapter>("setting.FactoryAdapterType", setting.FactoryAdapterType
            //                                , "Specified FactoryAdapter does not implement {0}.  Check implementation of class {1}"
            //                                , typeof(ILoggerFactoryAdapter).FullName
            //                                , setting.FactoryAdapterType.AssemblyQualifiedName);

            ILoggerFactoryAdapter adapter = null;

            ArgUtils.Guard(delegate
            {
                if (setting.Properties != null &&
                    setting.Properties.Count > 0)
                {
                    object[] args = { setting.Properties };

                    adapter = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType, args);
                }
                else
                {
                    adapter = (ILoggerFactoryAdapter)Activator.CreateInstance(setting.FactoryAdapterType);
                }
            }
                           , "Unable to create instance of type {0}. Possible explanation is lack of zero arg and single arg NameValueCollection constructors"
                           , setting.FactoryAdapterType.FullName
                           );

            // make sure
            ArgUtils.AssertNotNull("adapter", adapter, "Activator.CreateInstance() returned <null>");
            return(adapter);
        }
Пример #5
0
 public UnityDebugLoggerFactoryAdapter(NameValueCollection properties)
 {
     _level            = ArgUtils.TryParseEnum(LogLevel.All, ArgUtils.GetValue(properties, "level"));
     _showLogName      = ArgUtils.TryParse(true, ArgUtils.GetValue(properties, "showLogName"));
     _showLogLevel     = ArgUtils.TryParse(true, ArgUtils.GetValue(properties, "showLogLevel"));
     _useUnityLogLevel = ArgUtils.TryParse(true, ArgUtils.GetValue(properties, "useUnityLogLevel"));
 }
Пример #6
0
        public SetTagsForFileCommand(FileId fileId, IEnumerable <TagName> tags)
        {
            FileId = Guard.NotNull(fileId, nameof(fileId));
            Tags   = ArgUtils.ToRoList(tags, nameof(tags));

            // TODO: throw ex if duplicated tags
        }
Пример #7
0
        /// <summary>
        /// Builds the logger factory adapter.
        /// </summary>
        /// <returns>a factory adapter instance. Is never <c>null</c>.</returns>
        private static ILoggerFactoryAdapter BuildLoggerFactoryAdapter()
        {
#if UNITY3D
            ILoggerFactoryAdapter defaultFactory = new UnityDebugLoggerFactoryAdapter();
            return(defaultFactory);
#else
            object sectionResult = null;

            ArgUtils.Guard(delegate
            {
                sectionResult = ConfigurationReader.GetSection(COMMON_LOGGING_SECTION);
            }
                           , "Failed obtaining configuration for Common.Logging from configuration section 'common/logging'.");

            // configuration reader returned <null>
            if (sectionResult == null)
            {
                string message = (ConfigurationReader.GetType() == typeof(DefaultConfigurationReader))
                                     ? string.Format("no configuration section <{0}> found - suppressing logging output", COMMON_LOGGING_SECTION)
                                     : string.Format("Custom ConfigurationReader '{0}' returned <null> - suppressing logging output", ConfigurationReader.GetType().FullName);
#if PORTABLE
                Debug.WriteLine(message);
#else
                Trace.WriteLine(message);
#endif

                ILoggerFactoryAdapter defaultFactory = new NoOpLoggerFactoryAdapter();
                return(defaultFactory);
            }

            // ready to use ILoggerFactoryAdapter?
            if (sectionResult is ILoggerFactoryAdapter)
            {
#if PORTABLE
                Debug.WriteLine(string.Format("Using ILoggerFactoryAdapter returned from custom ConfigurationReader '{0}'", ConfigurationReader.GetType().FullName));
#else
                Trace.WriteLine(string.Format("Using ILoggerFactoryAdapter returned from custom ConfigurationReader '{0}'", ConfigurationReader.GetType().FullName));
#endif
                return((ILoggerFactoryAdapter)sectionResult);
            }

            // ensure what's left is a LogSetting instance
            ArgUtils.Guard(delegate
            {
                ArgUtils.AssertIsAssignable <LogSetting>("sectionResult", sectionResult.GetType());
            }
                           , "ConfigurationReader {0} returned unknown settings instance of type {1}"
                           , ConfigurationReader.GetType().FullName, sectionResult.GetType().FullName);

            ILoggerFactoryAdapter adapter = null;
            ArgUtils.Guard(delegate
            {
                adapter = BuildLoggerFactoryAdapterFromLogSettings((LogSetting)sectionResult);
            }
                           , "Failed creating LoggerFactoryAdapter from settings");

            return(adapter);
#endif
        }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AbstractSimpleLoggerFactoryAdapter"/> class.
 /// </summary>
 /// <remarks>
 /// Looks for level, showDateTime, showLogName, dateTimeFormat items from
 /// <paramref name="properties" /> for use when the GetLogger methods are called.
 /// <see cref="ConfigurationSectionHandler"/> for more information on how to use the
 /// standard .NET application configuraiton file (App.config/Web.config)
 /// to configure this adapter.
 /// </remarks>
 /// <param name="properties">The name value collection, typically specified by the user in
 /// a configuration section named common/logging.</param>
 protected AbstractSimpleLoggerFactoryAdapter(NameValueCollection properties)
     : base(true)
 {
     _level          = ArgUtils.TryParseEnum(LogLevel.All, ArgUtils.GetValue(properties, "level"));
     _showDateTime   = ArgUtils.TryParse(true, ArgUtils.GetValue(properties, "showDateTime"));
     _showLogName    = ArgUtils.TryParse(true, ArgUtils.GetValue(properties, "showLogName"));
     _showLevel      = ArgUtils.TryParse(true, ArgUtils.GetValue(properties, "showLevel"));
     _dateTimeFormat = ArgUtils.GetValue(properties, "dateTimeFormat", string.Empty);
 }
Пример #9
0
    static void Main(string[] args)
    {
        // Intercept termination of the console app, to flush and close the output file stream
        // (apparently the 'finally' block below is not executed if the app is terminated with Ctrl-C).
        Console.CancelKeyPress += delegate
        {
            if (__streamWriter is not null)
            {
                __streamWriter.Close();
            }
        };

        // Read command line arguments.
        StopCondition?stopCond = ArgUtils.ReadArgs(args, out string?experimentId, out string?filename);

        if (stopCond is null || experimentId is null || filename is null)
        {
            return;
        }

        // Initialise log4net (log to console).
        var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

        XmlConfigurator.Configure(logRepository, new FileInfo("log4net.properties"));

        // Create and configure a NEAT experiment instance.
        INeatExperiment <double>?experiment = InitExperiment(experimentId);

        if (experiment is null)
        {
            return;
        }

        // Create an evolution algorithm host.
        IEvolutionAlgorithmHost eaHost = CreateEvolutionAlgorithmHost(experiment, stopCond);

        // Open and initialise the output file.
        __streamWriter = InitOutputFile(filename);
        try
        {
            // Run the main efficacy sampling loop until the process is terminated.
            for (;;)
            {
                Sample s = eaHost.Sample();
                __streamWriter.WriteLine($"{s.ElapsedTimeSecs},{s.GenerationCount},{s.BestFitness:0.#####},{s.MeanFitness:0.#####},{s.MaxComplexity:0.#####},{s.MeanComplexity:0.#####},{s.EvaluationCount}");
                __streamWriter.Flush();
            }
        }
        finally
        {
            if (__streamWriter is not null)
            {
                __streamWriter.Close();
            }
        }
    }
        /// <summary>
        /// Constructor accepting configuration properties and an arbitrary
        /// <see cref="ILog4NetRuntime"/> instance.
        /// </summary>
        /// <param name="properties">configuration properties, see <see cref="Log4NetLoggerFactoryAdapter"/> for more.</param>
        /// <param name="runtime">a log4net runtime adapter</param>
        protected Log4NetLoggerFactoryAdapter(NameValueCollection properties, ILog4NetRuntime runtime)
            : base(true)
        {
            if (runtime == null)
            {
                throw new ArgumentNullException("runtime");
            }
            _runtime = runtime;

            // parse config properties
            string configType = ArgUtils.GetValue(properties, "configType", string.Empty).ToUpper();
            string configFile = ArgUtils.GetValue(properties, "configFile", string.Empty);

            // app-relative path?
            if (configFile.StartsWith("~/") || configFile.StartsWith("~\\"))
            {
                configFile = string.Format("{0}/{1}", AppDomain.CurrentDomain.BaseDirectory.TrimEnd('/', '\\'), configFile.Substring(2));
            }

            if (configType == "FILE" || configType == "FILE-WATCH")
            {
                if (configFile == string.Empty)
                {
                    throw new ConfigurationException("Configuration property 'configFile' must be set for log4Net configuration of type 'FILE' or 'FILE-WATCH'.");
                }

                if (!File.Exists(configFile))
                {
                    throw new ConfigurationException("log4net configuration file '" + configFile + "' does not exists");
                }
            }

            switch (configType)
            {
            case "INLINE":
                _runtime.XmlConfiguratorConfigure();
                break;

            case "FILE":
                _runtime.XmlConfiguratorConfigure(configFile);
                break;

            case "FILE-WATCH":
                _runtime.XmlConfiguratorConfigureAndWatch(configFile);
                break;

            case "EXTERNAL":
                // Log4net will be configured outside of Common.Logging
                break;

            default:
                _runtime.BasicConfiguratorConfigure();
                break;
            }
        }
Пример #11
0
        public static JsonDotNetJsonObject Parse(string json)
        {
            ArgUtils.CheckForNull(json, nameof(json));

            if (string.IsNullOrEmpty(json))
            {
                throw new ArgumentException($"Invalid json string: {json}", nameof(json));
            }

            JToken token = JToken.Parse(json);

            return(ParseToken(token));
        }
Пример #12
0
        internal Verb(string?shortName, string longName, string?parentShortAndLongName, CliParserConfig config)
        {
            ShortName            = shortName;
            LongName             = longName;
            ShortAndLongName     = ArgUtils.ShortAndLongName(shortName, longName);
            FullShortAndLongName = parentShortAndLongName == null?ArgUtils.ShortAndLongName(shortName, longName) : string.Concat(parentShortAndLongName, ' ', ArgUtils.ShortAndLongName(shortName, longName));

            Invoke      = () => throw new CliParserBuilderException(string.Concat("Invoke for verb ", FullShortAndLongName, " has not been configured"));
            InvokeAsync = () => throw new CliParserBuilderException(string.Concat("InvokeAsync for verb ", FullShortAndLongName, " has not been configured"));
            this.config = config;
            HelpText    = "No help available.";
            allVerbs    = new();
            verbsByName = new(config.StringComparer);
        }
Пример #13
0
        private static Atom ReadDirectory(Atom args, Context context)
        {
            Atom path = args?.atom;

            if (path == null || !path.IsString())
            {
                throw new ArgumentException("Path must be string!");
            }
            string       directory = (string)path.value;
            Atom         pattern   = args?.next?.atom;
            Atom         mode      = args?.next?.next?.atom;
            SearchOption option    = ArgUtils.GetEnum <SearchOption>(
                mode, 3);

            string[] dirs = null;
            if (pattern == null)
            {
                dirs = Directory.GetDirectories(directory);
            }
            else
            {
                dirs = Directory.GetDirectories(directory, (string)pattern.value, option);
            }

            string[] files = null;
            if (pattern == null)
            {
                files = Directory.GetFiles(directory);
            }
            else
            {
                files = Directory.GetFiles(directory, (string)pattern.value, option);
            }

            Atom[] elements = new Atom[dirs.Length + files.Length];
            for (int i = 0; i < dirs.Length; i++)
            {
                elements[i] = new Atom(AtomType.String, dirs[i]);
            }
            for (int i = 0; i < files.Length; i++)
            {
                elements[i + dirs.Length] = new Atom(AtomType.String, files[i]);
            }

            return(Atom.List(elements));
        }
Пример #14
0
        public Log4NetFactoryAdapter(NameValueCollection properties)
        {
            string configType = ArgUtils.GetValue(properties, "configType", string.Empty).ToUpper();
            string configFile = ArgUtils.GetValue(properties, "configFile", string.Empty);

            // app-relative path?
            if (configFile.StartsWith("~/") || configFile.StartsWith("~\\"))
            {
                configFile = string.Format("{0}/{1}", AppDomain.CurrentDomain.BaseDirectory.TrimEnd('/', '\\'), configFile.Substring(2));
            }

            if (configType == "FILE" || configType == "FILE-WATCH")
            {
                if (configFile == string.Empty)
                {
                    throw new ConfigurationException("Configuration property 'configFile' must be set for log4Net configuration of type 'FILE' or 'FILE-WATCH'.");
                }

                if (!File.Exists(configFile))
                {
                    throw new ConfigurationException("log4net configuration file '" + configFile + "' does not exists");
                }
            }

            switch (configType)
            {
            case "INLINE":
                log4net.Config.XmlConfigurator.Configure();
                break;

            case "FILE":
                log4net.Config.XmlConfigurator.Configure(new FileInfo(configFile));
                break;

            case "FILE-WATCH":
                log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFile));
                break;

            case "EXTERNAL":
            // Log4net will be configured outside of Common.Logging
            default:
                //log4net.Config.XmlConfigurator.Configure();
                break;
            }
        }
Пример #15
0
        public UserRoles(IEnumerable <UserRole> roles)
        {
            Items = ArgUtils.ToRoList(roles, nameof(roles))
                    .OrderBy(x => x.Value)
                    .ToList();

            int countDistinct = Items.Distinct().Count();

            if (countDistinct < Items.Count)
            {
                throw new ArgumentException("Duplicated roles.", nameof(roles));
            }

            if (Items.Count > 50)
            {
                throw new ArgumentException("Too many roles. Max is 50.", nameof(roles));
            }
        }
Пример #16
0
        internal Verb(string?shortName, string longName, string?parentShortAndLongName, CliParserConfig config)
        {
            ShortName            = shortName;
            LongName             = longName;
            ShortAndLongName     = ArgUtils.ShortAndLongName(shortName, longName);
            FullShortAndLongName = parentShortAndLongName == null?ArgUtils.ShortAndLongName(shortName, longName) : string.Concat(parentShortAndLongName, ' ', ArgUtils.ShortAndLongName(shortName, longName));

            Invoke            = x => throw new CliParserBuilderException(string.Concat("Invoke for verb ", FullShortAndLongName, " has not been configured"));
            InvokeAsync       = x => throw new CliParserBuilderException(string.Concat("InvokeAsync for verb ", FullShortAndLongName, " has not been configured"));
            this.config       = config;
            verbsByName       = new(config.StringComparer);
            allVerbs          = new();
            allValues         = new List <IValue <TClass> >();
            allSwitches       = new List <ISwitch <TClass> >();
            allOptions        = new List <IOption <TClass> >();
            allSwitchesByName = new Dictionary <string, ISwitch <TClass> >(config.StringComparer);
            allOptionsByName  = new Dictionary <string, IOption <TClass> >(config.StringComparer);
            HelpText          = "No help available.";
        }
        /// <summary>
        /// Writes the overall help.
        /// Lists all verbs, their aliases, and their help text.
        /// </summary>
        /// <param name="console">The console help is written to.</param>
        public void WriteOverallHelp(IConsole console, IReadOnlyCollection <IVerb> verbs, CliParserConfig config)
        {
            ConsoleColor original = console.ForegroundColor;
            int          width    = console.CurrentWidth;

            console.WriteLine("Verbs...");

            // This is the number of characters we're reserving for the key text
            // A few more spaces just so it's easier to read

            WritePaddedKeywordDescriptions(console,
                                           KeywordColor,
                                           verbs.Select(verb => new KeywordAndDescription(verb.ShortAndLongName, verb.HelpText)));

            console.Write("For detailed help, use: ");
            console.ForegroundColor = KeywordColor;
            console.WriteLine($"verbname " + ArgUtils.ShortAndLongName(config.ShortHelpSwitch, config.LongHelpSwitch));
            console.WriteLine();
            console.ForegroundColor = original;
        }
        private void ConfigureEventSource(NameValueCollection properties)
        {
            //assign an instance of a custom ICommonLoggingEventSource if specified, else use default type
            var commonLoggingEventSourceTypeDescriptor = ArgUtils.GetValue(properties, "commonLoggingEventSourceType",
                                                                           string.Empty);

            if (!string.IsNullOrEmpty(commonLoggingEventSourceTypeDescriptor))
            {
                var eventSourceType = Type.GetType(commonLoggingEventSourceTypeDescriptor);

                if (null == eventSourceType)
                {
                    throw new ConfigurationException(
                              string.Format(
                                  "Error in 'commonLoggingEventSourceType' arg.  Unable to determine TYPE information from value {0}",
                                  commonLoggingEventSourceTypeDescriptor));
                }

                try
                {
                    var candidate = Activator.CreateInstance(eventSourceType) as ICommonLoggingEventSource;
                    RecordEventSource(candidate);
                }
                catch (Exception exception) //no matter the underlying exception type we want to report it as a Config Exception
                {
                    throw new ConfigurationException("Error in 'commonLoggingEventSourceType' arg.", exception);
                }
            }
            else
            {
                var defaultEventSourceType = typeof(CommonLoggingEventSource);

                if (!EventSourceRegistry.ContainsKey(defaultEventSourceType))
                {
                    EventSource = new CommonLoggingEventSource();
                }

                _eventSourceType = defaultEventSourceType;
            }
        }
        private void ConfigureLogLevel(NameValueCollection properties)
        {
            //set the logging level; default to ALL
            var levelSetting = ArgUtils.TryParseEnum(LogLevel.All, ArgUtils.GetValue(properties, "level"));

            switch (levelSetting)
            {
            case LogLevel.Trace:
            case LogLevel.All:
                LogLevel = LogLevel.Trace | LogLevel.Debug | LogLevel.Info | LogLevel.Warn | LogLevel.Error | LogLevel.Fatal;
                break;

            case LogLevel.Debug:
                LogLevel = LogLevel.Debug | LogLevel.Info | LogLevel.Warn | LogLevel.Error | LogLevel.Fatal;
                break;

            case LogLevel.Info:
                LogLevel = LogLevel.Info | LogLevel.Warn | LogLevel.Error | LogLevel.Fatal;
                break;

            case LogLevel.Warn:
                LogLevel = LogLevel.Warn | LogLevel.Error | LogLevel.Fatal;
                break;

            case LogLevel.Error:
                LogLevel = LogLevel.Error | LogLevel.Fatal;
                break;

            case LogLevel.Fatal:
                LogLevel = LogLevel.Fatal;
                break;

            case LogLevel.Off:
            default:
                break;
            }
        }
        /// <exception cref="System.IO.IOException"/>
        public static void Main(string[] args)
        {
            string         modelPath          = null;
            string         outputPath         = null;
            string         inputPath          = null;
            string         testTreebankPath   = null;
            IFileFilter    testTreebankFilter = null;
            IList <string> unusedArgs         = Generics.NewArrayList();

            for (int argIndex = 0; argIndex < args.Length;)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-model"))
                {
                    modelPath = args[argIndex + 1];
                    argIndex += 2;
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-output"))
                    {
                        outputPath = args[argIndex + 1];
                        argIndex  += 2;
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-input"))
                        {
                            inputPath = args[argIndex + 1];
                            argIndex += 2;
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-testTreebank"))
                            {
                                Pair <string, IFileFilter> treebankDescription = ArgUtils.GetTreebankDescription(args, argIndex, "-testTreebank");
                                argIndex           = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                                testTreebankPath   = treebankDescription.First();
                                testTreebankFilter = treebankDescription.Second();
                            }
                            else
                            {
                                unusedArgs.Add(args[argIndex++]);
                            }
                        }
                    }
                }
            }
            string[]          newArgs = Sharpen.Collections.ToArray(unusedArgs, new string[unusedArgs.Count]);
            LexicalizedParser parser  = ((LexicalizedParser)LexicalizedParser.LoadModel(modelPath, newArgs));
            DVModel           model   = DVParser.GetModelFromLexicalizedParser(parser);
            File outputFile           = new File(outputPath);

            FileSystem.CheckNotExistsOrFail(outputFile);
            FileSystem.MkdirOrFail(outputFile);
            int count = 0;

            if (inputPath != null)
            {
                Reader input = new BufferedReader(new FileReader(inputPath));
                DocumentPreprocessor processor = new DocumentPreprocessor(input);
                foreach (IList <IHasWord> sentence in processor)
                {
                    count++;
                    // index from 1
                    IParserQuery pq = parser.ParserQuery();
                    if (!(pq is RerankingParserQuery))
                    {
                        throw new ArgumentException("Expected a RerankingParserQuery");
                    }
                    RerankingParserQuery rpq = (RerankingParserQuery)pq;
                    if (!rpq.Parse(sentence))
                    {
                        throw new Exception("Unparsable sentence: " + sentence);
                    }
                    IRerankerQuery reranker = rpq.RerankerQuery();
                    if (!(reranker is DVModelReranker.Query))
                    {
                        throw new ArgumentException("Expected a DVModelReranker");
                    }
                    DeepTree deepTree = ((DVModelReranker.Query)reranker).GetDeepTrees()[0];
                    IdentityHashMap <Tree, SimpleMatrix> vectors = deepTree.GetVectors();
                    foreach (KeyValuePair <Tree, SimpleMatrix> entry in vectors)
                    {
                        log.Info(entry.Key + "   " + entry.Value);
                    }
                    FileWriter     fout = new FileWriter(outputPath + File.separator + "sentence" + count + ".txt");
                    BufferedWriter bout = new BufferedWriter(fout);
                    bout.Write(SentenceUtils.ListToString(sentence));
                    bout.NewLine();
                    bout.Write(deepTree.GetTree().ToString());
                    bout.NewLine();
                    foreach (IHasWord word in sentence)
                    {
                        OutputMatrix(bout, model.GetWordVector(word.Word()));
                    }
                    Tree rootTree = FindRootTree(vectors);
                    OutputTreeMatrices(bout, rootTree, vectors);
                    bout.Flush();
                    fout.Close();
                }
            }
        }
Пример #21
0
        public FileMetadataGroup(string name, IEnumerable <FileMetadataEntry> entries)
        {
            Name = Guard.NotNull(name, nameof(name));

            Entries = ArgUtils.ToRoList(entries, nameof(entries));
        }
        /// <summary>
        /// An example command line for training a new parser:
        /// <br />
        /// nohup java -mx6g edu.stanford.nlp.parser.dvparser.DVParser -cachedTrees /scr/nlp/data/dvparser/wsj/cached.wsj.train.simple.ser.gz -train -testTreebank  /afs/ir/data/linguistic-data/Treebank/3/parsed/mrg/wsj/22 2200-2219 -debugOutputFrequency 400 -nofilter -trainingThreads 5 -parser /u/nlp/data/lexparser/wsjPCFG.nocompact.simple.ser.gz -trainingIterations 40 -batchSize 25 -model /scr/nlp/data/dvparser/wsj/wsj.combine.v2.ser.gz -unkWord "*UNK*" -dvCombineCategories &gt; /scr/nlp/data/dvparser/wsj/wsj.combine.v2.out 2&gt;&amp;1 &amp;
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.TypeLoadException"/>
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Help();
                System.Environment.Exit(2);
            }
            log.Info("Running DVParser with arguments:");
            foreach (string arg in args)
            {
                log.Info("  " + arg);
            }
            log.Info();
            string         parserPath           = null;
            string         trainTreebankPath    = null;
            IFileFilter    trainTreebankFilter  = null;
            string         cachedTrainTreesPath = null;
            bool           runGradientCheck     = false;
            bool           runTraining          = false;
            string         testTreebankPath     = null;
            IFileFilter    testTreebankFilter   = null;
            string         initialModelPath     = null;
            string         modelPath            = null;
            bool           filter            = true;
            string         resultsRecordPath = null;
            IList <string> unusedArgs        = new List <string>();
            // These parameters can be null or 0 if the model was not
            // serialized with the new parameters.  Setting the options at the
            // command line will override these defaults.
            // TODO: if/when we integrate back into the main branch and
            // rebuild models, we can get rid of this
            IList <string> argsWithDefaults = new List <string>(Arrays.AsList(new string[] { "-wordVectorFile", Options.LexOptions.DefaultWordVectorFile, "-dvKBest", int.ToString(TrainOptions.DefaultKBest), "-batchSize", int.ToString(TrainOptions.DefaultBatchSize
                                                                                                                                                                                                                                          ), "-trainingIterations", int.ToString(TrainOptions.DefaultTrainingIterations), "-qnIterationsPerBatch", int.ToString(TrainOptions.DefaultQnIterationsPerBatch), "-regCost", double.ToString(TrainOptions.DefaultRegcost), "-learningRate", double
                                                                                             .ToString(TrainOptions.DefaultLearningRate), "-deltaMargin", double.ToString(TrainOptions.DefaultDeltaMargin), "-unknownNumberVector", "-unknownDashedWordVectors", "-unknownCapsVector", "-unknownchinesepercentvector", "-unknownchinesenumbervector"
                                                                                             , "-unknownchineseyearvector", "-unkWord", "*UNK*", "-transformMatrixType", "DIAGONAL", "-scalingForInit", double.ToString(TrainOptions.DefaultScalingForInit), "-trainWordVectors" }));

            Sharpen.Collections.AddAll(argsWithDefaults, Arrays.AsList(args));
            args = Sharpen.Collections.ToArray(argsWithDefaults, new string[argsWithDefaults.Count]);
            for (int argIndex = 0; argIndex < args.Length;)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-parser"))
                {
                    parserPath = args[argIndex + 1];
                    argIndex  += 2;
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-testTreebank"))
                    {
                        Pair <string, IFileFilter> treebankDescription = ArgUtils.GetTreebankDescription(args, argIndex, "-testTreebank");
                        argIndex           = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                        testTreebankPath   = treebankDescription.First();
                        testTreebankFilter = treebankDescription.Second();
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-treebank"))
                        {
                            Pair <string, IFileFilter> treebankDescription = ArgUtils.GetTreebankDescription(args, argIndex, "-treebank");
                            argIndex            = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                            trainTreebankPath   = treebankDescription.First();
                            trainTreebankFilter = treebankDescription.Second();
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-cachedTrees"))
                            {
                                cachedTrainTreesPath = args[argIndex + 1];
                                argIndex            += 2;
                            }
                            else
                            {
                                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-runGradientCheck"))
                                {
                                    runGradientCheck = true;
                                    argIndex++;
                                }
                                else
                                {
                                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-train"))
                                    {
                                        runTraining = true;
                                        argIndex++;
                                    }
                                    else
                                    {
                                        if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-model"))
                                        {
                                            modelPath = args[argIndex + 1];
                                            argIndex += 2;
                                        }
                                        else
                                        {
                                            if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-nofilter"))
                                            {
                                                filter = false;
                                                argIndex++;
                                            }
                                            else
                                            {
                                                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-continueTraining"))
                                                {
                                                    runTraining      = true;
                                                    filter           = false;
                                                    initialModelPath = args[argIndex + 1];
                                                    argIndex        += 2;
                                                }
                                                else
                                                {
                                                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-resultsRecord"))
                                                    {
                                                        resultsRecordPath = args[argIndex + 1];
                                                        argIndex         += 2;
                                                    }
                                                    else
                                                    {
                                                        unusedArgs.Add(args[argIndex++]);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (parserPath == null && modelPath == null)
            {
                throw new ArgumentException("Must supply either a base parser model with -parser or a serialized DVParser with -model");
            }
            if (!runTraining && modelPath == null && !runGradientCheck)
            {
                throw new ArgumentException("Need to either train a new model, run the gradient check or specify a model to load with -model");
            }
            string[] newArgs = Sharpen.Collections.ToArray(unusedArgs, new string[unusedArgs.Count]);
            Edu.Stanford.Nlp.Parser.Dvparser.DVParser dvparser = null;
            LexicalizedParser lexparser = null;

            if (initialModelPath != null)
            {
                lexparser = ((LexicalizedParser)LexicalizedParser.LoadModel(initialModelPath, newArgs));
                DVModel model = GetModelFromLexicalizedParser(lexparser);
                dvparser = new Edu.Stanford.Nlp.Parser.Dvparser.DVParser(model, lexparser);
            }
            else
            {
                if (runTraining || runGradientCheck)
                {
                    lexparser = ((LexicalizedParser)LexicalizedParser.LoadModel(parserPath, newArgs));
                    dvparser  = new Edu.Stanford.Nlp.Parser.Dvparser.DVParser(lexparser);
                }
                else
                {
                    if (modelPath != null)
                    {
                        lexparser = ((LexicalizedParser)LexicalizedParser.LoadModel(modelPath, newArgs));
                        DVModel model = GetModelFromLexicalizedParser(lexparser);
                        dvparser = new Edu.Stanford.Nlp.Parser.Dvparser.DVParser(model, lexparser);
                    }
                }
            }
            IList <Tree> trainSentences = new List <Tree>();
            IdentityHashMap <Tree, byte[]> trainCompressedParses = Generics.NewIdentityHashMap();

            if (cachedTrainTreesPath != null)
            {
                foreach (string path in cachedTrainTreesPath.Split(","))
                {
                    IList <Pair <Tree, byte[]> > cache = IOUtils.ReadObjectFromFile(path);
                    foreach (Pair <Tree, byte[]> pair in cache)
                    {
                        trainSentences.Add(pair.First());
                        trainCompressedParses[pair.First()] = pair.Second();
                    }
                    log.Info("Read in " + cache.Count + " trees from " + path);
                }
            }
            if (trainTreebankPath != null)
            {
                // TODO: make the transformer a member of the model?
                ITreeTransformer transformer = BuildTrainTransformer(dvparser.GetOp());
                Treebank         treebank    = dvparser.GetOp().tlpParams.MemoryTreebank();
                treebank.LoadPath(trainTreebankPath, trainTreebankFilter);
                treebank = treebank.Transform(transformer);
                log.Info("Read in " + treebank.Count + " trees from " + trainTreebankPath);
                CacheParseHypotheses cacher = new CacheParseHypotheses(dvparser.parser);
                CacheParseHypotheses.CacheProcessor processor = new CacheParseHypotheses.CacheProcessor(cacher, lexparser, dvparser.op.trainOptions.dvKBest, transformer);
                foreach (Tree tree in treebank)
                {
                    trainSentences.Add(tree);
                    trainCompressedParses[tree] = processor.Process(tree).second;
                }
                //System.out.println(tree);
                log.Info("Finished parsing " + treebank.Count + " trees, getting " + dvparser.op.trainOptions.dvKBest + " hypotheses each");
            }
            if ((runTraining || runGradientCheck) && filter)
            {
                log.Info("Filtering rules for the given training set");
                dvparser.dvModel.SetRulesForTrainingSet(trainSentences, trainCompressedParses);
                log.Info("Done filtering rules; " + dvparser.dvModel.numBinaryMatrices + " binary matrices, " + dvparser.dvModel.numUnaryMatrices + " unary matrices, " + dvparser.dvModel.wordVectors.Count + " word vectors");
            }
            //dvparser.dvModel.printAllMatrices();
            Treebank testTreebank = null;

            if (testTreebankPath != null)
            {
                log.Info("Reading in trees from " + testTreebankPath);
                if (testTreebankFilter != null)
                {
                    log.Info("Filtering on " + testTreebankFilter);
                }
                testTreebank = dvparser.GetOp().tlpParams.MemoryTreebank();
                testTreebank.LoadPath(testTreebankPath, testTreebankFilter);
                log.Info("Read in " + testTreebank.Count + " trees for testing");
            }
            //    runGradientCheck= true;
            if (runGradientCheck)
            {
                log.Info("Running gradient check on " + trainSentences.Count + " trees");
                dvparser.RunGradientCheck(trainSentences, trainCompressedParses);
            }
            if (runTraining)
            {
                log.Info("Training the RNN parser");
                log.Info("Current train options: " + dvparser.GetOp().trainOptions);
                dvparser.Train(trainSentences, trainCompressedParses, testTreebank, modelPath, resultsRecordPath);
                if (modelPath != null)
                {
                    dvparser.SaveModel(modelPath);
                }
            }
            if (testTreebankPath != null)
            {
                EvaluateTreebank evaluator = new EvaluateTreebank(dvparser.AttachModelToLexicalizedParser());
                evaluator.TestOnTreebank(testTreebank);
            }
            log.Info("Successfully ran DVParser");
        }
 private void CheckPermitDuplicateEventSourceRegistration(NameValueCollection properties)
 {
     _permitDuplicateEventSourceRegistration = ArgUtils.TryParse(false, ArgUtils.GetValue(properties, "permitDuplicateEventSourceRegistration"));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TraceLoggerFactoryAdapter"/> class.
 /// </summary>
 /// <remarks>
 /// Looks for level, showDateTime, showLogName, dateTimeFormat items from
 /// <paramref name="properties" /> for use when the GetLogger methods are called.
 /// <see cref="ConfigurationSectionHandler"/> for more information on how to use the
 /// standard .NET application configuraiton file (App.config/Web.config)
 /// to configure this adapter.
 /// </remarks>
 /// <param name="properties">The name value collection, typically specified by the user in
 /// a configuration section named common/logging.</param>
 public TraceLoggerFactoryAdapter(NameValueCollection properties)
     : base(properties)
 {
     _useTraceSource = ArgUtils.TryParse(false, properties["useTraceSource"]);
 }
Пример #25
0
        /// <summary>
        /// An example of a command line is
        /// <br />
        /// java -mx1g edu.stanford.nlp.parser.dvparser.CacheParseHypotheses -model /scr/horatio/dvparser/wsjPCFG.nocompact.simple.ser.gz -output cached9.simple.ser.gz  -treebank /afs/ir/data/linguistic-data/Treebank/3/parsed/mrg/wsj 200-202
        /// <br />
        /// java -mx4g edu.stanford.nlp.parser.dvparser.CacheParseHypotheses -model ~/scr/dvparser/wsjPCFG.nocompact.simple.ser.gz -output cached.train.simple.ser.gz -treebank /afs/ir/data/linguistic-data/Treebank/3/parsed/mrg/wsj 200-2199 -numThreads 6
        /// <br />
        /// java -mx4g edu.stanford.nlp.parser.dvparser.CacheParseHypotheses -model ~/scr/dvparser/chinese/xinhuaPCFG.ser.gz -output cached.xinhua.train.ser.gz -treebank /afs/ir/data/linguistic-data/Chinese-Treebank/6/data/utf8/bracketed  026-270,301-499,600-999
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public static void Main(string[] args)
        {
            string parserModel = null;
            string output      = null;
            IList <Pair <string, IFileFilter> > treebanks = Generics.NewArrayList();
            int dvKBest    = 200;
            int numThreads = 1;

            for (int argIndex = 0; argIndex < args.Length;)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-dvKBest"))
                {
                    dvKBest   = System.Convert.ToInt32(args[argIndex + 1]);
                    argIndex += 2;
                    continue;
                }
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-parser") || args[argIndex].Equals("-model"))
                {
                    parserModel = args[argIndex + 1];
                    argIndex   += 2;
                    continue;
                }
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-output"))
                {
                    output    = args[argIndex + 1];
                    argIndex += 2;
                    continue;
                }
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-treebank"))
                {
                    Pair <string, IFileFilter> treebankDescription = ArgUtils.GetTreebankDescription(args, argIndex, "-treebank");
                    argIndex = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                    treebanks.Add(treebankDescription);
                    continue;
                }
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-numThreads"))
                {
                    numThreads = System.Convert.ToInt32(args[argIndex + 1]);
                    argIndex  += 2;
                    continue;
                }
                throw new ArgumentException("Unknown argument " + args[argIndex]);
            }
            if (parserModel == null)
            {
                throw new ArgumentException("Need to supply a parser model with -model");
            }
            if (output == null)
            {
                throw new ArgumentException("Need to supply an output filename with -output");
            }
            if (treebanks.IsEmpty())
            {
                throw new ArgumentException("Need to supply a treebank with -treebank");
            }
            log.Info("Writing output to " + output);
            log.Info("Loading parser model " + parserModel);
            log.Info("Writing " + dvKBest + " hypothesis trees for each tree");
            LexicalizedParser    parser      = ((LexicalizedParser)LexicalizedParser.LoadModel(parserModel, "-dvKBest", int.ToString(dvKBest)));
            CacheParseHypotheses cacher      = new CacheParseHypotheses(parser);
            ITreeTransformer     transformer = DVParser.BuildTrainTransformer(parser.GetOp());
            IList <Tree>         sentences   = new List <Tree>();

            foreach (Pair <string, IFileFilter> description in treebanks)
            {
                log.Info("Reading trees from " + description.first);
                Treebank treebank = parser.GetOp().tlpParams.MemoryTreebank();
                treebank.LoadPath(description.first, description.second);
                treebank = treebank.Transform(transformer);
                Sharpen.Collections.AddAll(sentences, treebank);
            }
            log.Info("Processing " + sentences.Count + " trees");
            IList <Pair <Tree, byte[]> > cache = Generics.NewArrayList();

            transformer = new SynchronizedTreeTransformer(transformer);
            MulticoreWrapper <Tree, Pair <Tree, byte[]> > wrapper = new MulticoreWrapper <Tree, Pair <Tree, byte[]> >(numThreads, new CacheParseHypotheses.CacheProcessor(cacher, parser, dvKBest, transformer));

            foreach (Tree tree in sentences)
            {
                wrapper.Put(tree);
                while (wrapper.Peek())
                {
                    cache.Add(wrapper.Poll());
                    if (cache.Count % 10 == 0)
                    {
                        System.Console.Out.WriteLine("Processed " + cache.Count + " trees");
                    }
                }
            }
            wrapper.Join();
            while (wrapper.Peek())
            {
                cache.Add(wrapper.Poll());
                if (cache.Count % 10 == 0)
                {
                    System.Console.Out.WriteLine("Processed " + cache.Count + " trees");
                }
            }
            System.Console.Out.WriteLine("Finished processing " + cache.Count + " trees");
            IOUtils.WriteObjectToFile(cache, output);
        }
Пример #26
0
 /// <summary>
 /// Create a new logger instance.
 /// </summary>
 public CapturingLogger(CapturingLoggerFactoryAdapter owner, string logName)
     : base(logName, LogLevel.All, true, true, true, null)
 {
     ArgUtils.AssertNotNull("owner", owner);
     Owner = owner;
 }
Пример #27
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.TypeLoadException"/>
        public static void Main(string[] args)
        {
            string         dvmodelFile        = null;
            string         lexparserFile      = null;
            string         testTreebankPath   = null;
            IFileFilter    testTreebankFilter = null;
            IList <string> unusedArgs         = new List <string>();

            for (int argIndex = 0; argIndex < args.Length;)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-lexparser"))
                {
                    lexparserFile = args[argIndex + 1];
                    argIndex     += 2;
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-testTreebank"))
                    {
                        Pair <string, IFileFilter> treebankDescription = ArgUtils.GetTreebankDescription(args, argIndex, "-testTreebank");
                        argIndex           = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                        testTreebankPath   = treebankDescription.First();
                        testTreebankFilter = treebankDescription.Second();
                    }
                    else
                    {
                        unusedArgs.Add(args[argIndex++]);
                    }
                }
            }
            log.Info("Loading lexparser from: " + lexparserFile);
            string[]          newArgs   = Sharpen.Collections.ToArray(unusedArgs, new string[unusedArgs.Count]);
            LexicalizedParser lexparser = ((LexicalizedParser)LexicalizedParser.LoadModel(lexparserFile, newArgs));

            log.Info("... done");
            Treebank testTreebank = null;

            if (testTreebankPath != null)
            {
                log.Info("Reading in trees from " + testTreebankPath);
                if (testTreebankFilter != null)
                {
                    log.Info("Filtering on " + testTreebankFilter);
                }
                testTreebank = lexparser.GetOp().tlpParams.MemoryTreebank();
                testTreebank.LoadPath(testTreebankPath, testTreebankFilter);
                log.Info("Read in " + testTreebank.Count + " trees for testing");
            }
            double[] labelResults = new double[weights.Length];
            double[] tagResults   = new double[weights.Length];
            for (int i = 0; i < weights.Length; ++i)
            {
                lexparser.GetOp().baseParserWeight = weights[i];
                EvaluateTreebank evaluator         = new EvaluateTreebank(lexparser);
                evaluator.TestOnTreebank(testTreebank);
                labelResults[i] = evaluator.GetLBScore();
                tagResults[i]   = evaluator.GetTagScore();
            }
            for (int i_1 = 0; i_1 < weights.Length; ++i_1)
            {
                log.Info("LexicalizedParser weight " + weights[i_1] + ": labeled " + labelResults[i_1] + " tag " + tagResults[i_1]);
            }
        }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.TypeLoadException"/>
        public static void Main(string[] args)
        {
            string         modelPath          = null;
            IList <string> baseModelPaths     = null;
            string         testTreebankPath   = null;
            IFileFilter    testTreebankFilter = null;
            IList <string> unusedArgs         = new List <string>();

            for (int argIndex = 0; argIndex < args.Length;)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-model"))
                {
                    modelPath = args[argIndex + 1];
                    argIndex += 2;
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-testTreebank"))
                    {
                        Pair <string, IFileFilter> treebankDescription = ArgUtils.GetTreebankDescription(args, argIndex, "-testTreebank");
                        argIndex           = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                        testTreebankPath   = treebankDescription.First();
                        testTreebankFilter = treebankDescription.Second();
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-baseModels"))
                        {
                            argIndex++;
                            baseModelPaths = new List <string>();
                            while (argIndex < args.Length && args[argIndex][0] != '-')
                            {
                                baseModelPaths.Add(args[argIndex++]);
                            }
                            if (baseModelPaths.Count == 0)
                            {
                                throw new ArgumentException("Found an argument -baseModels with no actual models named");
                            }
                        }
                        else
                        {
                            unusedArgs.Add(args[argIndex++]);
                        }
                    }
                }
            }
            string[]          newArgs          = Sharpen.Collections.ToArray(unusedArgs, new string[unusedArgs.Count]);
            LexicalizedParser underlyingParser = null;
            Options           options          = null;
            LexicalizedParser combinedParser   = null;

            if (baseModelPaths != null)
            {
                IList <DVModel> dvparsers = new List <DVModel>();
                foreach (string baseModelPath in baseModelPaths)
                {
                    log.Info("Loading serialized DVParser from " + baseModelPath);
                    LexicalizedParser dvparser = ((LexicalizedParser)LexicalizedParser.LoadModel(baseModelPath));
                    IReranker         reranker = dvparser.reranker;
                    if (!(reranker is DVModelReranker))
                    {
                        throw new ArgumentException("Expected parsers with DVModel embedded");
                    }
                    dvparsers.Add(((DVModelReranker)reranker).GetModel());
                    if (underlyingParser == null)
                    {
                        underlyingParser = dvparser;
                        options          = underlyingParser.GetOp();
                        // TODO: other parser's options?
                        options.SetOptions(newArgs);
                    }
                    log.Info("... done");
                }
                combinedParser = LexicalizedParser.CopyLexicalizedParser(underlyingParser);
                CombinedDVModelReranker reranker_1 = new CombinedDVModelReranker(options, dvparsers);
                combinedParser.reranker = reranker_1;
                combinedParser.SaveParserToSerialized(modelPath);
            }
            else
            {
                throw new ArgumentException("Need to specify -model to load an already prepared CombinedParser");
            }
            Treebank testTreebank = null;

            if (testTreebankPath != null)
            {
                log.Info("Reading in trees from " + testTreebankPath);
                if (testTreebankFilter != null)
                {
                    log.Info("Filtering on " + testTreebankFilter);
                }
                testTreebank = combinedParser.GetOp().tlpParams.MemoryTreebank();
                testTreebank.LoadPath(testTreebankPath, testTreebankFilter);
                log.Info("Read in " + testTreebank.Count + " trees for testing");
                EvaluateTreebank evaluator = new EvaluateTreebank(combinedParser.GetOp(), null, combinedParser);
                evaluator.TestOnTreebank(testTreebank);
            }
        }
Пример #29
0
 /// <summary>
 /// Creates this target using a custom layout.
 /// </summary>
 public CommonLoggingTarget(string layout)
 {
     ArgUtils.AssertNotNull("layout", layout);
     this.Layout = layout;
 }
Пример #30
0
        public static void Main(string[] args)
        {
            IList <string> remainingArgs = Generics.NewArrayList();
            IList <Pair <string, IFileFilter> > trainTreebankPath = null;
            Pair <string, IFileFilter>          testTreebankPath  = null;
            Pair <string, IFileFilter>          devTreebankPath   = null;
            string serializedPath   = null;
            string tlppClass        = null;
            string continueTraining = null;

            for (int argIndex = 0; argIndex < args.Length;)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-trainTreebank"))
                {
                    if (trainTreebankPath == null)
                    {
                        trainTreebankPath = Generics.NewArrayList();
                    }
                    trainTreebankPath.Add(ArgUtils.GetTreebankDescription(args, argIndex, "-trainTreebank"));
                    argIndex = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                }
                else
                {
                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-testTreebank"))
                    {
                        testTreebankPath = ArgUtils.GetTreebankDescription(args, argIndex, "-testTreebank");
                        argIndex         = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                    }
                    else
                    {
                        if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-devTreebank"))
                        {
                            devTreebankPath = ArgUtils.GetTreebankDescription(args, argIndex, "-devTreebank");
                            argIndex        = argIndex + ArgUtils.NumSubArgs(args, argIndex) + 1;
                        }
                        else
                        {
                            if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-serializedPath") || Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-model"))
                            {
                                serializedPath = args[argIndex + 1];
                                argIndex      += 2;
                            }
                            else
                            {
                                if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-tlpp"))
                                {
                                    tlppClass = args[argIndex + 1];
                                    argIndex += 2;
                                }
                                else
                                {
                                    if (Sharpen.Runtime.EqualsIgnoreCase(args[argIndex], "-continueTraining"))
                                    {
                                        continueTraining = args[argIndex + 1];
                                        argIndex        += 2;
                                    }
                                    else
                                    {
                                        remainingArgs.Add(args[argIndex]);
                                        ++argIndex;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            string[] newArgs = new string[remainingArgs.Count];
            newArgs = Sharpen.Collections.ToArray(remainingArgs, newArgs);
            if (trainTreebankPath == null && serializedPath == null)
            {
                throw new ArgumentException("Must specify a treebank to train from with -trainTreebank or a parser to load with -serializedPath");
            }
            ShiftReduceParser parser = null;

            if (trainTreebankPath != null)
            {
                log.Info("Training ShiftReduceParser");
                log.Info("Initial arguments:");
                log.Info("   " + StringUtils.Join(args));
                if (continueTraining != null)
                {
                    parser = ((ShiftReduceParser)ShiftReduceParser.LoadModel(continueTraining, ArrayUtils.Concatenate(ForceTags, newArgs)));
                }
                else
                {
                    ShiftReduceOptions op = BuildTrainingOptions(tlppClass, newArgs);
                    parser = new ShiftReduceParser(op);
                }
                parser.Train(trainTreebankPath, devTreebankPath, serializedPath);
                parser.SaveModel(serializedPath);
            }
            if (serializedPath != null && parser == null)
            {
                parser = ((ShiftReduceParser)ShiftReduceParser.LoadModel(serializedPath, ArrayUtils.Concatenate(ForceTags, newArgs)));
            }
            //parser.outputStats();
            if (testTreebankPath != null)
            {
                log.Info("Loading test trees from " + testTreebankPath.First());
                Treebank testTreebank = parser.op.tlpParams.MemoryTreebank();
                testTreebank.LoadPath(testTreebankPath.First(), testTreebankPath.Second());
                log.Info("Loaded " + testTreebank.Count + " trees");
                EvaluateTreebank evaluator = new EvaluateTreebank(parser.op, null, parser);
                evaluator.TestOnTreebank(testTreebank);
            }
        }