Create a class called Bigram. The class will have a constructor which takes a St
ID: 3840780 • Letter: C
Question
Create a class called Bigram. The class will have a constructor which takes a String. Use a Scanner withits default tokenization on the String. As long as hasNext() returns true, each call to next() will retrievethe next word. Note that some words will be capitalized differently or contain punctuation. Treat eachof those differently (for example, "Dogs", "dogs", and "dogs." are all different strings).
Checking a phrase will consist of looking at each adjacent pair of adjacent words. If all adjacent pairswere seen in your input text, your code will return true, otherwise false.
Example:Bigram x = new Bigram("Bob likes dogs. Bill likes cats. Jane hates dogs.");
x.check("Bob likes cats.") returns true: "Bob likes" and "likes cats." both appear in the inputtext.
x.check("Jane likes cats.") returns false: "Jane likes" does not appear in the input text.
Your phrase generation method will be given a start word and a count indicating the number of totalwords 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. Italways starts by generating the start word. As you generate each word, the next word generatedshould be the one that appears most often in the input (constructor) text after the previous wordgenerated. If you reach a dead end (either the previous word was never seen or there are no words everseen after that word), end generation early and return a shorter array. If there is more than one “mostcommon” choice seen in the input text, pick the one with the smallest word according to theString.compareTo method (NOTE: OrderedSets and OrderedMaps such as TreeSets and TreeMaps ordertheir 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.
Please show coding! thanks
Explanation / Answer
PROGRAM CODE:
import java.util.Arrays;
import java.util.Scanner;
import arrays.ArrayList;
public class Bigram {
ArrayList<String> wordList;
int size;
public Bigram(String s) {
size = 0;
wordList = new ArrayList<>();
Scanner splitter = new Scanner(s);
while(splitter.hasNext())
wordList.add(splitter.next());
size = wordList.size();
splitter.close();
}
public boolean check(String s) {
String tokens[] = s.split(" ");
for(int i=0; i<size-1; i++)
{
if(wordList.get(i).equals(tokens[0]))
{
if(wordList.get(i+1).equals(tokens[1]))
return true;
}
}
return false;
}
public String[] generate(String start, int count) {
ArrayList<String> list = new ArrayList<>();
for(int i=0; i<size; i++)
{
if(wordList.get(i).equals(start))
{
int j=0;
while(j<count && (i+j)<size)
{
if(list.size()<count)
{
list.add( wordList.get(i+j));
}
else if(list.get(j).compareTo(wordList.get(i+j))>0)
{
list.set(j, wordList.get(i+j));
}
j++;
}
}
}
String res[] = new String[list.size()];
for(int i=0; i<list.size(); i++)
res[i] = list.get(i);
return res;
}
public static void main(String args[])
{
Bigram y = new Bigram("The balloon was red. The balloon got bigger and bigger. The balloon popped.");
String result1[] = y.generate("The", 3);
System.out.println(Arrays.toString(result1));
String result2[] = y.generate("popped.", 2);
System.out.println(Arrays.toString(result2));
}
}
OUTPUT:
[The, balloon, got]
[popped.]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.