Exemplo n.º 1
0
        private IList <ISingleFilter> GetFilters()
        {
            var filters = new List <ISingleFilter>();

            if (filterConfig.NoFilter)
            {
                return(filters);
            }
            if (filterConfig.IgnoreEnglish)
            {
                filters.Add(new EnglishFilter());
            }
            if (filterConfig.IgnoreFirstCJK)
            {
                filters.Add(new FirstCJKFilter());
            }
            var lenFilter = new LengthFilter();

            lenFilter.MinLength = filterConfig.WordLengthFrom;
            lenFilter.MaxLength = filterConfig.WordLengthTo;

            if (filterConfig.WordLengthFrom > 1 || filterConfig.WordLengthTo < 9999)
            {
                filters.Add(lenFilter);
            }
            var rankFilter = new RankFilter();

            rankFilter.MaxLength = filterConfig.WordRankTo;
            rankFilter.MinLength = filterConfig.WordRankFrom;
            if (filterConfig.WordRankFrom > 1 || filterConfig.WordRankTo < 999999)
            {
                filters.Add(rankFilter);
            }
            if (filterConfig.IgnoreSpace)
            {
                filters.Add(new SpaceFilter());
            }
            if (filterConfig.IgnorePunctuation)
            {
                filters.Add(new ChinesePunctuationFilter());
                filters.Add(new EnglishPunctuationFilter());
            }
            if (filterConfig.IgnoreNumber)
            {
                filters.Add(new NumberFilter());
            }
            if (filterConfig.IgnoreNoAlphabetCode)
            {
                filters.Add(new NoAlphabetCodeFilter());
            }
            return(filters);
        }
Exemplo n.º 2
0
        private CommandType RunCommand(string command)
        {
            if (command == "--help" || command == "-?")
            {
                showHelp(this.cbxImportItems);
                return(CommandType.Help);
            }
            if (command == "--version" || command == "-v")
            {
                Console.WriteLine("Version:" + Assembly.GetExecutingAssembly().GetName().Version);
                return(CommandType.Help);
            }
            if (command.StartsWith("-i:"))
            {
                wordLibraryImport = GetImportInterface(command.Substring(3));
                beginImportFile   = true;
                return(CommandType.Import);
            }

            if (command.StartsWith("-o:"))
            {
                wordLibraryExport = GetExportInterface(command.Substring(3));
                beginImportFile   = false;
                return(CommandType.Export);
            }
            if (command.StartsWith("-c:")) //code
            {
                codingFile = command.Substring(3);
                pattern.MappingTablePath = codingFile;
                pattern.IsPinyinFormat   = false;
                beginImportFile          = false;
                return(CommandType.Coding);
            }
            if (command.StartsWith("-ft:")) //filter
            {
                var   filterStrs = command.Substring(4);
                Regex lenRegex   = new Regex(@"len:(\d+)-(\d+)");
                Regex rankRegex  = new Regex(@"rank:(\d+)-(\d+)");
                Regex rmRegex    = new Regex(@"rm:(\w+)");
                foreach (var filterStr in filterStrs.Split('|'))
                {
                    if (lenRegex.IsMatch(filterStr))
                    {
                        var match        = lenRegex.Match(filterStr);
                        var from         = Convert.ToInt32(match.Groups[1].Value);
                        var to           = Convert.ToInt32(match.Groups[2].Value);
                        var numberFilter = new LengthFilter()
                        {
                            MinLength = from, MaxLength = to
                        };
                        this.filters.Add(numberFilter);
                    }
                    else if (rankRegex.IsMatch(filterStr))
                    {
                        var match   = rankRegex.Match(filterStr);
                        var from    = Convert.ToInt32(match.Groups[1].Value);
                        var to      = Convert.ToInt32(match.Groups[2].Value);
                        var rFilter = new RankFilter()
                        {
                            MinLength = from, MaxLength = to
                        };
                        this.filters.Add(rFilter);
                    }
                    else if (rmRegex.IsMatch(filterStr))
                    {
                        var           match  = rmRegex.Match(filterStr);
                        var           rmType = match.Groups[1].Value;
                        ISingleFilter filter;
                        switch (rmType)
                        {
                        case "eng": filter = new EnglishFilter(); break;

                        case "num": filter = new NumberFilter(); break;

                        case "space": filter = new SpaceFilter(); break;

                        case "pun": filter = new EnglishPunctuationFilter(); break;

                        default: throw new ArgumentException("Unsupport filter type:" + rmType);
                        }
                        this.filters.Add(filter);
                    }
                }
                return(CommandType.Coding);
            }
            if (command.StartsWith("-ct:")) //code type
            {
                var codeType = command.Substring(4).ToLower();
                switch (codeType)
                {
                case "pinyin": pattern.CodeType = CodeType.Pinyin; break;

                case "wubi": pattern.CodeType = CodeType.Wubi; break;

                case "zhengma": pattern.CodeType = CodeType.Zhengma; break;

                case "cangjie": pattern.CodeType = CodeType.Cangjie; break;

                case "zhuyin": pattern.CodeType = CodeType.TerraPinyin; break;

                default: pattern.CodeType = CodeType.Pinyin; break;
                }
                return(CommandType.CodeType);
            }
            if (command.StartsWith("-r:")) //Rank
            {
                var rankType = command.Substring(3).ToLower();
                switch (rankType)
                {
                case "baidu": this.wordRankGenerater = new BaiduWordRankGenerater(); break;

                case "google": this.wordRankGenerater = new GoogleWordRankGenerater(); break;

                default: {
                    var rankNumber = Convert.ToInt32(rankType);
                    var gen        = new DefaultWordRankGenerater();
                    gen.ForceUse           = true;
                    gen.Rank               = rankNumber;
                    this.wordRankGenerater = gen;
                } break;
                }
                return(CommandType.CodeType);
            }
            if (command.StartsWith("-os:")) //code type
            {
                var os = command.Substring(4).ToLower();
                switch (os)
                {
                case "windows": pattern.OS = OperationSystem.Windows; break;

                case "mac":
                case "macos": pattern.OS = OperationSystem.MacOS; break;

                case "linux":
                case "unix": pattern.OS = OperationSystem.Linux; break;

                default: pattern.OS = OperationSystem.Windows; break;
                }
                return(CommandType.OS);
            }
            if (command.StartsWith("-ld2:")) //ld2 encoding
            {
                string   ecodes = command.Substring(5);
                string[] arr    = ecodes.Split(',');

                wordEncoding = Encoding.GetEncoding(arr[0]);
                if (arr.Length > 1)
                {
                    xmlEncoding = Encoding.GetEncoding(arr[1]);
                }

                return(CommandType.Encoding);
            }
            if (command.StartsWith("-f:")) //format
            {
                format          = command.Substring(3);
                beginImportFile = false;
                var sort = new List <int>();
                for (int i = 0; i < 3; i++)
                {
                    char c = format[i];
                    sort.Add(Convert.ToInt32(c));
                }
                pattern.Sort            = sort;
                pattern.CodeSplitString = format[3].ToString();
                pattern.SplitString     = format[4].ToString();
                string t = format[5].ToString().ToLower();
                beginImportFile = false;
                if (t == "l")
                {
                    pattern.CodeSplitType = BuildType.LeftContain;
                }
                if (t == "r")
                {
                    pattern.CodeSplitType = BuildType.RightContain;
                }
                if (t == "b")
                {
                    pattern.CodeSplitType = BuildType.FullContain;
                }
                if (t == "n")
                {
                    pattern.CodeSplitType = BuildType.None;
                }
                pattern.ContainCode = (format[6].ToString().ToLower() == "y");
                pattern.ContainRank = (format[8].ToString().ToLower() == "y");
                return(CommandType.Format);
            }

            if (beginImportFile)
            {
                importPaths.AddRange(FileOperationHelper.GetFilesPath(command));
            }
            if (type == CommandType.Export)
            {
                exportPath = command;
            }
            return(CommandType.Other);
        }