Получить слова рангов из массива - PullRequest
0 голосов
/ 30 октября 2009

Я сделал этот код, который имеет три строки, как вы видите, я выполняю много запросов

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 от пользователя (write / readline), а затем выполнила поиск по каждой (строке) и присвоила мне ранг слова (каковым является слово, которое повторяется больше) опыт: Java --- 4 раза \ Hard --- 5

Теперь я знаю, как читать слово, и я знаю, что нужно разбивать строки, чтобы получить слова, но как можно получить ранг для каждого слова и затем расположить их в соответствии с их рангами?

1 Ответ

1 голос
/ 30 октября 2009

С LINQ это действительно просто:

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

(где words - это IEnumerable<string> некоторого вида, например, массив.)

Я вижу, что вы используете ArrayList, несмотря на то, что, по крайней мере, явно имеете .NET 2.0 (судя по директивам using) - есть ли причина по-прежнему использовать неуниверсальные коллекции?

РЕДАКТИРОВАТЬ: Хорошо, решение не для 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...