Пример #1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("WordStats (options) file");
            }
            else if (args.Any(x => x == "--help" || x == "-h"))
            {
                Console.WriteLine("WordStats");
                Console.WriteLine("  WordStats (options) file");
                Console.WriteLine();
                Console.WriteLine("  options:");
                Console.WriteLine("    --help, --h: this message");
                Console.WriteLine("    @(fileName): setting file name");
            }
            else
            {
                string?filePath        = null;
                string?settingFilePath = null;

                foreach (var item in args)
                {
                    if (item.StartsWith("@"))
                    {
                        settingFilePath = item.Substring(1);
                    }
                    else
                    {
                        filePath = item;
                    }
                }

                if (string.IsNullOrWhiteSpace(filePath))
                {
                    throw new ArgumentException($"invalid dictionary name: {filePath}");
                }

                WordStatsSettings settings   = GetSettings(settingFilePath);
                IWordDictionary   dictionary = GetDictionary(settings.Dictionary);
                dictionary.ReadFile(filePath);

                IList <IWordCategorizer> categorizers = GetCategorizers(settings.Categorizers);
                IWordStatistics          statistics   = GetStatistics(settings.Statistics, settings.Order);
                statistics.Dictionary = dictionary;
                statistics.Execute(categorizers);
            }
        }
Пример #2
0
        private static WordStatsSettings GetSettings(string?settingFilePath)
        {
            WordStatsSettings settings;

            if (string.IsNullOrWhiteSpace(settingFilePath))
            {
                settings = new WordStatsSettings
                {
                    Dictionary = new UsingClassInfo
                    {
                        DllPath   = "WordStatistics.dll",
                        ClassName = typeof(DefaultDictionary).FullName,
                    },
                    Categorizers = new List <UsingClassInfo>
                    {
                        new UsingClassInfo
                        {
                            DllPath   = "WordStatistics.dll",
                            ClassName = typeof(LengthCategorizer).FullName,
                        },
                        new UsingClassInfo
                        {
                            DllPath   = "WordStatistics.dll",
                            ClassName = typeof(UsingCharactersCategorizer).FullName,
                        },
                    },
                    Statistics = new UsingClassInfo
                    {
                        DllPath   = "WordStatistics.dll",
                        ClassName = typeof(DefaultStatistics).FullName,
                    },
                };
            }
            else
            {
                var option = new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                };
                settings = JsonSerializer.Deserialize <WordStatsSettings>(File.ReadAllText(settingFilePath), option);
            }

            return(settings);
        }