Search code examples
c#porter-stemmer

how to access and write each word in string array read from a file onto a new file in c#?


My testerfile contains:

processes
deleting 
agreed

And this the code in C#

PorterStemmer testing = new PorterStemmer();
string temp,stemmed;
string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\PJM\\Documents\\project\\testerfile.txt");
System.Console.WriteLine("Contents of testerfile.txt = ");
for (int i = 0; i <2; i++)
   {
      temp = lines[i];
      stemmed = testing.StemWord(temp);
System.IO.File.WriteAllText(@"C:\\Users\\PJM\\Documents\\project\\testerfile3.txt", stemmed);
       Console.WriteLine("\t" + stemmed);
   }

After running the code, the testerfile3 contains only "agre" . So my problem here is that I want each word in the string array to be processed seperately i.e. I am having problem accessing string array. Is there any way to access every index in the string array?


Solution

  • From the documentation of WriteAllText:

    If the target file already exists, it is overwritten.

    so each iteration in your for loop overwrites the file, and you're only left with the text from the last iteration.

    you can use System.IO.File.AppendAllText instead

    also, you can use the array's Length property to loop through all words for (int i = 0; i < lines.Length; i++)

    Alternatively, instead of the for-loop you can use LINQ's Select to project the non-stemmed line to the stemmed one and use AppendAllLines to write the results:

    System.IO.File.AppendAllLines(@"C:\\Users\\PJM\\Documents\\project\\testerfile3.txt", lines.Select(l => testing.StemWord(l)));