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

/**
 * Class that generates random text with the same statistical distribution 
 * of letters and spaces as a specified text file.
 *   @author Dave Reed
 *   @version 1/5/19
 */
public class Approximation {
    private String cleanText;

    /**
     * Constructs an Approximation object initialized to a specified file. 
     *   @param fileName the file containing the text
     */
    public Approximation(String fileName) throws java.io.FileNotFoundException {
        Scanner infile = new Scanner(new File(fileName));
        
        this.cleanText = "";
        while (infile.hasNextLine()) {
            String nextLine = infile.nextLine().toUpperCase();
            for (int i = 0; i < nextLine.length(); i++) {
                char ch = nextLine.charAt(i);
                if (Character.isLetter(ch) || ch == ' ') {
                    this.cleanText += ch;
                }
            }   
            this.cleanText += " ";
        }
        this.cleanText = this.cleanText.trim().replaceAll("\\s+", " ");
        
        infile.close();
    }

    /**
     * Generates a string of the specified length, matching the statistical 
     * distribution of letters and spaces as the specified file.
     *   @param numChars the length of the generated string
     *   @return the generated string
     */
    public String generate(int numChars) {
        Random randy = new Random();
        
        String text = "";
        for (int i = 0; i < numChars; i++) {
            int index = randy.nextInt(this.cleanText.length());
            text += this.cleanText.charAt(index);
        }
        return text;
    }   
}
