3 Replies - 216 Views - Last Post: 19 January 2013 - 11:22 AM Rate Topic: -----

#1 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

pulling best match from textfile search

Posted 18 January 2013 - 11:03 PM

Hello
the code below will pull a matched word from a text file and display the line it matched something on. It will even display 3 lines that matched the same word.

but what I really want to do is to take the line with the most matches so if textbox1.text was entered "How would I learn c sharp the fastest" than it would take the whole sentence into account and then display the most accurate line with the most matches from the text file.


private void button1_Click(object sender, EventArgs e)
        {
            int counter = 0; string line;
            StringBuilder sb = new StringBuilder();

            // Read the file and display it line by line.              
            using (System.IO.StreamReader file = new System.IO.StreamReader("C:\\TextFile1.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains(textBox1.Text))
                    {
                        // append the text and a newline into the StringBuilder buffer       
                        sb.AppendLine(line.ToString());
                    }
                }
            }
            textBox2.Text = sb.ToString();
        }
   }
}


Is This A Good Question/Topic? 0
  • +

Replies To: pulling best match from textfile search

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,358
  • Joined: 02-June 10

Re: pulling best match from textfile search

Posted 19 January 2013 - 07:58 AM

Doing content and context understanding is very tough subject, border on AI.

But if you just want to match by counting the number of words that match that should just be a couple of nested loops.

You could determine that sentence 1 has 5 matches to your word list, sentence 2 has 8 matches and sentence 3 has 9 matches - so sentence 3 wins.

Hard drives are slow, memory is fast. If the word list can fit in memory then read it one time so you can loop through it as often as you like without going to HDD over and over.

So you just need a couple nested loops

For each sentence
{
   for each word in this sentence
   {
      for each word in the word list
      {
          do they match. If so, add to the score for this sentence
      }
   }
}


Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

This post has been edited by tlhIn`toq: 19 January 2013 - 07:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: pulling best match from textfile search

Posted 19 January 2013 - 11:10 AM

Sounds like a good way to get it to do what I want.

could I get you to show me a way to use a nested loop into my existing code?

thanks
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,358
  • Joined: 02-June 10

Re: pulling best match from textfile search

Posted 19 January 2013 - 11:22 AM

You at least need to make an effort on your own. We will help you with your code, but we won't write it for you. Go back to your book and find the chapter that does loops. It will have an example.

Once you have at least tried to write your own come back to this thread with the code you have written and any errors or problems you're encountering.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1