Click here to Skip to main content
15,898,981 members
Please Sign up or sign in to vote.
1.10/5 (7 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        // index files
        private void buttonIndex_Click(object sender, EventArgs e)
        {

            if (openFileDialog.ShowDialog() != DialogResult.OK)
                return;
            
            // parse each file and build the inverted index. 
            foreach (string fileName in openFileDialog.FileNames)
            {
                string text = File.ReadAllText(fileName).Replace("\r", " ").Replace("\n", " ");
                string[] terms = text.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                // parse each term (word) in text file and put it in dictionary
                foreach (string term in terms)
                {
                    if (!InvertedIndex.ContainsKey(term))
                        InvertedIndex.Add(term, new List<string>());

                    if (!InvertedIndex[term].Contains(fileName))
                        InvertedIndex[term].Add(fileName);
                }
            }

            // update label
            labelIndex.Text = openFileDialog.FileNames.Length.ToString() + " files indexed";
        }


        // perform a seach over the inverted index built earlier
        private void buttonSearch_Click(object sender, EventArgs e)
        {

            // split query in terms
            string query = textBoxQuery.Text;
            string[] terms = query.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);


            // see documents for each term and merge them
            List<string> commonDocuments = null;
            foreach (string term in terms)
            {
                // if one term not in index, end search
                if (!InvertedIndex.ContainsKey(term))
                {
                    if (commonDocuments != null)
                        commonDocuments.Clear();
                    break;
                }

                // find documents containing all terms
                if (commonDocuments == null)
                    commonDocuments = InvertedIndex[term];
                else
                    commonDocuments = GetCommonDocuments(commonDocuments, InvertedIndex[term]);
            }


            // display results
            if (commonDocuments != null)
            {
                listBoxResults.Items.Clear();
                foreach (string fileName in commonDocuments)
                    listBoxResults.Items.Add(fileName);
            }
        }




        // merge two arrays of file indexes
        private List<string> GetCommonDocuments(List<string> documentList, List<string> newDocumentsList)
        {
            List<string> common = new List<string>();
            foreach (string file in documentList)
                if (newDocumentsList.Contains(file))
                    common.Add(file);

            return common;
        }


    }
}



Error:

Error   1   The name 'openFileDialog' does not exist in the current 
Error   2   The name 'DialogResult' does not exist in the current context   
Error   3   The name 'openFileDialog' does not exist in the current context 
Error   4   The name 'labelIndex' does not exist in the current context 
Error   5   The name 'openFileDialog' does not exist in the current context 
Error   6   The name 'textBoxQuery' does not exist in the current context   
Error   7   The name 'InvertedIndex' does not exist in the current context  
Error   8   The name 'InvertedIndex' does not exist in the current context  
Error   9   The name 'InvertedIndex' does not exist in the current context  
Error   10  The name 'listBoxResults' does not exist in the current context 
Error   11  The name 'listBoxResults' does not exist in the current context
Posted
Updated 16-Dec-13 9:02am
v3
Comments
TryAndSucceed 16-Dec-13 14:59pm    
You might be missing some namespaces. Also, make sure, you have the reference of code behind in your aspx/html front page.
[no name] 16-Dec-13 15:00pm    
what can i do?
Dave Kreskowiak 16-Dec-13 15:43pm    
This isn't a web app! It's a Console application.
Adam Zgagacz 16-Dec-13 15:01pm    
If what you are showing is 100% of your code, errors are valid. I don't see any declaration and instantiation of object listed as not existing.

But there is even more wrong with your code. It is console application and you are trying to use WinForms objects. If it is console app you don't have windows forms, labels, dialogs etc.. If you want to use them you should start from scratch and select Windows Form application when creating new project.
abbaspirmoradi 16-Dec-13 15:06pm    
and you must define all your class like:openFileDialog

Why? Because you copy-pasted this code rows from someone else's code, who understood what he/she did. You don't understand. This code snippet is part of a Windows forms application. You can't just paste it into a console application. Here is what's telling me that:
C#
namespace ConsoleApplication3
{
    class Program
    {

You can call some of forms features from a console application (like dialogs), but you won't be able to resolve the labels and lisboxes.
You might not need all this, but to be able to get rid of it and get the code working in a console application you need to understand it, and refactor it as needed. If possible at all. But you better start from the scratch.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Dec-13 20:28pm    
5ed.
—SA
Why?
Because you don't have the needed namespaces included in your source: In this case System.Windows.Forms.

Move the input cursor into the class name the compiler is complaining about.
A small blue line will appear at the beginning of the name.
Hover your mouse over the line, and a drop down will appear. Open the drop down.
You will be given options to help fix the problem. In this case, select the first option which will add the using statement to the top of your file for you.

Repeat this for all the names it is still complaining about.
 
Share this answer
 
1. OpenFileDialog Class is available in System.Windows.Forms namespace. So, you need to add below line in your code.
using System.Windows.Forms;


If you are still getting error, then you might need to add reference to System.Wondows.Forms.dll, which can be done following this step: Right Click on Reference folder in Solution Explorer. In .NET tab of the Add Referrence Dialog box, Search for System.Windows.Forms and than Click on ok button.

2. Second Error should also get resolved by following above steps.

3. For Error no 3rd to 11th mentioned by you, is coming because you have not declared any object of such names. like for resolving error no 3, you need to create an object named openFileDialog of class OpenFileDialog as below.
C#
OpenFileDialog openFileDialog = new OpenFileDialog();


Hope this will help. !! Happy Programming !!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900