tags:

views:

83

answers:

1

I made this code which has three strings as you see I perform many queries

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();
            }

        }
    }
}

Now I want the program to read a Word from the user ( write/readline ) and then the perform a search on each (string) and give me the rank of the word (as which is the word that is repeated more) exp : java --- 4 times \ hard --- 5

Now I know how to read a word and I know I should split the strings to get words but what is the way to get the rank for each word and then arrange them according to there ranks ?

+1  A: 

That's really easy with LINQ:

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

(Where words is an IEnumerable<string> of some kind, e.g. an array.)

I see you're using an ArrayList despite clearly having .NET 2.0 at least (judging by the using directives) - any reason for still using nongeneric collections?

EDIT: Okay, a non-LINQ solution (although I'd advise you to learn LINQ instead, really - it makes life so much easier):

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;
}
Jon Skeet
I dont want to use LINQ jon ,,, Im still not familer with itthanx for your reply
nader
With respect - surely this is best regared as an opportunity to get familiar (if that is the only reason not to use Linq)?
Murph
is it ok if you explain me what you did here ?please bare with me
nader