Beispiel #1
0
        public void output(string path, int n)
        {
            Read             read            = new Read();
            string           str             = read.Read1(path);
            ICountCharacters countcharacters = new CountCharacters();
            ValidLine        validline       = new ValidLine();
            ICountWords      countwords      = new CountWords();
            ITopWords        toptenwords     = new TopWords();
            bool             a = false;

            Console.WriteLine("是否输出以上数据至output.txt(是请输入Y,否则输入N)");
            char b = 'N';

            b = Convert.ToChar(Console.ReadLine());
            if (b == 'Y')
            {
                a = true;
            }
            if (a)
            {
                string       FileName = @"C: \Users\24554\Desktop\第三次作业\201731062117\wordCount\output.txt";
                StreamWriter sw       = new StreamWriter(FileName);
                sw.WriteLine("\n\ncharacters:" + countcharacters.Countcharacters(str));
                sw.WriteLine("validline:" + validline.ValidLine1(path));
                sw.WriteLine("countwords:" + countwords.Countwords(str));
                sw.WriteLine("toptenwords:\n\n" + toptenwords.Topnwords(countwords.Getwords(str), n));
                Console.WriteLine("保存成功!!!");
                sw.Close();
            }
        }
Beispiel #2
0
        public void output(string path, int n)
        {
            Read             read            = new Read();
            string           str             = read.Read1(path);
            ICountCharacters countcharacters = new CountCharacters();
            ValidLine        validline       = new ValidLine();
            ICountWords      countwords      = new CountWords();
            ITopWords        toptenwords     = new TopWords();
            bool             a = false;

            Console.WriteLine("是否输出以上数据至output.txt(是请输入Y,否则输入N)");
            char b = 'N';

            b = Convert.ToChar(Console.ReadLine());
            if (b == 'Y')
            {
                a = true;
            }
            if (a)
            {
                string FileName = @"D:\output.txt";
                //FileStream fs = new FileStream("D:\\wordCount\\wordCount\\wordCount\\bin\\Debug\\123.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine("\n\ncharacters:" + countcharacters.Countcharacters(str));
                sw.WriteLine("validline:" + validline.ValidLine1(path));
                sw.WriteLine("countwords:" + countwords.Countwords(str));
                sw.WriteLine("toptenwords:\n\n" + toptenwords.Topnwords(countwords.Getwords(str), n));
                Console.WriteLine("保存成功,可前往D盘查看!");
                sw.Close();
            }
        }
Beispiel #3
0
        /// <summary>
        /// 进行各种统计的函数
        /// </summary>
        /// <param name="s"></param>
        static void Operate(string s)
        {
            CountCharacters count_characters = new CountCharacters(s);

            count_characters.Count_character();
            CountLines count_lines = new CountLines(s);

            count_lines.Count_line();
            CountWords count_words = new CountWords(s);

            count_words.Count_word();
            count_words.Count_word_frequency();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string str  = null;
            string path = null;
            int    n;

            if (args.Length != 0)
            {
                path = args[0];
            }
            if (path == null)
            {
                Console.WriteLine("请输入文件路径:");
                path = Console.ReadLine();
            }
            Read read = new Read();

            while (true)
            {
                //异常处理文件路径输错的情况
                try
                {
                    str = read.Read1(path);
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message.ToString());
                    Console.WriteLine("\n请重新输入文件路径:");
                    path = Console.ReadLine();
                }
            }
            ICountCharacters countcharacters = new CountCharacters();
            ValidLine        validline       = new ValidLine();
            ICountWords      countwords      = new CountWords();
            ITopWords        toptenwords     = new TopWords();

            Console.WriteLine("\n\ncharacters:" + countcharacters.Countcharacters(str));
            Console.WriteLine("\nvalidline:" + validline.ValidLine1(path));
            Console.WriteLine("\ncountwords:" + countwords.Countwords(str));
            Console.WriteLine("\n输入需要显示的前多少个单词 n :");
            n = Int32.Parse(Console.ReadLine());
            Console.WriteLine("\ntoptenwords:\n\n" + toptenwords.Topnwords(countwords.Getwords(str), n));
            Output output = new Output();

            output.output(path, n);
        }
Beispiel #5
0
        /// <summary>
        /// 进行各种统计的函数
        /// </summary>
        /// <param name="s"></param>
        static void Operate(string[] args)
        {
            string        file = null;
            List <string> textlist = new List <string>();
            List <string> outputlist = new List <string>();
            const string  i = "-i", n = "-n", o = "-o", m = "-m";

            string[]      phrase         = null;
            string[]      word_frequency = null;
            List <string> list           = new List <string>();

            foreach (var s in args)
            {
                list.Add(s);
            }
            int j = 0;

            foreach (var s in list)
            {
                if (s == i)
                {
                    file = list[j + 1];
                    if (OpenFile(file, ref textlist))
                    {
                    }
                    else
                    {
                        Console.WriteLine("打开文件失败!1");
                    }
                    break;
                }
                ++j;
            }
            CountCharacters character = new CountCharacters(textlist);
            string          ctr       = character.Count_character();

            Console.WriteLine(ctr);
            CountWords word = new CountWords(textlist);
            string     ws   = word.Count_word();

            Console.WriteLine(ws);
            CountLines lines = new CountLines(textlist);
            string     ls    = lines.Count_line();

            Console.WriteLine(ls);
            for (int k = 0; k < list.LongCount(); ++k)
            {
                if (list[k] == m)
                {
                    int        num   = int.Parse(list[++k]);
                    CountWords words = new CountWords(textlist);
                    phrase = words.Count_Phrase(num);
                }
                else if (list[k] == n)
                {
                    int        num   = int.Parse(list[++k]);
                    CountWords words = new CountWords(textlist);
                    word_frequency = words.Count_word_frequency(num);
                }
                else
                {
                    continue;
                }
            }
            outputlist.Add(ctr);
            outputlist.Add(ws);
            outputlist.Add(ls);
            outputlist.Add("\r");
            Console.WriteLine();
            if (word_frequency != null)
            {
                foreach (var s in word_frequency)
                {
                    outputlist.Add(s);
                    Console.WriteLine(s);
                }
            }
            outputlist.Add("\r");
            Console.WriteLine();
            if (phrase != null)
            {
                foreach (var s in phrase)
                {
                    outputlist.Add(s);
                }
            }
            for (int l = 0; l < list.LongCount(); ++l)
            {
                if (list[l] == o)
                {
                    string outfile = list[++l];
                    if (WriteFile(outfile, outputlist))
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("打开文件失败");
                        break;
                    }
                }
            }
        }