Beispiel #1
0
        public string[] topTen(string website)
        {
            proxy = new ServiceReference1.ServiceClient();
            string websiteString = proxy.GetWebContent(website);
            List<string> words = new List<string>();
            List<int> wordCount = new List<int>();
            string[] websiteStringParsed = websiteString.Split(new Char[] {' ', ',', '.', '\n', '\t', '>', '<', '&', '+', ';', '='});

            bool wordExists;
            int loopCount = 0;
            foreach (string value in websiteStringParsed) {

                if (value.All(Char.IsLetter) && value.Length > 1)
                {
                    wordExists = words.Exists(word => word.Equals(value));
                    if (wordExists) //if the word is in the list already, then only increase
                    //the count of it in the 2nd list
                    {
                        int index = words.FindIndex(word => word.Equals(value));
                        wordCount[index] = wordCount[index] + 1;
                    }
                    else//else add the word to the word list and make count 1
                    {
                        words.Add(value);
                        wordCount.Add(1);
                    }
                }
                //loopCount++;
            }

            //get top ten words in the word count list
            string[] topTen = new string[10];
            int[] maxCounts = {0,0,0,0,0,0,0,0,0,0};
            loopCount = 0;
            foreach (string word in words)
            {
                int maxCountLoop = 0;
                bool alreadyEntered = false;
                foreach(int max in maxCounts){
                    if (wordCount[loopCount] > max && alreadyEntered == false)
                    {
                        maxCounts[maxCountLoop] = loopCount;
                        alreadyEntered = true;
                    }
                    maxCountLoop++;
                }

                loopCount++;
            }

            for (int i = 0; i < 10; ++i)
            {
                topTen[i] = words[maxCounts[i]];
            }
            return topTen;
        }