从数组中获取单词排名

时间:2009-10-30 11:45:20

标签: c# search

我制作了这个代码,它有三个字符串,你看我执行了很多查询

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace IR
{
    class Program
    {
        public static bool Search(string[] stop, string w)
        {
            for (int i = 0; i < stop.Length; i++)
                if (w == stop[i])
                    return true;
            return false;
            // i didnt add the else thing becuse i want it to continue searching
        }

        public static int Count(ArrayList doc, string term)
        {
            int c=0;
            for (int i = 0; i < doc.Count; i++)
            {
                if (term == doc[i].ToString())
                {
                    c++;
                }

            }
            return c; // be carefull keep this put of the for
        }

        static void Main(string[] args)
        {
            string d1 = "java is fun. java is a programming language.";
            string d2 = "java is a hard language of all language.";
            string d3 = "all language is hard. hard language.";

            string[] stop={"","is","all","a","of","the","to","at"};
            string[] d1words = d1.Split(' ','.');
            string[] d2words = d2.Split(' ', '.');
            string[] d3words = d3.Split(' ', '.');


            //removing stop words
            //after addding the using system collections

            ArrayList d1good = new ArrayList();
            for (int i = 0; i < d1words.Length; i++)
                if (Search(stop, d1words[i]) == false)
                    d1good.Add(d1words[i]);

            ArrayList d2good = new ArrayList();
            for (int i = 0; i < d2words.Length; i++)
                if (Search(stop, d2words[i]) == false)
                    d2good.Add(d2words[i]);

            ArrayList d3good = new ArrayList();
            for (int i = 0; i < d3words.Length; i++)
                if (Search(stop, d3words[i]) == false)
                    d3good.Add(d3words[i]);


            for (int i = 0; i < d1good.Count; i++)

                Console.WriteLine(d1good[i]);

            Console.WriteLine("----------------------------");

            for (int i = 0; i < d2good.Count; i++)

                Console.WriteLine(d2good[i]);

            Console.WriteLine("----------------------------");

            for (int i = 0; i < d3good.Count; i++)

                Console.WriteLine(d3good[i]);

            Console.WriteLine("----------------------------");



            double[,] W = new double[3, 5];
            W[0, 0] = Count(d1good, "java") * Math.Log(3.0 / 2.0 );// the 2 i have to count it not just add it i just did it to save time
            W[0, 1] = Count(d1good, "programming") * Math.Log(3.0 / 1.0);
            W[0, 2] = Count(d1good, "language") * Math.Log(3.0 / 3.0);
            W[0, 3] = Count(d1good, "hard") * Math.Log(3.0 / 2.0);
            W[0, 4] = Count(d1good, "fun") * Math.Log(3.0 / 1.0);

            W[1, 0] = Count(d2good, "java") * Math.Log(3.0 / 2.0);// the 2 i have to count it not just add it i just did it to save time
            W[1, 1] = Count(d2good, "programming") * Math.Log(3.0 / 1.0);
            W[1, 2] = Count(d2good, "language") * Math.Log(3.0 / 3.0);
            W[1, 3] = Count(d2good, "hard") * Math.Log(3.0 / 2.0);
            W[1, 4] = Count(d2good, "fun") * Math.Log(3.0 / 1.0);

            W[2, 0] = Count(d3good, "java") * Math.Log(3.0 / 2.0);// the 2 i have to count it not just add it i just did it to save time
            W[2, 1] = Count(d3good, "programming") * Math.Log(3.0 / 1.0);
            W[2, 2] = Count(d3good, "language") * Math.Log(3.0 / 3.0);
            W[2, 3] = Count(d3good, "hard") * Math.Log(3.0 / 2.0);
            W[2, 4] = Count(d3good, "fun") * Math.Log(3.0 / 1.0);

            Console.WriteLine("----------------------------");
            Console.WriteLine("----------------------------");

            for (int i = 0; i < 3; i++)
            {
                for(int j=0; j<5; j++)
                {
                    Console.Write(W[i,j] + ", ");
                }
                Console.WriteLine();
            }

        }
    }
}

现在我希望程序从用户读取一个Word(写入/读取行),然后对每个(字符串)执行搜索并给出该单词的等级(其中重复的单词更多) exp:java --- 4次\ hard --- 5

现在我知道如何阅读一个单词,我知道我应该分割字符串来获取单词,但是获得每个单词的排名然后根据排名排列它们的方法是什么?

1 个答案:

答案 0 :(得分:1)

使用LINQ非常简单:

var frequency = words.GroupBy(word => word)
                     .ToDictionary(g => g.Key, g => g.Count());

(其中words是某种IEnumerable<string>,例如数组。)

我看到你正在使用ArrayList,尽管至少清楚地拥有.NET 2.0(根据使用指令判断) - 仍然使用非通用集合的任何理由?

编辑:好的,非LINQ解决方案(虽然我建议你学习LINQ,但实际上 - 它让生活所以更容易):

Dictionary<string, int> frequency = new Dictionary<string, int>();
foreach (string word in words)
{
    int count;
    frequency.TryGetValue(word, out count); // Will default to 0 if not found
    frequency[word] = count + 1;
}