The goal of this assignment is to practice string processing functions, and desi
ID: 3872073 • Letter: T
Question
The goal of this assignment is to practice string processing functions, and design a solution which distributes the functionalities among appropriate classes.
Problem:
When you are given a text file, parse, tokenize, and further split the tokens into specified sized letter groups.
Example:
Input: “Second Programming Assignment”
Tokens: “second” “programming” “assignment”
Letter Groups (2) : "se" "ec" "co" "on" "nd" "pr" "ro" "og" "gr" "ra" "am" "mm" "mi" "in" "ng" "as" "ss" "si" "ig" "gn" "nm" "me" "en" "nt"
Letter Groups (3): "sec" "eco" "con" "ond" "pro" "rog" "ogr" "gra" "ram" "amm" "mmi" "min" "ing" "ass" "ssi" "sig" "ign" "gnm" "nme" "men" "ent"
After generating the letter groups, generate the histogram (frequency of occurrence) of the letter groups.
The name of the text file will be the first argument of your main function and letterGroupLen will be the second argument of your main function.
Parse the input text file
You will have 2 classes in your design. SentenceUtils and Histogram.
SentenceUtils class will tokenize and partition the tokens into letter groups
Histogram class using a HashMap data structure will count the number of occurrence of each letter group, and print the results when requested.
The solution is composed of 2 classes: a SentenceUtils Class to convert a string into letterGroups and also an Histogram class to do histogram processing. These 2 functionalities are independent of each other.
You need to implement body of the following 4 functions and also the main function:
private String[] getTokens(String line)
private void splitTokenstoLetterGroups(String[] tokens)
public void generateHistogram(ArrayList letterGroups)
public void printHistogram()
Prof wants it in her format like this. Im so lost in this class please help
public class SentenceUtils f private ArrayList mLetterGroups; private int mLetterGroupLen; public SentenceUtils (int letterGroupLen) this.mLetterGroups = new ArrayList(); this.mLetterGroupLen = let terGroupLen; public void addwords (String line) String[] tokens = this.getTokens (line); this.splitTokenstoLetterGroups (tokens); public ArrayList getLetterGroups) return (this.mLetterGroups); This function splits a string into array of strings separated by space character input: a string output: array of words seperated by space character example: input: "object oriented" output: ["object", "oriented"] private String[ getTokens (String line) You are going to implement body of this function This function splits the tokens into letter groups according to this.mLetterGroupLen input: array of tokens output: letterGroups of size this.mLetterGroupLen appended to this.mLetterGroups example:input "object" "oriented" output: this·mLetterGroups (of size 4) = this.mLetterGroups + ["obje","bjec", "ject", "orie", "rien", "ient", "ente", "nted"] private void splitTokenstoLetterGroups (String] tokens) You are going to implement body of this functionExplanation / Answer
import java.util.*;
import java.io.*;
public class Histogram {
public static void main(String[] args) throws Exception
{
File myFileName=new File("example.txt");
String line="";
Scanner input=new Scanner(myFileName);
while(input.hasNextLine())
{
line+=input.nextLine();
}
line=line.replaceAll(" ", "");
String[] tokens=getTokens(line);
}
private static String[] getTokens(String line)
{
//for three letter just replace 2 with the 3.
String[] tokens=line.split("(?<=\G.{2})");
splitTokenstoLetterGroups(tokens);
return tokens;
}
private static void splitTokenstoLetterGroups(String[] tokens)
{
ArrayList <String> letterGroups=new ArrayList<>();
for(int i=0;i<tokens.length;i++)
{
letterGroups.add(tokens[i]);
}
generateHistogram(letterGroups);
}
public static void generateHistogram(ArrayList<String> letterGroups) {
HashMap<String, Integer> histoGramPairSet= new HashMap<String , Integer>();
int size=letterGroups.size();
for(int i=0;i<size;i++)
{
String str=letterGroups.get(i);
if (histoGramPairSet.containsKey(str))
{
int value=histoGramPairSet.get(str);
histoGramPairSet.put(str,++value );
}
else
{
histoGramPairSet.put(str, 1);
}
}
printHistogram(histoGramPairSet);
}
public static void printHistogram(HashMap<String, Integer> histoGram) {
System.out.println("Histohrams are :");
System.out.println(histoGram);
}
}
example.txt file contains
SeSeSeSeSeSeSeSeSe OeOeOe
OUTPUT:-
Histohrams are :
{Se=9, Oe=3}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.