Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 4 - User interface controller class (MatcherController) Task 1: https://www

ID: 3845021 • Letter: T

Question

Task 4 - User interface controller class (MatcherController)

Task 1: https://www.chegg.com/homework-help/questions-and-answers/task-1-analysing-n-grams-sample-text-ngramanalyser-task-need-complete-ngramanalyser-class--q22251885

Task 2:https://www.chegg.com/homework-help/questions-and-answers/task-2-building-markov-model-text-sample-markovmodel-task-need-complete-markovmodel-class--q22255515

Task 3:https://www.chegg.com/homework-help/questions-and-answers/task-3-matching-test-strings-model-modelmatcher-link-task-1-https-wwwcheggcom-homework-hel-q22271495

Your final required task is to complete a class which can be used to create and modify models and measure how well a test string matches them, as part of a user application. The class will represent the “Controller” portion of an application contructed using the Model View Controller (MVC) software architectural pattern. A controller class in this pattern understands how to create and manipulate a modelof some sort (a database of students, say), and generate output from the model which can be displayed in a view class that displays the model in some way. The model might be displayed using text output to a terminal, or graphs displayed on a graphical user interface (GUI), or in many other ways. The controller can be seen as acting as an intermediary between the a user and the model: it accepts “requests” from the user, updates the model in some way based on those requests, and uses the model to generate output which can be displayed to the user in a convenient form. In this case, the “model” consists of a ModelMatcher object, and any other objects needed to accurately represent the state of the application.

Note that it is generally considered poor design to allow exceptions to reach the user – an end-user of your application is unlikely to know how to deal with a message written to output saying “File input error”, with a stack-trace (assuming there even is an “output” for stack traces to be written to; on some platforms, such as Android mobile phones, there may not be). You should try to forestall possible error conditions by catching and handling the errors thrown by, for example, the FileIO class.

You will need to write code in the MatcherController class that can create and manipulate an ArrayList collection of ModelMatcher objects and display their results for convenient viewing.

You will need to decide what fields the MatcherController class will require (although at a minimum, it will need a ModelMatcher field, which we have already included). You will also need to decide what helper methods (if any) are needed, and what code will need to go in the constructor for the class.

Fill in the @author and @version fields in the header comments with all authors’ names, student numbers and the date.

Complete the constructor and each of the stub methods in the MatcherController class, implementing the functionality described in their Javadoc comments.

Ensure you complete Javadoc comments for each method and class including @param, @returns and @throws fields as required.

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.io.*;

/** Create and manipulate Markov models and model matchers for lists of training data
* a test data String and generate output from it for convenient display.
*
* @author (your name)
* @version (a version number or a date)
*
*/
public class MatcherController {

/** list of training data string used to generate markov models */
ArrayList<String> trainingDataList;
/** test data to be matched with the models */
String testData;
/** order of the markov models*/
int k;
/** generated list of markov models for the given training data*/
ArrayList<MarkovModel> modelList;
/** generated list of matchers for the given markov models and test data*/
ArrayList<ModelMatcher> matcherList;

/** Generate models for analysis
* @param k order of the markov models to be used
* @param testData String to check against different models
* @throw unchecked exceptions if the input order or data inputs are invalid
*/
public MatcherController(int k, ArrayList<String> trainingDataList, String testData)
{
//TODO
}

/** @return a string containing all lines from a file
* ff file contents can be got, otherwise null
* This method should process any exceptions that arise.
*/
private static String getFileContents(String filename) {
//TODO
return null;
}

/**
* @return the ModelMatcher object that has the highest average loglikelihood
* (where all candidates are trained for the same test string
*/
public ModelMatcher getBestMatch(ArrayList<ModelMatcher> candidates)
{
//TODO
return null;
}

/** @return String an *explanation* of
* why the test string is the match from the candidate models
*/
public String explainBestMatch(ModelMatcher best) {
//TODO
return null;
}

/** Display an error to the user in a manner appropriate
* for the interface being used.
*
* @param message
*/
public void displayError(String message) {
// LEAVE THIS METHOD EMPTY
}

}

Explanation / Answer

Considering the length of the project its quit tedious taks. I hope this might help. Let me know if you need any further help in this.
//MatcherController.java

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.io.*;

/**
* Create and manipulate Markov models and model matchers for lists of training data
* a test data String and generate output from it for convenient display.
*/
public class MatcherController {

    /** list of training data string used to generate markov models */
    ArrayList<String> trainingDataList;
    /** test data to be matched with the models */
    String testData;
    /** order of the markov models */
    int k;
    /** generated list of markov models for the given training data */
    ArrayList<MarkovModel> modelList;
    /** generated list of matchers for the given markov models and test data */
    ArrayList<ModelMatcher> matcherList;
    /** best ModelMatcher for the given testData string */
    ModelMatcher bestModelMatcher;

    /**
     * Generate models for analysis
     * Initialize class fields
     * @param k order of the markov models to be used
     * @param testData String to check against different models
     * @throw unchecked exceptions if the input order or data inputs are invalid
     */
    public MatcherController(int k, ArrayList<String> trainingDataList, String testData) {

        this.checkInputs(k, trainingDataList, testData);

        this.k = k;
        this.testData = testData;
        this.trainingDataList = trainingDataList;
        this.modelList = new ArrayList<MarkovModel>();
        this.matcherList = new ArrayList<ModelMatcher>();

        ModelMatcher tempMatcher;
        MarkovModel tempModel;

        for (String trainingString : this.trainingDataList) {
            tempModel = new MarkovModel(k, trainingString);
            tempMatcher = new ModelMatcher(tempModel, testData);

            this.modelList.add(tempModel);
            this.matcherList.add(tempMatcher);
        }

        this.bestModelMatcher = this.getBestMatch(this.matcherList);

    }

    /**
     * @return a string containing all lines from a file
     * if file contents can be got, otherwise null
     * This method should process any exceptions that arise.
     */
    private static String getFileContents(String filename) {

        try {

            String outputString = "";
            ArrayList<String> fileLines = FileIO.readFile(filename);

            for (String fileLine : fileLines) {
                outputString = outputString + fileLine;
            }

            return outputString;

        } catch(FileNotFoundException e) {
            //TODO
        } catch(IOException e) {
            //TODO
        }

        return null;
    }

    /**
     * @return the ModelMatcher object that has the highest average loglikelihood
     * (where all candidates are trained for the same test string)
     */
    public ModelMatcher getBestMatch(ArrayList<ModelMatcher> candidates) {

        double bestLikelihood = 0;
        ModelMatcher bestMatcher = candidates.get(0);
        double tempLikelihood;

        for (ModelMatcher matcher : candidates) {
            tempLikelihood = matcher.getAverageLogLikelihood();
            if (bestLikelihood == 0 || tempLikelihood > bestLikelihood) {
                bestLikelihood = tempLikelihood;
                bestMatcher = matcher;
            }
        }

        return bestMatcher;
    }

    /**
     * @return String an *explanation* of
     * why the test string is the match from the candidate models
     * Prints a table of each loglikelihood relative to the lowest loglikelihood
     * Table rows are based on the negative inverse of each loglikelihood
     * to produce a proportional and increasing group of values.
     * Also shows numerical values next to each table with best one labelled
     */
    public String explainBestMatch(ModelMatcher best) {

        ArrayList<Double> modifiedLoglikelihoods = new ArrayList<Double>();
        ArrayList<Double> normalLoglikelihoods = new ArrayList<Double>();
        ArrayList<Long> barNumbers = new ArrayList<Long>();

        Double lowestLikelihood = 0.0;
        Double loglikelihood = 0.0;
        String outputString = "";

        for (ModelMatcher matcher : this.matcherList) {

            loglikelihood = matcher.getAverageLogLikelihood();
            normalLoglikelihoods.add(loglikelihood);

            loglikelihood = -(1 / loglikelihood);
            modifiedLoglikelihoods.add(loglikelihood);
        }

        for (Double scaledLikelihood : modifiedLoglikelihoods) {
            if (lowestLikelihood == 0.0 || scaledLikelihood < lowestLikelihood) {
                lowestLikelihood = scaledLikelihood;
            }
        }

        Double relativeLikelihood;
        Long barNumber;

        for (int i = 0; i < modifiedLoglikelihoods.size(); i++) {
            relativeLikelihood = modifiedLoglikelihoods.get(i);

            barNumber = Math.round(relativeLikelihood / lowestLikelihood);
            barNumbers.add(i, barNumber);
        }

        outputString += "Table of loglikelihoods from each matcher:";
        Double actualLikelihood;

        for (int i = 0; i < barNumbers.size(); i++) {

            barNumber = barNumbers.get(i);
            actualLikelihood = normalLoglikelihoods.get(i);

            outputString += " ";
            outputString += String.format("%.5g%n", actualLikelihood);
            outputString += " |";
            outputString += new String(new char[(int) (long) barNumber]).replace("", "-");
        }

        return null;
    }

    /**
     * Display an error to the user in a manner appropriate
     * for the interface being used.
     *
     * @param message
     */
    public void displayError(String message) {
        // LEAVE THIS METHOD EMPTY
    }

    /**
     * Helper function to display the results of the analysis of the
     * matchers. Does this textually in a simple, formatted manner
     * Designed for use with terminal for experienced programmers
     */
    private void displayResults() {
        String outputString = "";
        outputString += "Displaying results for textual analysis of string";
        outputString += " " + this.testData + ". ";
        outputString += "Comparing test string against ";
        outputString += " " + this.matcherList.size() + " ";
        outputString += "text sources .";

        outputString += "Analysis determines that the string ";
        outputString += "was most likely from the following text source: ";

        int bestIndex = this.matcherList.indexOf(this.bestModelMatcher);
        outputString += this.trainingDataList.get(bestIndex);

    }

    /**
     * Helper function to sanitize data inputs
     * @throws unchecked exceptions:
     * - if k is below or equal to zero
     * - if k is higher than the testData string length
     * - if the trainingData list is null or empty
     * - if the testData string is null or empty
     * - if any of the strings in the testData list are empty or null
     */

    public void checkInputs(int k, ArrayList<String> trainingDataList, String testData) {
        if (k <= 0) {
            throw new IllegalArgumentException
            ("MatcherController: ngram size cannot be below zero");
        } if (k > testData.length()) {
            throw new IllegalArgumentException
            ("MatcherController: ngram size cannot be larger than string length");
        } if (trainingDataList.size() == 0) {
            throw new IllegalArgumentException
            ("MatcherController: list of training strings cannot be empty");
        } if (trainingDataList == null) {
            throw new IllegalArgumentException
            ("MatcherController: list of training strings cannot be uninitialized");
        } if (testData.length() == 0) {
            throw new IllegalArgumentException
            ("MatcherController: string to test cannot be empty");
        } if (testData == null) {
            throw new IllegalArgumentException
            ("MatcherController: string cannot be null");
        }

        for (String trainingString : trainingDataList) {
            if (trainingString.length() == 0 || trainingString == null) {
                throw new IllegalArgumentException
                ("MatcherController: trainingDataList cannot have empty/null entries");
            }
        }
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote