import java.util.Scanner;
import java.io.File;
import java.util.Random;
import java.util.Set;
import java.util.HashMap;

/**
 * Class that reads in and stores the contents of a text file.  
 *   @author Dave Reed
 *   @version 11/18/15
 */
public class TextProcessor {
    private String text;

    /**
     * Constructs a FileProcessor object that reads in text from a file.  
     * The text is processed to remove non-letters and adjacent spaces, and 
     * letters are converted to lowercase.
     *   @param fileName the file containing the text
     */
    public TextProcessor(String fileName) {
        this.text = "";

        try {
            Scanner infile = new Scanner(new File(fileName));
            while (infile.hasNextLine()) {
                String nextLine = infile.nextLine();
                nextLine = this.strip(nextLine.toLowerCase());
                if (nextLine.length() > 0) {
                    this.text += " " + nextLine;
                }
            }
            this.text = this.text.trim().replaceAll("\\s+", " ");
            infile.close();
        }
        catch (java.io.FileNotFoundException e) {
            System.out.println("No such file: " + fileName);
        }        
    }

    /**
     * Accesses and returns the file contents as a single String.
     * @return the file contents (lowercase, with non-letters and adjacent spaces removed)
     */
    public String toString() {
        return this.text;
    }
    
    ///////////////////////////////////////////////////////////////////////////////

    /**
     * Strips the specified String of all characters except letters and spaces.
     *   @param str the String to be stripped
     *   @return str with all characters except letters and spaces removed
     */
    private String strip(String str) {
        String copy = "";
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (Character.isLetter(ch) || ch == ' ') {
                copy += ch;
            }
        }
        return copy;
    }
    
    ///////////////////////////////////////////////////////////////////////////////

    public static void main(String[] args) {
        TextProcessor gettysburg = new TextProcessor("lincoln.txt");
        
        System.out.println(gettysburg);
    }
}
