8.38 - Text censorer

Objetive

Create a program to "censor" text files. It must read a text file, and dump its results to a new text file, replacing certain words with "[CENSORED]". The words to censor will be in stored a second data file, a text file which will contain a word in each line.

Solution

using System.IO;
using System;

class TextCensorer
{
    static string[] CreateDictionary(string fileName)
    {
        string[] result;
        StreamReader inputFile;
        string line;
        int numLines=0;
        
        if (File.Exists( fileName ))
        {
            try
            {
                // First: count lines
                inputFile = File.OpenText(fileName);
                do {
                    line = inputFile.ReadLine();
                    if (line != null)
                        numLines++;
                } while(line != null);
                inputFile.Close();
                result = new string[numLines];
                
                // Then: store lines
                inputFile = File.OpenText(fileName);
                int currentLineNumber=0;
                do {
                    line = inputFile.ReadLine();
                    if (line != null)
                    {
                        result[currentLineNumber] = line;
                        currentLineNumber++;
                    }
                } while(line != null);
                inputFile.Close();
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
        return null;
    }
    
    
    static void Main(string[] args)
    {
        StreamReader inputFile;
        StreamWriter outputFile;
        string line;
        string name;

        if (args.Length < 1)
        {
          Console.WriteLine("Not enough parameters!");
          Console.Write("Enter file name: ");
          name = Console.ReadLine();
        }
        else
          name = args[0];

        if (!File.Exists( name ))
        {
          Console.WriteLine( "File not found!");
          return;
        }

        try
        {
            string[] wordsList = CreateDictionary("dictionary.txt");
            inputFile = File.OpenText(name);
            outputFile = File.CreateText(name + ".censored");

            do {
                line = inputFile.ReadLine();
                if (line != null)
                {
                    // For each line, we must replace any censored word
                    foreach(string censoredWord in wordsList)
                    {
                        if (line.Contains(" "+censoredWord+" "))
                            line = line.Replace(
                                " "+censoredWord+" ",
                                " [CENSORED] ");
                                
                        if (line.Contains(" "+censoredWord+"."))
                            line = line.Replace(
                                " "+censoredWord+".",
                                " [CENSORED].");
                                
                        if (line.Contains(" "+censoredWord+","))
                            line = line.Replace(
                                " "+censoredWord+",",
                                " [CENSORED],");
                                
                        if (line.EndsWith(" "+censoredWord))
                        {
                            line = line.Substring(0, 
                                line.LastIndexOf(" "+censoredWord));
                            line += " [CENSORED]";
                        }
                      
                    }
                    // Finally, changes are saved
                    outputFile.WriteLine(line);
                }

            } while(line != null);

            inputFile.Close();
            outputFile.Close();
        }
        catch (PathTooLongException)
        {
            Console.WriteLine("Entered path was too long");
            return;
        }
        catch (IOException ex)
        {
            Console.WriteLine("Input/output error: {0}", ex.Message);
            return;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected error: {0}", ex.Message);
            return;
        }

    }
}


Popular Posts