private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            tex.Text = Guess;

            Dictionary <string, List <nodePosting> > invertedList = new Dictionary <string, List <nodePosting> >();
            nodePosting p = new nodePosting();


            using (BinaryReader reader = new BinaryReader(File.Open("inverted.dat", FileMode.Open)))
                using (BinaryReader reader2 = new BinaryReader(File.Open("index.dat", FileMode.Open)))
                {
                    while (reader2.BaseStream.Position < reader2.BaseStream.Length)
                    {
                        string s = reader2.ReadString();
                        int    a = reader2.ReadInt32();
                        for (int i = 0; i < a; i++)
                        {
                            p.noDoc   = reader.ReadInt32();
                            p.posting = reader.ReadInt32();
                            p.noPage  = reader.ReadInt32();
                            p.nameDoc = reader.ReadString();
                            if (!invertedList.ContainsKey(s))
                            {
                                invertedList.Add(s, new List <nodePosting> {
                                    p
                                });
                            }
                            else
                            {
                                invertedList[s].Add(p);
                            }
                            p = null;
                            p = new nodePosting();
                        }
                    }
                    if (invertedList.ContainsKey(Guess))
                    {
                        list_searching.ItemsSource = invertedList[Guess];
                    }
                    else
                    {
                        tex.Text += " not found";
                    }
                }
        }
Esempio n. 2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Multiselect = true;                      // choose multi File
                openFileDialog.Filter      = "Text Files|*.txt";

                if (openFileDialog.ShowDialog() == true)
                {
                    if (File.Exists(@"inverted.dat"))
                    {
                        File.Delete(@"inverted.dat");
                    }
                    if (File.Exists(@"index.dat"))
                    {
                        File.Delete(@"index.dat");
                    }

                    int    noPage         = 0;
                    int    noDocument     = 0;
                    int    noWordPosition = 0;
                    string endPage        = "*****";



                    Dictionary <string, List <nodePosting> > invertedList = new Dictionary <string, List <nodePosting> >();


                    if (!File.Exists(@"stopwords.txt"))
                    {
                        throw new Exception("1");
                    }


                    foreach (string filename in openFileDialog.FileNames)
                    {
                        if (System.IO.Path.GetExtension(filename) != ".txt")
                        {
                            throw new Exception();
                        }
                        noDocument++;//number file
                        noPage         = 0;
                        noWordPosition = 0;
                        using (StreamReader sr = new StreamReader(filename))
                        {
                            noPage++;
                            string   line;
                            string[] words;
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line))
                                {
                                    continue;
                                }
                                line  = Regex.Replace(line, @"(?:\s+|[^A-Za-z*])", " ");
                                words = line.Split();
                                foreach (string word1 in words)
                                {
                                    noWordPosition++;
                                    string word = word1.Trim().ToLower();
                                    if (word == endPage)
                                    {
                                        noPage++;
                                        continue;
                                    }
                                    else if (String.IsNullOrEmpty(word))
                                    {
                                        continue;
                                    }
                                    else if (stopwords.ContainsKey(word))
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        nodePosting temp = new nodePosting();
                                        temp.noDoc   = noDocument;
                                        temp.posting = noWordPosition;
                                        temp.noPage  = noPage;
                                        temp.nameDoc = filename;

                                        if (invertedList.ContainsKey(word))
                                        {
                                            invertedList[word].Add(temp);
                                        }
                                        else
                                        {
                                            List <nodePosting> parts = new List <nodePosting>();
                                            parts.Add(temp);
                                            invertedList.Add(word, parts);
                                        }
                                    }
                                } // end words
                            }     //end read lines
                        }         // end using
                    }             //end for files
                     //Create index file

                    using (BinaryWriter writer = new BinaryWriter(File.Open("inverted.dat", FileMode.Create)))   // save posting file
                        using (BinaryWriter writer2 = new BinaryWriter(File.Open("index.dat", FileMode.Create))) // save index for read in posting file
                        {
                            foreach (KeyValuePair <string, List <nodePosting> > kvp in invertedList)
                            {
                                foreach (nodePosting nop in kvp.Value)
                                {
                                    writer.Write(nop.noDoc);
                                    writer.Write(nop.posting);
                                    writer.Write(nop.noPage);
                                    writer.Write(nop.nameDoc);
                                }
                                writer2.Write(kvp.Key);
                                writer2.Write(kvp.Value.Count);
                            }
                        }
                    //
                    butSerch.IsEnabled = true;// don
                }
            }//end try
            catch (Exception e1)
            {
                butSerch.IsEnabled = false;// don
                if (e1.Message == "1")
                {
                    MessageBox.Show("stopwords.txt does not exist");
                }
                else
                {
                    MessageBox.Show("Please choose a valid file");
                }
            }
        }