예제 #1
0
        public static void Output()
        {
            body1.clear();
            body2.clear();
            body3.clear();
            filenames.clear();
            counter = 0;
            foreach (var FirstIt in Batches.Reverse())                   //Since the It's a sorted dictionary so it sorts in ascending order and we want in desending order so we iterate in reverse
            {
                foreach (var SecondIt in Batches[FirstIt.Key].Reverse()) //Same thing but for the dictionary inside the batches dictionary
                {
                    int i = 0;
                    while (i < SecondIt.Value.count()) //i<Total number of files in the biggest total number of occurence in the biggest batch EX: Query is 7 words, Batch 7, 120Times in 5 different files
                    {
                        if (FirstIt.Key == 0)          //Since there is no unique word in this file ,there is no zero batches
                        {
                            i++; continue;
                        }
                        body1.add(" Contains " + FirstIt.Key.ToString() + " words :-");
                        filenames.add(File[SecondIt.Value.getvalue(i)].ToString());
                        SortedSet <Tuple <int, string> > Sorted = new SortedSet <Tuple <int, string> >();//<Number of occurence,The word> To sort the words that we output in their number of occurence in each file
                        counter++;
                        int j = 0;
                        while (j < FirstIt.Key)//j<Number of words in this dictionary
                        {
                            int    NumberOfcurrentFile = SecondIt.Value.getvalue(i);
                            string WordO   = WordOccurenceInFiles[new Tuple <int, int>(NumberOfcurrentFile, SecondIt.Key)].getvalue(j);
                            int    WordOcc = Hashmap[NumberOfcurrentFile][WordO];
                            Sorted.Add(new Tuple <int, string>(WordOcc, WordO));

                            j++;
                        }
                        string temp = "       ";
                        foreach (var yt in Sorted.Reverse())//Itereate on the sorted map in reverse as well and then add to the body
                        {
                            temp += yt.Item2.ToString() + " Has been repeated " + yt.Item1.ToString() + " times         ";
                        }
                        body2.add(temp);
                        body3.add(FirstLines.getvalue(SecondIt.Value.getvalue(i)));
                        i++;
                    }
                }
            }
        }
예제 #2
0
        public static void InverseIndexing()
        {
            string         FileName   = dir + @"\DelimitersWords.txt";; //The file of word delimiters like and or which where what and so on
            Listt <string> DelimiterW = new Listt <string>();           //a list to store the word delimiters
            FileStream     fsIn1      = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);

            using (StreamReader sr = new StreamReader(fsIn1, Encoding.UTF8, true))
            {
                // While not at the end of the file, read lines from the file.
                while (sr.Peek() > -1)
                {
                    DelimiterW.add(sr.ReadLine());
                }
            }
            ////////////////////////////////////////////////////////

            int NumberOfFiles = 0;

            while (NumberOfFiles < File.Length)
            {
                Hashmap[NumberOfFiles] = new Dictionary <string, int>();      //Intializing an object for each index of the array of dictionary
                FileName = dir + @"\Files\" + File[NumberOfFiles].ToString(); //directory + name of file + .txt for a full directory path of each file
                string     FirstThreeLines = "", Line;
                int        TempCount = 0;
                FileStream fsIn2     = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                using (StreamReader sr = new StreamReader(fsIn2, Encoding.UTF8, true))
                {
                    // While not at the end of the file, read lines from the file.
                    while (sr.Peek() > -1)
                    {
                        Line = sr.ReadLine();
                        TempCount++;
                        if (TempCount <= 3)
                        {
                            FirstThreeLines += Line + Environment.NewLine;//writing the first lines in this string
                            if (TempCount == 3)
                            {
                                FirstLines.add(FirstThreeLines);//Pushing the whole string into the first index of the list which means the first file and so on
                            }
                        }
                        Line = Line.RemoveSpecialCharacters();
                        var words = Line.Split(' ');   //splitting the line into an array of words
                        foreach (string word in words) //loop on each word in the var words
                        {
                            bool   Flag = true;
                            string temp = word.ToLower();//change the word to lowercase
                            //foreach (string delim in DelimiterW)//loop on the word delimeters to check whether the word is a word delimiter or not
                            for (int i = 1; i <= DelimiterW.count(); i++)
                            {
                                string delim = DelimiterW.getvalue(i);
                                if (temp == delim)
                                {
                                    Flag = false; break;
                                }
                            }

                            if (Flag)                                         //If the word isn't a delimiter
                            {
                                if (Hashmap[NumberOfFiles].ContainsKey(temp)) //If this dictionary contains that key, get the value and add 1 on it and then put it back again
                                {
                                    int value;
                                    Hashmap[NumberOfFiles].TryGetValue(temp, out value);
                                    value++;
                                    Hashmap[NumberOfFiles][temp] = value;
                                }
                                else//Add the word to the dictionary with a value of one
                                {
                                    Hashmap[NumberOfFiles].Add(temp, 1);
                                }
                            }
                        }
                    }
                }

                NumberOfFiles++;
            }
        }