/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package documents;
import java.io.*;
import java.util.Scanner;

/**
 *
 * @author kvaradar
 */
public class MyDocument {
    
    String fileName; // name of file that the document is made from
    int defaultLength = 10000; // presumed upper bound on number of words 
    MyWord [] arr; // array to hold words in non-increasing order of frequency
    int numWords = 0; // actual number of words in document
    
    public MyDocument (String inputFileName) {
        fileName = inputFileName;
        arr = new MyWord[defaultLength];
        populateArray();
    }
    
    private void populateArray() {
        try {
            Scanner s = new Scanner(new File(fileName));
            scanWords(s);
        }
        catch (IOException e) {
            System.out.println("There may have been a problem opening " + fileName);
        }
    }

    private void scanWords(Scanner s)  {
        String next;
        int i,j;
        MyWord temp;
        while (s.hasNext()) {
            next = s.next();
            for (i = 0; i < numWords; i++) {
                if (arr[i].word.equals(next))
                    break;
            }
            if (i < numWords) {  // next was found
                arr[i].frequency = arr[i].frequency + 1;
                // logic to correctly sort the array follows
                j = i;
                while (j >= 1) {
                    if (arr[j-1].frequency >= arr[j].frequency)
                        break;
                    temp = arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                    j--;
                }
                }
            else { // i = numWords; next was not found
                arr[numWords] = new MyWord(next); //possibe array overflow here
                numWords++;
                }

        }
    }

    public String mostFrequent() {
        return arr[0].word; // we're assuming arr[0] has a valid MyWord entry
    }

    public String [] kMostFrequent(int k) {
        // again, we'll assume that the document has at least one word
        if (k > numWords) k = numWords;
        String [] sArray = new String [k];
        for (int i = 0; i < k; i++)
            sArray[i]=arr[i].word;
        return sArray;

    }

    public void printWords() { //Just for checking whether it works
        for (int i = 0; i < numWords; i++) {
            System.out.println(arr[i].word + " : " + arr[i].frequency);
        }
    }

}
