Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**The bigram and check method are completed, but having issues with the generate method. The text files couldnt be provided due to the inappropriated words.

image text in transcribed

**The bigram and check method are completed, but having issues with the generate method.

The text files couldnt be provided due to the inappropriated words.

------------------------------ Bigram.java -------------------------------

import java.util.ArrayList;

import java.util.HashSet;

import java.util.TreeMap;

import java.util.Scanner;

import java.util.Set;

public class Bigram {

public Set set = new HashSet();

public TreeMap> gMap = new TreeMap();

public Bigram(String s) {

....

}

public boolean check(String s) {

....

}

public void createMapBigram(String s){

Scanner sc = new Scanner(s);

String previous = sc.next();

while(sc.hasNext()){

String next = sc.next();

if(gMap.containsKey(previous)){

ArrayList arr = new ArrayList();

arr = gMap.get(previous);

arr.add(next);

gMap.remove(previous);

gMap.put(previous, arr);

}else{

ArrayList arr = new ArrayList();

arr.add(next);

gMap.put(previous, arr);

}

previous = next;

}

if(gMap.containsKey(previous)){

ArrayList arr = new ArrayList();

arr = gMap.get(previous);

arr.add("");

gMap.remove(previous);

gMap.put(previous, arr);

}else{

ArrayList arr = new ArrayList();

arr.add("");

gMap.put(previous, arr);

}

}

public String[] generate(String start, int count) {

}

}

------------------------------ BigramTest.java -------------------------------

import java.nio.file.Files; import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays;

public class BigramTest {

public static int test(String file, byte[] xmd5, String[] gen, String[] desired, String[] check, boolean[] truth) throws NoSuchAlgorithmException { System.out.println("Loading " + file + "..."); String text; try { text = new String(Files.readAllBytes(Paths.get(file))); } catch (Exception e) { System.out.println("Couldn't find '" + file + "'. Please place this file in the root directory of this project (next to JRE System Library, not indented)."); return 0; } MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] digest = md5.digest(text.replaceAll("\\s+", " ").getBytes()); //System.out.println(Arrays.toString(digest)); if (!Arrays.equals(digest, xmd5)) { System.out.println("Your copy of " + file + " appears to contain errors! Please download it again."); return 0; } System.out.println("Loaded " + file + ". Initializing Bigram object..."); long start = System.currentTimeMillis(); Bigram u = new Bigram(text); System.out.println("Generating."); int genScore = 0; for (int i = 0; i 8) { System.out.println("Your program is taking a while! Try speeding it up for extra credit."); } else if ((double)(end - start - 5)/(sortime - end) > 2) { System.out.println("Fast, but could be faster! Takes "+(end-start)+" ms, try to get it below ~"+(2*(sortime - end)+5)); genScore += 1; } else { System.out.println("Super fast! Took "+(end - start)+" ms"); genScore += 1; checkScore += 1; } return genScore * 100 + checkScore; }

public static void main(String[] args) throws NoSuchAlgorithmException { final byte[] dmd5 = { -61, 106, 118, -21, 62, -73, 33, 75, 68, -48, 38, 39, 108, 27, 95, -44 }; final byte[] gmd5 = { -59, 120, 53, -92, 81, 59, -34, 72, 56, 2, 112, -125, 127, 50, -42, 55 }; int checkScore = 0, genScore = 0; try { System.out.println("Trying 'Bob' example from homework."); Bigram x = new Bigram("Bob likes dogs. Bill likes cats. Jane hates dogs."); if (x.check("Bob likes cats.")) { checkScore += 10; } else { System.out.println("First check failed."); } if (!x.check("Jane likes cats.")) { checkScore += 10; } else { System.out.println("Second check failed."); } System.out.println("Trying 'Balloon' example from homework."); Bigram y = new Bigram("The balloon was red. The balloon got bigger and bigger. The balloon popped."); String[] g1 = y.generate("The", 3); if (Arrays.equals(g1, new String[] { "The", "balloon", "got" })) { genScore += 10; } else { System.out.println("First generate failed. Got " + Arrays.toString(g1)); } String[] g2 = y.generate("popped.", 2); if (Arrays.equals(g2, new String[] { "popped." })) { genScore += 10; } else { System.out.println("Second generate failed. Got " + Arrays.toString(g2)); }

System.out.println("Testing with the Declaration of Independence..."); int dscores = test("decl.txt", dmd5, new String[] { "When" }, new String[] { "When in the most barbarous ages, and to the most" }, new String[] { "We have Petitioned for the rectitude of this Declaration,", "instrument for pretended offences For abolishing" }, new boolean[] { true, true }); genScore += dscores / 100; checkScore += dscores % 100;

System.out.println("Testing with Great Expectations..."); int gscores = test("gexp.txt", gmd5, new String[] { "Pip", "dozen" }, new String[] { "Pip and I had been a little while, and I", "dozen yards of the same time to be a little" }, new String[] { "low leaden hue" }, new boolean[] { false }); genScore += gscores / 100; checkScore += gscores % 100;

} finally { System.out.println("Check: " + checkScore + " / 50"); System.out.println("Generate: " + genScore + " / 50"); System.out.println("Tentative total: " + (checkScore + genScore + " / 100")); System.out.println("Violations of the academic honesty policy may affect this score."); } }

}

------------------------------ Hint -------------------------------

image text in transcribed

Your phrase generation method will be given a start word and a count indicating the number of total words to generate (including the start word). It will generate the "most likely" or "most common" phrase based on bigram counts. It will return an array of Strings with the words generated in order. It always starts by generating the start word. As you generate each word, the next word generated should be the one that appears most often in the input (constructor) text after the previous word generated. If you reach a dead end (either the previous word was never seen or there are no words ever seen after that word), end generation early and return a shorter array. If there is more than one "most common" choice seen in the input text, pick the one with the smallest word according to the String compareTo method (NOTE: OrderedSets and OrderedMaps such as TreeSets and TreeMaps order their set (or set of keys) according to compareTo.) Example: Bigram y new Bigram("The balloon was red. The balloon got bigger and bigger. The balloon popped."); y.generate("The", 3) returns the String array ["The", "balloon", "got" y generate("popped 2) returns "popped. A tester program will be released which will test multiple larger examples. Your code should be able to work with input text containing up to a million words

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions