I need to write a Java program given the following parameters: When you are give
ID: 3870551 • Letter: I
Question
I need to write a Java program given the following parameters:
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<String> letterGroups)
public void printHistogram()
Explanation / Answer
JAVAPROGRAM
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class TextFileParser {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file=new File("test.txt");
String line="";
Scanner scanner=new Scanner(file);
while(scanner.hasNextLine())
{
line+=scanner.nextLine();
}
//For printing the text file string
//System.out.println(line);
line=line.replaceAll(" ", "");
String[] words=getTokens(line);
//For printg two two letters string array.
/*
for(int i=0;i<words.length;i++)
{
System.out.println(words[i]);
}
*/
}
private static String[] getTokens(String line) {
// TODO Auto-generated method stub
//This line divide the tockens into two two letters.
//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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
HashMap<String, Integer> histoGram= new HashMap<String , Integer>();
int size=letterGroups.size();
for(int i=0;i<size;i++)
{
String str=letterGroups.get(i);
if (histoGram.containsKey(str))
{
int value=histoGram.get(str);
histoGram.put(str,++value );
}
else
{
histoGram.put(str, 1);
}
}
printHistogram(histoGram);
}
public static void printHistogram(HashMap<String, Integer> histoGram) {
System.out.println(histoGram);
}
}
Histograms are:-
output:-
{nt=1, As=1, si=1, og=1, me=1, mi=1, ig=1, ec=1, tS=1, on=1, mm=1, ss=1, Pr=1, in=1, gn=1, en=1, gr=1, co=1, dP=1, am=1, ra=1, Se=1, nd=1, ng=1, gA=1, ro=1, nm=1}