Link to home
Start Free TrialLog in
Avatar of asif07
asif07

asked on

a method to open a file that contains words and load the words into a List<string>.

get the words from the input to a a file and load words in to a list of strings
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

asif07 - You need to make an attempt to write at least part of this homework yourself.

Jim
Avatar of asif07
asif07

ASKER

Jim- I have tried to do the first part, but there are errors in my method,  I just need some guidance in the early stages to understand the language.  If I see the code, I can understand it for further exercises.

Here is my code so far:
using System;
using System.IO;

public class ProgramExercise
{
        static void Main()  {
            StreamWriter sw = File.OpenText("Lines.txt");
            string input;
            Console.Write("Please enter the text to be examined ");
            while ((input = sw.ReadLine()) != null)
            {
                Console.WriteLine(input);
            }
            sw.Close;

        }
      }
What is the error you get?

Jim
Avatar of asif07

ASKER

I fixed the first error, I had to write StreamReader instead of streamWriter.

I am getting one more error-only assignment, call, increment, decrement, and new object expressions can be used as a statement
I suspect that is coming from this:
sw.Close;

This is a method invocation, so it needs to have parentheses after Close, as:
sw.Close();

Jim
Avatar of asif07

ASKER

The error is fixed; however, I cannot run the program.  It is prompting that there was an error and it is requesting I want to send the error report!  It is not displaying my prompt which is asking for the text to be examined!
Can you post your current code?

Jim
Avatar of asif07

ASKER

Here is my current code-
using System;
using System.IO;


public class ProgrammingExercise
{
    public static int Main(string[] args)
    {

        StreamReader re = File.OpenText("Arungg.txt");
        string input;
        while ((input = re.ReadLine()) != null)
        {
            Console.WriteLine(input);
        }
        re.close;
        return 0;
    }
}
Avatar of asif07

ASKER

Sorry Jim, wrong file!
This is the code of my current exercise

using System;
using System.IO;

public class ProgramExercise
{
        static void Main()  {
            StreamReader sr = File.OpenText("Lines.txt");
            string input;
            Console.Write("Please enter the text to be examined ");
            while ((input = sr.ReadLine()) != null)
            {
                Console.WriteLine(input);
            }
            sr.Close();

        }
      }
You need to change this:
        re.close;

to this:
re.Close();

Jim
Avatar of asif07

ASKER

Sorry Jim, wrong file!
This is the code of my current exercise

using System;
using System.IO;

public class ProgramExercise
{
        static void Main()  {
            StreamReader sr = File.OpenText("Lines.txt");
            string input;
            Console.Write("Please enter the text to be examined ");
            while ((input = sr.ReadLine()) != null)
            {
                Console.WriteLine(input);
            }
            sr.Close();

        }
      }
Then the error has to be on this line:
StreamReader sr = File.OpenText("Lines.txt");

Did you create this file? If so, does it reside in the same directory as the executable you are running?

One more thing; To make debugging easier for you, we want the program to break into the debugger any time there is an error. Open this class in VisualStudio. Then from the main menu, select Debug/Exceptions. From the ensuing dialog, tell it to break when any exception is thrown.

Now, when you run fom the debugger, and it encounters an error, it will pop a dialog showing you the error message, and asking you to Break or Continue. Note the error message, then select Break.

Now, you are looking at live but paused code. You can see the line that caused the error - it's highlighted. You can also look at the value of variables as they are at run-time. Details depend on the version of VisualStudio. In VS2005, you can right-click a variable name, then from the context menu, select Quickwatch. It will pop a dialog tht shows you the current value of that variable. Sometimes, it's not what it is supposed to be, and that's the cause of the problem.

Jim
Avatar of asif07

ASKER

Jim, I am a little confused!

Isn't Line.txt created when the user input is read through the streamReader?
The exercise is asking to organize the lines of input text; therefore, I need to create a text file that contains the input.  

Is this the wrong procedure?
You are right. The StreamReader is for reading a file, while StreamWriter is for writing. So, to write to the file, you would use:
StreamWriter sw = new StreamWriter("Lines.txt"); // Opens or creates the file named Lines.txt in the same directory as the executable.
string aLine = "This is a line for my file.";
sw.WriteLine(aLine); // Write the string to the file and append a newline character.
sw.Flush(); // Flush the output buffer to the disk.
sw.Close(); // Close the file.

Jim

Next problem. You have been trying to read the line from the file. If you want to read it from the keyboard, you need:
            while ((input = ReadLine()) != null)
            {
                // Now write the line you just read to the file.
            }

Avatar of asif07

ASKER

Thanks a lot Jim for your help.

The program is running; however, the program is not ending.

here is the code


public class ProgramExercise
{
        static void Main()  {
            StreamWriter sw = new StreamWriter("Lines.txt");
            string fileLine;
            Console.Write("Please enter the text to be examined ");
            while ((fileLine = Console.ReadLine()) != null)
            {
                sw.WriteLine(fileLine);
               
            }
            sw.Flush();
            sw.Close();

        }
      }
I enjoy helping, as others have helped me. It keeps me on my toes too.

Do you want it to end when you hit a carriage return without any text?

If so, the problem is that you are testing the string for null. It will not be null, but it may be empty. So, change this:
            while ((fileLine = Console.ReadLine()) != null)
            {
                sw.WriteLine(fileLine);
               
            }
 to this:
            fileLine = Console.ReadLine();
            while (fileLine.Length > 0)
            {
                sw.WriteLine(fileLine);
                fileLine = Console.ReadLine();
            }

Jim
Avatar of asif07

ASKER

Jim, I am trying to do the second part you had advised-
Add a method to open your file that contains words to ignore, and load the words into a List<string>.

I am getting an error that the type or namespace name List could not be found.  

Here is my code so far:

using System;
using System.IO;

public class NewProgram
{
      
        static void Main()
        {
            StreamWriter sw = new StreamWriter("IgnoredWords.txt");
            List<string> list = new List<string>();
            string words;
            Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

            words = Console.ReadLine();
            char[] sepWords = {' ', '\n', '\r' };
            list.Add(words.Split(sepWords));
           

        }
      
}

I have another hour to submit the exercise!
Hope I can finish it by then, I have two more parts after this one:
Add a method to generate the output. In this method
For each input line (or current line if not from a file)
   A.Break the current input line into an array of words.
   B. For each word, if it is in the Ignored words, skip it. Otherwise conver that word to all caps, rebuild the phrase with the capitalized word and sen it to the output.
4. Exit the program, or go back and get another phrase from the user.


Give me any tips for faster process if you can!!!



You need to add:
using System.Collections.Generic;

To the top of the file to get the List definition.

Jim

Avatar of asif07

ASKER

Jim, I added the the line at the top; however, it is giving an error saying that it cannot change string to string[].  Here is my revised code:

using System;
using System.IO;
using System.Collections.Generic;

public class NewProgram
{
      
        static void Main()
        {
            StreamWriter sw = new StreamWriter("IgnoredWords.txt");
            List<string> list = new List<string>();
            string words;
            Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

            words = Console.ReadLine();
            char[] sepWords = {' ', '\n', '\r' };
            for(int i=0; i < sepWords.Length; i++)
            {
                list.Add(words.Split(sepWords[i]));
                sw.WriteLine(sepWords[i]);
            }
           
            sw.Close();

        }
      
}



           
Avatar of asif07

ASKER

Jim, I have to leave for the class.  I will ask the teacher for an extra day.  Thanks a lot for your help and if I do get the extension, I will bother you again in a few hours! Thanks again.

Asif
I'll be around tonight. The problem you are seeing is from this:
 list.Add(words.Split(sepWords[i]));

words.Split() returns an aarray of strings, but the list can only contain individual strings. Also, you told the user to separate the words by spaces. So, there does not appear to be a need to look for a linefeed or carriage return. We can simplify it this way:

        static void Main()
        {
            StreamWriter sw = new StreamWriter("IgnoredWords.txt");
            List<string> list = new List<string>(); // If you want to write to the file, this is not really needed.
            Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

            string wordList = Console.ReadLine();
            string[] words = wordList.Split(' ');
            for(int i=0; i < words.Length; i++)
            {
                list.Add(words[i]); // This is not needed.
                sw.WriteLine(words[i]);
            }
            sw.Flush(); // It's a good practice to specifically flush the buffer to the disk.
            sw.Close();
        }

Jim
 
Avatar of asif07

ASKER

Thanks Jim.  I understand this part, can you show me how the code would look different if I did not ask the user to separate words by spaces.  Just curious!
There has to be some sort of separator, unless you want one word per input.

Jim
Avatar of asif07

ASKER

Hello Jim!  I have couple of days to finish up this exercise.

I am a little stuck!  I wrote the first two methods in different classes.  How do I combine these methods in one class.  Can I get a little more advice how to go through the third part!
Here are my two classes that contain the methods.
Class 1:
using System;
using System.IO;

public class ProgramExercise
{
        static void Main()  {
            StreamWriter sw = new StreamWriter("Lines.txt");
            string fileLine;
            Console.Write("Please enter the text to be examined ");
            fileLine = Console.ReadLine();
            while (fileLine.Length > 0)
            {
                sw.WriteLine(fileLine);
                fileLine = Console.ReadLine();
            }
            sw.Flush();
            sw.Close();

        }
      }

Class 2:

using System;
using System.IO;
using System.Collections.Generic;

public class NewProgram
{
      
        static void Main()
        {
            StreamWriter sw = new StreamWriter("IgnoredWords.txt");
            List<string> list = new List<string>();
            string words;
            Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

            words = Console.ReadLine();
            char[] sepWords = {' ', '\n', '\r' };
            for(int i=0; i < sepWords.Length; i++)
            {
                list.Add(words.Split(sepWords[i]));
                sw.WriteLine(sepWords[i]);
            }
           
            sw.Close();

        }
      
}

Her is the third part that I need more help on!

Add a method to generate the output. In this method
For each input line (or current line if not from a file)
   A.Break the current input line into an array of words.
   B. For each word, if it is in the Ignored words, skip it. Otherwise conver that word to all caps, rebuild the phrase with the capitalized word and sen it to the output.

As to my understanding, We need to create file readers which read the ignoredWords file and the Lines file(which contains the actual text).  

How do I read the file word by word and compare it?
How do I rebuild the phrase with capitalized word?

Please send me some functions that I need to use in the method (like FileReader).

As always, Thanks for your help!
Here's code with the two combined into one program, where main invokes two methods.

The rest isn't that difficult. I would add a method called BuildOutput or somesuch. It needs two parameters, the path for each of the files we created, and it should return a string, which you can send to the screen in Main.

So, first, we need to open and read the two files we just created. This is your chance to use StreamReader. Since you wrote (potentially) multiple lines, use ReadLine to read each one in, and add it to a List<string>.

Then, after you fix the bug in GetIgnoredWords (Look carefully at what you wrote to the file), load the ignored words into another List<string>.

Now, for each input line, you can use string.Split() to break the lines into words to examine - be careful of punctuation in the input string when comparing with ignored words. Take a look at String.EndsWith().

Now, create a new StringBuilder, loop over the input words and apply your rules, writing each to the stringBuilder for output. You can use StringBuilder.Append("\n") to put a carriage return at the end of a line, and keep putting all the output into the same stringbuilder. When you finish composing the output, just return StringBuilder.ToString() to main, and it can write that string to the screen.

Jim

using System;
using System.Collections.Generic; // Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.
 
public class ProgramExercise
{
   static void Main() 
   {
      string textPath = "Lines.txt";
      string ignoredPath = "IgnoredWords.txt";
 
      GetTextLines( textPath);
      GetIgnoredWords(ignoredPath);
   }
   static void GetTextLines( string path )
   {
      StreamWriter sw = new StreamWriter(path);
      string fileLine;
      Console.Write("Please enter the text to be examined ");
      fileLine = Console.ReadLine();
      while (fileLine.Length > 0)
      {
          sw.WriteLine(fileLine);
          fileLine = Console.ReadLine();
      }
      sw.Flush();
      sw.Close();
   }
   static void GetIgnoredWords( string path)
   {
      StreamWriter sw = new StreamWriter(path);
      List<string> list = new List<string>();
      string words;
      Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
 
      words = Console.ReadLine();
      char[] sepWords = {' ', '\n', '\r' };
      for(int i=0; i < sepWords.Length; i++)
      {
          list.Add(words.Split(sepWords[i]));
          sw.WriteLine(sepWords[i]);
      }
 
      sw.Close();
   }
}

Open in new window

One bit of clarification - Just load one input string into the list at a time, process that line, then clear the list and get the next one, if there is one.

This will probably be easier for you if you create some additional methods called by the output builder. Then it's easier to see the logic flow and fix problems you may encounter.

Jim
Avatar of asif07

ASKER

Jim. Today is the last day to finish this exercise.  I have spent quite a few hours trying to finish the exercise, but i am confused.  I fixed the bug in GetIgnoredWords

i am getting an error when i try to add anything to the list<string>.  when do i change a word to uppercase.  In which loop?

Here is my code so far:
using System;
using System.Collections.Generic; // Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreProgram
{
    static void Main()
   {
      string textPath = "Lines.txt";
      string ignoredPath = "IgnoredWords.txt";
 
      GetTextLines( textPath);
      GetIgnoredWords(ignoredPath);
   }    

    static void GetTextLines( string path )
   {
      StreamWriter sw = new StreamWriter(path);
      string fileLine;
      Console.Write("Please enter the text to be examined ");
      fileLine = Console.ReadLine();
      while (fileLine.Length > 0)
      {
          sw.WriteLine(fileLine);
          fileLine = Console.ReadLine();
      }
      sw.Flush();
      sw.Close();
   }
   static void GetIgnoredWords( string path)
   {
      StreamWriter sw = new StreamWriter(path);
      Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

      string wordList = Console.ReadLine();
      string[] words = wordList.Split(' ');
      for(int i=0; i < words.Length; i++)
      {
          sw.WriteLine(words[i]);
      }
      sw.Flush();
      sw.Close();
   }

   static string BuildOutput(string path1, string path2)
   {
       StreamReader sr = new StreamReader(path1);
       
       List<string> list1 = new List<string>();
       string text;
       while ((text = sr.ReadLine()) !=null)
       {

           list1.Add(text);
       }

       StreamReader sr2 = new StreamReader(path2);
       List<string> list2 = new List<string>();
       string ignoredLine;

       while ((ignoredLine = sr2.ReadLine()) != null)
        {
           list2.Add(ignoredLine);
        }
    }

}
      
1. What is the error?
2. Have you set your debugger to break on all exceptions?

Jim
Avatar of asif07

ASKER

Jim, I don't know how to use the debugger.

The error is:  ignoreprogram.buildouptut <string, string> not all code paths return a value.
The error is because it's a method that's supposed to return a string, and it's not returning anything.

See below.

Jim



  static string BuildOutput(string ignorePath, string inputPath)
   {
       StringBuilder sb = new StringBuilder(1024);
       StreamReader sr = new StreamReader(ignorePath);
       
       List<string> ignoredWords = new List<string>();
       string text;
       while ((text = sr.ReadLine()) !=null)
       {
           ignoredWords.Add(text);
       }
 
       StreamReader sr2 = new StreamReader(inputPath);
       List<string> inputs = new List<string>();
 
       while ((text= sr2.ReadLine()) != null)
        {
           inputs.Add(text);
        }
       // Now, one of the lists contains ignored words,
       // And the other contains your lines to process.
       // loop over the input lines
       // for each line, apply your rules to generate some
       // some output, and add it to the stringbuilder.
 
       // When finished, this will send all your output back to the caller.
       return sb.ToString();
    }

Open in new window

Avatar of asif07

ASKER

Jim, one more question!

how do I advance the list<string> inputs?  the loop is advancing, but how do i advance the list of strings as the loop progresses?

static string BuildOutput(string ignorePath, string inputPath)
   {
       StringBuilder sb = new StringBuilder(1024);
       StreamReader sr = new StreamReader(ignorePath);

       List<string> ignoredWords = new List<string>();
       string text;
       while ((text = sr.ReadLine()) != null)
       {
           ignoredWords.Add(text);
       }

       StreamReader sr2 = new StreamReader(inputPath);
       List<string> inputs = new List<string>();

       while ((text = sr2.ReadLine()) != null)
       {
           inputs.Add(text);
       }

       for(int i=0; i<inputs.Count; i++)
       {
           int currenti;
           string inputWords = inputs.

       // Now, one of the lists contains ignored words,
       // And the other contains your lines to process.
       // loop over the input lines
       // for each line, apply your rules to generate some
       // some output, and add it to the stringbuilder.

       // When finished, this will send all your output back to the caller.
       }
           return sb.ToString();
   }
You can index a list just like an array.
string first = inputs[0];
string second = inputs[1];
etc.

Jim
Avatar of asif07

ASKER

Jim, I am in a bit of trouble!

I showed the assignment to my classmate and I am sort of missing the assignment.

The assignment is:

You are to write a program that organizes lines of input text based on key words. Key words are words appearing in the lines that do not appear in the "ignored" file; ignored words typically include such words as "a", "in", "the", etc. For example, if the file of lines we wanted to index is named lines, and contains the text:

Behind the grandstand

Not a child left behind

Yes, yes, not

and the file ignore contains:

 

a

an

the

in

 

then executing the command

 

kwic lines ignore

 

produces

 

                 BEHIND the grandstand

Not a child left BEHIND

           Not a CHILD left behind

      Behind the GRANDSTAND

     Not a child LEFT behind

                 NOT a child left behind

       Yes, yes, NOT

                 YES, yes, not

            Yes, YES, not

 

Notice that a line appears once in the index for every instance of every key word that appears in it. Key words consist solely of (uppercase or lowercase) letters: 'a'-'z' and 'A'-'Z'. The indexed lines should preserve punctuation and be aligned on the key words. A key word used as an index is always CAPITALIZED. Be sure to sufficiently test your application.

I am reading from the console rather than reading my text files; therefore, I ams stuck and cannot proceed.

Please tell me what changes  I can make and how I can fit it in the assignment? I It is urgent!  Please!
Here is my code:

using System;
using System.Collections.Generic; // Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.


/// <summary>
/// Summary description for Class1
/// </summary>
public class IgnoreProgram
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
   {
       StringBuilder sb = new StringBuilder(1024);
       StreamReader sr = new StreamReader(ignorePath);

       List<string> ignoredWords = new List<string>();
       string text;
       while ((text = sr.ReadLine()) != null)
       {
           ignoredWords.Add(text);
       }

       StreamReader sr2 = new StreamReader(inputPath);
       List<string> inputs = new List<string>();

        int currenti = 0;
        int index= 0;

       while ((text = sr2.ReadLine()) != null)
       {
           inputs.Add(text);
       }

       for(int i=0; i<inputs.Count; i++)
       {
           string inputWord = inputs[currenti];
           
           for(int i=0; i<ignoredWords.Count; i++)
           {
               string ignoreWord = ignoredWords[index];
               if(ignoreWord.Equals(inputWord))
               {
               }


       }
           return sb.ToString();
   }
}



I'm not sure I understand the problem. You have two methods that read from the console and write files. Then you have a method to load the data from the files so you can apply the rules and generate the output. If you mean it needs to read files only, and you already have the files, just skip (or delete) the first two methods and invoke the third.

Jim
Avatar of asif07

ASKER

According to the exercise, I am suppose to check any size of files.  if I type kwic lines ignore, the program kwic is suppose to look at lines file as the input and the ignore file as the words to ignore. Any word in lines file that contains words in ignore file is skipped. At the end, the output aligns the keywords(words that do not appear in ignore file).
example:
                       BEHIND the grandstand

Not a child left BEHIND

             Not a CHILD left behind

     Behind the GRANDSTAND

     Not a child LEFT behind

                       NOT a child left behind

       Yes, yes, NOT

                        YES, yes, not

               Yes, YES, not

Hope you understand the problem.  Please tell me what changes I can make to the code to fit the problem.  

Thanks for your help.
Yesterday in a post, I put in:
       // Now, one of the lists contains ignored words,
       // And the other contains your lines to process.
       // loop over the input lines
       // for each line, apply your rules to generate some
       // some output, and add it to the stringbuilder.

where this code needs to go. The whole point of the exercise is to design the logic to accomplish this task. Everything else is just a wrapper to contain and execute this bit of code.

To start with, write out a series of steps you would have to take to do it by hand. Once you are sure they are correct, write the same steps in code. If you need help with the code, let me know.

Jim
Avatar of asif07

ASKER

Jim, i am working on the steps, but I had one question.  In the command prompt, we type kwic lines ignore

from the practice programs I did, if the program is called newprogram, i would just type newprogram to run it.  what do we need to do for running the program like this?  

as to my understanding, the text files ignore or whatever need to be written in the same directory ahead of running the program.  right?

p.s-how long are you going to be around?

i want to show my steps, so i know that i am going in the right direction.
I have a contractor coming over in about 20 minutes, will be spending a couple of hours with him.

To get Streamwriter to overwrite,  change your constructor invocations to:
 StreamWriter sw = new StreamWriter(path, true);

With the project open in dev studio, from the main menu, select Debug/Start Debugging. If you want to run it from a commad prompt, look at the name of the executable in the Bin/Debug directory where your project resides.

Jim
Avatar of asif07

ASKER

Jim,  I have been spending loads of hours trying to solve this exercise.  I am trying to give it my best shot.  Just to remind you that this is my first program after a couple of years; therefore it's a little rusty.  So bare with me if I make stupid mistakes.  I have tried to write the program on paper and this is what I came up with.  I still have a few questions which I have commented. For now, I have left the same layout.  The program is still prompting the user for the files. I will change that once I get the main part working.  Thanks for your help again!

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreProgram
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
   {
       List<string> finalArray = new List<string>();
       //StringBuilder sb = new StringBuilder(1024);
       StreamReader sr = new StreamReader(ignorePath);

       List<string> ignoredWords = new List<string>();
       string text;
       while ((text = sr.ReadLine()) != null)
       {
           ignoredWords.Add(text);
       }

       StreamReader sr2 = new StreamReader(inputPath);
       List<string> inputs = new List<string>();

       while ((text = sr2.ReadLine()) != null)
       {
           inputs.Add(text);
       }
       //kaahinm@cs.umb.edu
       
       int currenti = 0;

       for( int i = 0; i<inputs.Count; i++)
       {
           string inputWords = inputs[currenti];
           string[] words = inputWords.Split(' ');
           
           int counter;
           for (int inputIndex= 0; i < words.Length; inputIndex++)
           {
               counter = 0;
               string ignored = ignoredWords[counter];
               string[] ignoredSplit = ignored.Split(' ');
               
               for(int ignoredIndex=0; i<ignoredSplit.Length; ignoredIndex++)
                   if(words.Equals(ignoredSplit))
                   {
                       return null;
                   }
                   else
                   {
                       /***words.AsString.ToUpper();***/
                   }
                  finalArray.Add(inputs[currenti]);
           }
           currenti++;
       }
           //return sb.ToString();
           for(int finalIndex = 0; finalIndex<finalArray.Count; finalIndex++)
           {
                /*
                 * I need help on how to organize the output based on the capitalized words and
                 * align them to the center.  Please give me some functions and procedures.
                 * */

            }
           return finalArray.ToString();
        }
}
Try this. I did not test it, but I get a clean build.

Jim

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.
 
   public class IgnoreProgram
   {
      static void Main()
      {
         string textPath = "Lines.txt";
         string ignoredPath = "IgnoredWords.txt";
 
         GetTextLines(textPath);
         GetIgnoredWords(ignoredPath);
         string output = BuildOutput(ignoredPath, textPath);
 
         // *** Now write the output to the screen.
      }
 
      static void GetTextLines(string path)
      {
         StreamWriter sw = new StreamWriter(path);
         string fileLine;
         Console.Write("Please enter the text to be examined ");
         fileLine = Console.ReadLine();
         while (fileLine.Length > 0)
         {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
         }
         sw.Flush();
         sw.Close();
      }
      static void GetIgnoredWords(string path)
      {
         StreamWriter sw = new StreamWriter(path);
         Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
 
         string wordList = Console.ReadLine();
         string[] words = wordList.Split(' ');
         for (int i = 0; i < words.Length; i++)
         {
            sw.WriteLine(words[i]);
         }
         sw.Flush();
         sw.Close();
      }
 
      static string BuildOutput(string ignorePath, string inputPath)
      {
         StringBuilder sb = new StringBuilder(1024);
         StreamReader sr = new StreamReader(ignorePath);
 
         List<string> ignoredWords = new List<string>();
         string text;
         while ((text = sr.ReadLine()) != null)
         {
            ignoredWords.Add(text);
         }
 
         StreamReader sr2 = new StreamReader(inputPath);
         List<string> inputs = new List<string>();
 
         while ((text = sr2.ReadLine()) != null)
         {
            inputs.Add(text);
         }
         //kaahinm@cs.umb.edu
 
         // Get the index words from the set of inputs
         List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
         List<string> currentLines = null;
         List<int> offsets = null;
         int maxOffset;
         string pad = string.Empty;
 
         foreach (string indexWord in indexWords)
         {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
               if (offsets[i] > maxOffset)
                  maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
               pad = new string(' ', maxOffset - offsets[j]);
               sb.Append(pad);
               if (offsets[j] > 0)
               {
                  sb.Append(currentLines[j].Substring(0, offsets[j]));
               }
               sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
               if (currentLines[j].Length > (offsets[j] + indexWord.Length))
               {
                  sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
               }
            }
         }
         return sb.ToString();
      }
      private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
      {
         List<string> output = new List<string>(10);
         string lowIndex = indexWord.ToLower();
         int pos = 0;
 
         foreach (string line in inputLines)
         {
            pos = (line.ToLower()).IndexOf(lowIndex);
            if (pos > -1)
            {
               output.Add(line);
               offsets.Add(pos);
            }
         }
         return output;
      }
      private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
      {
         List<string> indexWords = new List<string>(64);
         string[] allCurrent = null;
         string current;
         int currentIndex = 0;
         int inputIndex = 0;
         int ignoredIndex = 0;
         string actualCurrent = string.Empty;
 
         while (inputIndex < inputLines.Count)
         {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
               current = allCurrent[currentIndex++];
               actualCurrent = current;
               if (current.EndsWith(","))
                  current = current.Substring(0, current.Length - 1);
               ignoredIndex = 0;
               while (ignoredIndex < ignoredWords.Count)
               {
                  if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                     break;
                  ignoredIndex++;
               }
               if (ignoredIndex < ignoredWords.Count) // This one gets caps
                  indexWords.Add(actualCurrent);
            }
         }
         return indexWords;
      }
   }

Open in new window

Avatar of asif07

ASKER

Jim, when I run the program, it is prompting for the input and ignored words; however when i type it in, there is an error stating: System.NullReferenceException  Object Reference not set to an instance of an object.

at IgnoreKwic.GetAllLinesForIndexWords<with parameters>
at IgnoreKwic.BuildOutput<with parameters>
at IgnoreKwic.Main<>

For writing the output to the screen, do I need to write line at a time or should i just send the whole string to the screen?
Open it up in the debugger, tell it to break on all exceptions, and it will allow you to see where the problem is.

Jim
Avatar of asif07

ASKER

The problem is in line 115 offsets.Add(pos);  object reference not set to an instance of an object. Is it because offsets is set to null?
Good catch. Make that:
List<int> offsets = new List<int>(10);

Jim
Avatar of asif07

ASKER

Thanks Jim, the code is executing.  When I wrote console.write(output).  It is returning the words in the ignore.txt as capitalized in a single line.  I have to capitalize all the words that are not in the ignored file.

This is what the program is suppose to do in words.

If the input file contains:
hello how are you
this program is suppose to
capitalize the keywords.
 
and the ignore contains:
the are is

then the output is suppose to be like this:

first the output should go through the input line at a time, for each line, go through each word and if a word is not in the ignored file, caps it and save it.  then the program should read the line again and find the next word that is not in the ignored file and caps it and save it.

at the end, the program is suppose to align the saved array in alphabetical order

something like this:

                                      CAPITALIZE the keywords          
                                       HELLO how are you
                               hello HOW are you
                  capitalize the KEYWORDS
                                this PROGRAM is suppose to
              this program is SUPPOSE to
                                       THIS program is suppose to
this program is suppose TO
                 hello how are YOU

Please tell me what changes I should make.



Please post your current code.

Jim
Avatar of asif07

ASKER

Jim, here is the current code:

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);

        // *** Now write the output to the screen.

       
        Console.WriteLine(output);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);

        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }

        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();

        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        //kaahinm@cs.umb.edu

        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = new List<int>(10);
        int maxOffset;
        string pad = string.Empty;

        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
            }
        }
        return sb.ToString();
    }
    private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        string lowIndex = indexWord.ToLower();
        int pos = 0;

        foreach (string line in inputLines)
        {
            pos = (line.ToLower()).IndexOf(lowIndex);
            if (pos > -1)
            {
                output.Add(line);
                offsets.Add(pos);
            }
        }
        return output;
    }
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;

        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current;
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex < ignoredWords.Count) // This one gets caps
                    indexWords.Add(actualCurrent);
            }
        }
        return indexWords;
    }
}
I fixed a few problems in the code below. Output is now:
                 BEHIND the grandstand
Not a child left BEHIND
Behind the GRANDSTAND
          NOT a child left behind
Yes, yes, NOT
Not a CHILD left behind
Not a child LEFT behind
YES, yes, not

There are some differences between what it is putting out and what you want. So there are two problems for you to tackle.

1. You need to deal with finding an index word in an input line more than once. Notice that Yes was only located once.
2. if you want all the capitalized words to line up in the output, you need to scan all the inputs for all the index words, and find the max offset, then use that value to determine pad length.

Jim



         // Get the index words from the set of inputs
         List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
         List<string> currentLines = null;
         List<int> offsets = null;
         int maxOffset;
         string pad = string.Empty;
 
         foreach (string indexWord in indexWords)
         {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
               if (offsets[i] > maxOffset)
                  maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
               pad = new string(' ', maxOffset - offsets[j]);
               sb.Append(pad);
               if (offsets[j] > 0)
               {
                  sb.Append(currentLines[j].Substring(0, offsets[j]));
               }
               sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
               if (currentLines[j].Length > (offsets[j] + indexWord.Length))
               {
                  sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
               }
               sb.Append("\n");
            }
         }
         return sb.ToString();
      }
      private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
      {
         List<string> output = new List<string>(10);
         offsets = new List<int>(10);
         string lowIndex = indexWord.ToLower();
         int pos = 0;
 
         foreach (string line in inputLines)
         {
            pos = (line.ToLower()).IndexOf(lowIndex);
            if (pos > -1)
            {
               output.Add(line);
               offsets.Add(pos);
            }
         }
         return output;
      }
      private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
      {
         List<string> indexWords = new List<string>(64);
         string[] allCurrent = null;
         string current;
         int currentIndex = 0;
         int inputIndex = 0;
         int ignoredIndex = 0;
         string actualCurrent = string.Empty;
 
         while (inputIndex < inputLines.Count)
         {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
               current = allCurrent[currentIndex++];
               actualCurrent = current.ToLower();
               if (current.EndsWith(","))
                  current = current.Substring(0, current.Length - 1);
               ignoredIndex = 0;
               while (ignoredIndex < ignoredWords.Count)
               {
                  if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                     break;
                  ignoredIndex++;
               }
               if (ignoredIndex == ignoredWords.Count) // This one gets caps
               {
                  if (!indexWords.Contains(actualCurrent))
                     indexWords.Add(actualCurrent);
               }
            }
         }
         return indexWords;
      }

Open in new window

Hint for the first problem:
Create a new method similar to GetAllLinesForIndexWord(), say:
private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
{
   // loop over all index words
      // loop over all input lines
   return max offset;
}

For the second problem, modify GetAllLinesForIndexWord().
Look at the options for string.IndexOf(), specifically,
String.IndexOf Method (Char, Int32)

Jim
Avatar of asif07

ASKER

Jim, the maxoffset is suppose to return the occurrence of a word, right?

I tried to do the GetMaxOffset method.

Here is what I have come up with:

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);

        // *** Now write the output to the screen.


        Console.WriteLine(output);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);

        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }

        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();

        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        //kaahinm@cs.umb.edu

        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset;
        string pad = string.Empty;

        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
    private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int pos = 0;

        foreach (string line in inputLines)
        {
            pos = (line.ToLower()).IndexOf(lowIndex);
            if (pos > -1)
            {
                output.Add(line);
                offsets.Add(pos);
            }
        }
        return output;
    }

    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
       
       
        int maxOffset = 0;
        int totalLines = inputLines.Count;
        int track = 0;
        // loop over all index words
        // loop over all input lines
        foreach (string wordIndex in indexWords)
        {
           while(track < totalLines)
               if(wordIndex.Equals(inputLines[totalLines]))
               {
                   maxOffset++;
                   track++;
               }
        }
        return maxOffset;
    }
   
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;

        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                }
            }
        }
        return indexWords;
    }
}

Hope I did I right!!!
Avatar of asif07

ASKER

Here is what I have done for the second question:

  private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;

        foreach (string line in inputLines)
        {
            while (start < line.Length)
            {
                pos = (line.ToLower()).IndexOf(lowIndex, start);
           
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
            }
        }
        return output;
    }
This block is returning a count, not the offset into the string. Also, if the index word is Hello, and the line conatins hello or HELLO, do you think it will find either one?

 GetAllLinesForIndexWord() looks good.

Jim

    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        
        
        int maxOffset = 0;
        int totalLines = inputLines.Count;
        int track = 0;
        // loop over all index words
        // loop over all input lines
        foreach (string wordIndex in indexWords)
        {
           while(track < totalLines)
               if(wordIndex.Equals(inputLines[totalLines]))
               {
                   maxOffset++;
                   track++;
               }
        }
        return maxOffset;
    }

Open in new window

Avatar of asif07

ASKER

Jim, what is the point of GetMaxOffset method and where do I use it?
Avatar of asif07

ASKER

Also, when I run this program, it is freezing after I enter the words to be ignored.

Here is the code:

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);

        // *** Now write the output to the screen.


        Console.WriteLine(output);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);

        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }

        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();

        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        //kaahinm@cs.umb.edu

        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset;
        string pad = string.Empty;

        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
    private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;

        foreach (string line in inputLines)
        {
            while (start < line.Length)
            {
                pos = (line.ToLower()).IndexOf(lowIndex, start);
           
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
            }
        }
        return output;
    }

    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
       
       
        int maxOffset = 0;
        int totalLines = inputLines.Count;
        int track = 0;
        // loop over all index words
        // loop over all input lines
        foreach (string wordIndex in indexWords)
        {
           while(track < totalLines)
               if(wordIndex.Equals(inputLines[totalLines]))
               {
                   maxOffset++;
                   track++;
               }
        }
        return maxOffset;
    }
   
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;

        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                }
            }
        }
        return indexWords;
    }
}
The current output lines up sets that contain the same word, but not all capitalized words. You need to find the largest offset to use when createing the padding string.

When a program seems to "freeze", that's usually a good sign that you are in an infinite loop somewhere. What happens in this loop if there's no match?

          while(track < totalLines)
               if(wordIndex.Equals(inputLines[totalLines]))
               {
                   maxOffset++;
                   track++;
               }
 
If you run from the debugger, that's easy to spot when it happens.

Jim


Avatar of asif07

ASKER

Jim, the debugger is not responding!!!
I have changed the GetMaxOffset.  If I am doing it wrong, please tell me where i am messing up.

I stil don't get where to use the maxoffset?

Is the rest of the program fine?



Here is my code:

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);

        // *** Now write the output to the screen.


        Console.WriteLine(output);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);

        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }

        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();

        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        //kaahinm@cs.umb.edu

        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset;
        string pad = string.Empty;

        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
    private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;

        foreach (string line in inputLines)
        {
            while (start < line.Length)
            {
                pos = (line.ToLower()).IndexOf(lowIndex, start);
           
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
            }
        }
        return output;
    }

    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        int maxOffset = 0;

        int pos = 0;

        // loop over all index words
        // loop over all input lines
        foreach (string line in inputLines)
        {
            while (maxOffset < line.Length)
            {
                foreach (string stringIndex in indexWords)
                {
                    pos = (line.ToLower()).IndexOf(stringIndex, maxOffset);
                    if (pos > -1)
                    {
                        maxOffset = pos + stringIndex.Length;
                    }
                 
                }
               
            }
        }
        return maxOffset;
    }
   
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;

        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                }
            }
        }
        return indexWords;
    }
}

p.s. how long are you going to be available?  I really want to finish the program today.  I still have to change the program so that it receives files rather than getting input from the console.
You need to get the max offset before this block:
        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
 
Then use it to determine the pad length. Do not change maxOffset in this code.

This:
    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        int maxOffset = 0;

        int pos = 0;

        // loop over all index words
        // loop over all input lines
        foreach (string line in inputLines)
        {
            while (maxOffset < line.Length)
            {
                foreach (string stringIndex in indexWords)
                {
                    pos = (line.ToLower()).IndexOf(stringIndex, maxOffset);
                    if (pos > -1)
                    {
                        maxOffset = pos + stringIndex.Length;
                    }
                 
                }
               
            }
        }
        return maxOffset;
    }

Will throw an exception when you try to index past the end of a line. The loop structure is right, but what you are doing inside the inner loop is wrong. Look at how GetAllLinesForIndexWord handles the offset.

I will be up working on my own code until I get tired and hit the sack.

Jim

Avatar of asif07

ASKER

Jim, I have tried to fix the problem in GetMaxOffset; however, my debugger is still not working.  I am sorry for disturbing you frequently.  
The program is still freezing and I cannot debug it.  Please assist me in this problem.  Today is my final deadline.  I won't be bothering you for a while after this.

here is my code:
using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);

        // *** Now write the output to the screen.


        Console.WriteLine(output);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);

        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }

        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();

        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        //kaahinm@cs.umb.edu

        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset;
        string pad = string.Empty;

        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
    private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;

        foreach (string line in inputLines)
        {
            while (start < line.Length)
            {
                pos = (line.ToLower()).IndexOf(lowIndex, start);
           
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
            }
        }
        return output;
    }

    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        int maxOffset = 0;
        int start = 0;

        int pos = 0;

        // loop over all index words
        // loop over all input lines
        foreach (string line in inputLines)
        {
            while (start < line.Length)
            {
                foreach (string stringIndex in indexWords)
                {
                    pos = (line.ToLower()).IndexOf(stringIndex, start);
                    if (pos > -1 && pos <= maxOffset)
                    {

                        maxOffset = pos;
                        start = pos + stringIndex.Length;
                    }
                    else
                    {
                        maxOffset = maxOffset + 0;
                    }
                 
                }
               
            }
        }
        return maxOffset;
    }
   
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;

        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                }
            }
        }
        return indexWords;
    }
}
Try it like this.

    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        int maxOffset = 0;
        int start = 0;

        int pos = 0;

        // loop over all index words
        // loop over all input lines
        foreach (string line in inputLines)
        {
            foreach (string stringIndex in indexWords)
            {
                while (pos > -1)
                {
                    pos = (line.ToLower()).IndexOf(stringIndex, start);
                    if (pos > -1 && pos <= maxOffset)
                    {

                        maxOffset = pos;
                    }
                    start = pos + 1;
                }
               
            }
        }
        return maxOffset;
    }

Jim
Avatar of asif07

ASKER

Jim, the program is still freezing!!! ever since i added this method.  Is there an exception in this method, or is the problem lying somewhere else?

 private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;

        foreach (string line in inputLines)
        {
            while (start < line.Length)
            {
                pos = (line.ToLower()).IndexOf(lowIndex, start);

                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
         
                   
            }
        }
        return output;
    }
Avatar of asif07

ASKER

there is still no output displayed on the screen
Avatar of asif07

ASKER

Jim, I have to submit the assignment  in a few hours.  I have been waiting for your reply, but I guess you have hit the sack.  I am going to post a question on the forum asking for the problem.  If you, by any chance, get to this forum, please reply to my questions.  I am very thankful to your help and if I had another day, I would have waited for your reply.
Avatar of asif07

ASKER

Jim, I desperately need your help!!!
Avatar of asif07

ASKER

Jim, I am messing up somewhere in the code.  I am getting wrong results.

when i enter for the first file
"Now is the time
 for all good men"

and for the second file "now all"

it should return:  

                 FOR all good men
       for all GOOD men
         now IS the time
for all good MEN
      now is THE time
now is the TIME


Instead, it is returning:
now IS the time
now is THE time
now is the TIME
FOR all good men
for all GOOD men
for all good MEN
 
it is still not aligning.  Please assist me.  I have few hours left!!!

here is my code:
using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.
 
public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";
 
        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);
 
        // *** Now write the output to the screen.
 
 
        Console.WriteLine(output);
    }
 
    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
 
        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }
 
    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);
 
        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }
 
        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();
 
        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        
 
        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset= GetMaxOffset(inputs, ignoredWords);
        string pad = string.Empty;
 
        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
   private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;
 
        foreach (string line in inputLines)
        {
            while(start < line.Length)
            {
           
                pos = (line.ToLower()).IndexOf(lowIndex, start);
 
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
                else
                {
                    break;
                }
        
            }
            
        }
        return output;
    }
   
 
    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        int maxOffset = 0;
        int start = 0;
 
        int pos = 0;
 
        // loop over all index words
        // loop over all input lines
        foreach (string line in inputLines)
        {
            foreach (string stringIndex in indexWords)
            {
                while (pos > -1)
                {
                    pos = (line.ToLower()).IndexOf(stringIndex, start);
                    if (pos > -1 && pos <= maxOffset)
                    {
 
                        maxOffset = pos;
                    }
                    start = pos + 1;
                }
 
            }
        }
        return maxOffset;
    }
     
    
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;
 
        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                }
            }
        }
        return indexWords;
    }
}

Open in new window

MaxOffset is now generated by your new routine, so you need to delete this block:
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }

Jim
Avatar of asif07

ASKER

Jim, after I delete this block, I am getting an argument out of range exception error that count must be non-negative.<char, int32 count>.  Is there an error in the GetMaxOffset?
In this block:
        foreach (string indexWord in indexWords)
        {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();

Delete
maxOffset = -1;

Jim
You need to corner one of your classmates or a TA and get them to show you how to use the debugger. Once you have the algorithm developed, it should not take more that two or three hours to get something like this running. You can step through code in the debugger and see immediately where something went wrong when there is a problem.

Jim
Avatar of asif07

ASKER

Jim, it is getting the offset for the second line, but not for the first line.  the program is also not sorting by alphabets.

if i enter:
now is the time
for all good men

now good

it returns:
now IS the time
now is THE time
now is the TIME
                  FOR all good men
            for ALL good men
for all good MEN


do we need another loop?
Please post your current code.

Jim
Avatar of asif07

ASKER

Jim, here is my current code:

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.
 
public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";
 
        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);
 
        // *** Now write the output to the screen.
 
 
        Console.WriteLine(output);
    }
 
    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
 
        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }
 
    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);
 
        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }
 
        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();
 
        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
        
 
        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset= GetMaxOffset(inputs, ignoredWords);
        string pad = string.Empty;
 
        foreach (string indexWord in indexWords)
        {
           
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
                if (offsets[i] > maxOffset)
                    maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
   private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;
 
        foreach (string line in inputLines)
        {
            while(start < line.Length)
            {
           
                pos = (line.ToLower()).IndexOf(lowIndex, start);
 
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
                else
                {
                    break;
                }
        
            }
            
        }
        return output;
    }
   
 
    private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
    {
        int maxOffset = 0;
        int start = 0;
 
        int pos = 0;
 
        // loop over all index words
        // loop over all input lines
        foreach (string line in inputLines)
        {
            foreach (string stringIndex in indexWords)
            {
                while (pos > -1)
                {
                    pos = (line.ToLower()).IndexOf(stringIndex, start);
                    if (pos > -1 && pos <= maxOffset)
                    {
 
                        maxOffset = pos;
                    }
                    start = pos + 1;
                }
 
            }
        }
        return maxOffset;
    }
     
    
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;
 
        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                }
            }
        }
        return indexWords;
    }
}

Open in new window

When I run this code with your original inputs, I get correct output.
                 BEHIND the grandstand
Not a child left BEHIND      
      Behind the GRANDSTAND
                 NOT a child left behind      
       Yes, yes, NOT          
           Not a CHILD left behind    
     Not a child LEFT behind                
                 YES, yes, not            
            Yes, YES, not

Note - this is not a mono-spaced font. Copy and paste to notpad.

Jim
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asif07

ASKER

Jim, the program is suppose to take any size of inputs and words; however, when i enter any other inputs or words, it is not functioning properly.

like, if i enter Kwic(name of the program) and two names of text files, it should operate on the two text fiels and display the results.

Do we have to change the whole layout for it to work properly?

You just have to accept the files as input from the command line instead of building the files. Then pass them to process the same way you do now.

Jim
Avatar of asif07

ASKER

Jim, one more question:  The output array has to be ordered in alphabetical order, Like:
                       BEHIND the grandstand
Not a child left BEHIND
              Not a CHILD left behind

How do I do that?
That was not stated in your requirements. You need to sort the list of index words after you build it and before you use it. Do that in FindAllWordsForCaps after you complete the list, and before you return it.

I will be in meetings the rest of the day, but will be back on-line tonight.

Jim
Avatar of asif07

ASKER

i did indexWords.SOrt() and it works!!!
Thanks a lot Jim!!!
Glad that worked.

Jim
Avatar of asif07

ASKER

Jim, I have already passed in the assignment.  I had a question for me to learn.  
How would the code look different if I was to compare text files that were already saved in my directory, rather then getting input from the user and creating a file from that.

This is for my learning purpose, so take your time, there is no deadline for me!
Avatar of asif07

ASKER

Just in case you need the code, here it is:


using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.
 
public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";
 
        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);
 
        // *** Now write the output to the screen.
 
 
        Console.WriteLine(output);
    }
 
    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");
 
        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }
 
    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);
 
        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }
 
        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();
 
        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }
 
 
        // Get the index words from the set of inputs
        List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
        List<string> currentLines = null;
        List<int> offsets = null;
        int maxOffset = GetMaxOffset(inputs, indexWords);
        string pad = string.Empty;
 
        foreach (string indexWord in indexWords)
        {
 
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            //for (int i = 0; i < offsets.Count; i++)
            //{
            //    if (offsets[i] > maxOffset)
            //        maxOffset = offsets[i];
            //}
            for (int j = 0; j < currentLines.Count; j++)
            {
                pad = new string(' ', maxOffset - offsets[j]);
                sb.Append(pad);
                if (offsets[j] > 0)
                {
                    sb.Append(currentLines[j].Substring(0, offsets[j]));
                }
                sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
                if (currentLines[j].Length > (offsets[j] + indexWord.Length))
                {
                    sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
                }
                sb.Append("\n");
            }
        }
        return sb.ToString();
    }
   private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
    {
        List<string> output = new List<string>(10);
        offsets = new List<int>(10);
        string lowIndex = indexWord.ToLower();
        int start = 0;
        int pos = 0;
 
        foreach (string line in inputLines)
        {
            while(start < line.Length)
            {
           
                pos = (line.ToLower()).IndexOf(lowIndex, start);
 
                if (pos > -1)
                {
                    output.Add(line);
                    offsets.Add(pos);
                    start = pos + lowIndex.Length;
                }
                else
                {
                    break;
                }
        
            }
            
        }
        return output;
    }
 
 
   private static int GetMaxOffset(List<string> inputLines, List<string> indexWords)
   {
       int maxOffset = 0;
       int start = 0;
       int pos = 0;
 
       // loop over all index words
       // loop over all input lines
       foreach (string line in inputLines)
       {
           foreach (string stringIndex in indexWords)
           {
               pos = 0;
               while (pos > -1)
               {
                   pos = (line.ToLower()).IndexOf(stringIndex, start);
                   if (pos > -1 && pos > maxOffset)
                   {
                       maxOffset = pos;
                   }
                   start = pos + 1;
               }
 
           }
       }
       return maxOffset;
   }
     
    
    private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
    {
        List<string> indexWords = new List<string>(64);
        string[] allCurrent = null;
        string current;
        int currentIndex = 0;
        int inputIndex = 0;
        int ignoredIndex = 0;
        string actualCurrent = string.Empty;
 
        while (inputIndex < inputLines.Count)
        {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
                current = allCurrent[currentIndex++];
                actualCurrent = current.ToLower();
                if (current.EndsWith(","))
                    current = current.Substring(0, current.Length - 1);
                ignoredIndex = 0;
                while (ignoredIndex < ignoredWords.Count)
                {
                    if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                        break;
                    ignoredIndex++;
                }
                if (ignoredIndex == ignoredWords.Count) // This one gets caps
                {
                    if (!indexWords.Contains(actualCurrent))
                        indexWords.Add(actualCurrent);
                        
                }
            }
        }
        indexWords.Sort();
        return indexWords;
    }
}

Open in new window

Comment out these two lines:
        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);

And instead of this:
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

pass the filenames in on the command line. You can read them as shown here:
http://msdn2.microsoft.com/en-us/library/acy3edy3(VS.80).aspx

Jim
Avatar of asif07

ASKER

Jim. I have a new exercise and I need some pointers.


For this exercise, I have to do screen scraping using regular expressions.
Here is the exercise.  

By now you may have noticed that different news organizations, and so different web sites have different counts for the various delegates committed to the various candidates, both on the Democratic and Republican side.  You are to implement a web application that goes out to at least five of these sites to gleen (actually, scrape) the various counts off of each site and present the results on a single web page so that they may be easily compared.  Your own site should have some mechanism for deciding what and how much information is to be displayed: a particular candidate, perhaps two candidates, all of the Democrats, all of the Republicans, etc.  Use your imagination

Please give me some sites for tutorials and some procedure for doing the exercise.

Thanks.
asif07 - I am tied up for the forseeable future, and have not had time to answer questions for some time now.  I recommend that you accept an answer for this one and post a new question. No other expert will look at a question with this many posts in one thread.

Jim