Write a program named MyTokens8 to read from two text files that are passed as a
ID: 664998 • Letter: W
Question
Write a program named MyTokens8 to read from two text files that are passed as a command-line argument. Words are delimited by whitespace, punctuation marks (,;.:?), quotation marks (’”), and parentheses. The program should do the following:
Count the number of lines in each file.
Count the number of tokens in each file.
Count the number of sentences in each file.
Store all alphabet words in file1 and file2 into separate data structures.
Store all numeric values in file1 and file2 into separate data structures.
Stores all other tokens in file1 and file2 into separate data structures.
Have the union, difference, and intersection of the numeric values. (You can clone the sets to preserve the original sets from being changed by these operations).
Sort the alphabet words in each list
Create lists without duplicates.
Display the alphabet words with their frequencies.
Have a sample output in addition to the program report. Make sure the output is well labeled, aligned, indented, and formatted.
Fill in self-evaluation (have the self-evaluation in the report):
Can your program display number of lines in each file? _______________
Can your program display number of tokens in each file? _______________
Can your program display number of sentences in each file? _______________
Can your program recognize alphabet words? _______________
Can your program recognize numeric values? _______________
Can your program recognize other tokens? _______________
Can your program compute union, difference, and intersection of numeric values? _______________
Can your program sort lists? ___________________________
Can your program create unique lists (no duplicates)? __________________________
Can your program display frequency of words?____________________________
Explanation / Answer
// File: MyTokens8.java
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class MyTokens8
{
public static void main(String[] args) throws FileNotFoundException
{
String fileName1 = args[0];
String fileName2 = args[1];
Scanner infile1 = new Scanner(new File(fileName1));
Scanner infile2 = new Scanner(new File(fileName2));
int linesCount1 = 0, linesCount2 = 0;
int tokensCount1 = 0, tokensCount2 = 0;
int sentencesCount1 = 0, sentencesCount2 = 0;
HashMap<String, Integer> words1 = new HashMap<String, Integer>();
HashMap<String, Integer> words2 = new HashMap<String, Integer>();
HashMap<String, Integer> values1 = new HashMap<String, Integer>();
HashMap<String, Integer> values2 = new HashMap<String, Integer>();
ArrayList<String> tokens1 = new ArrayList<String>();
ArrayList<String> tokens2 = new ArrayList<String>();
String line;
String str;
StringTokenizer tokens;
while(infile1.hasNextLine())
{
line = infile1.nextLine();
linesCount1++;
tokens = new StringTokenizer(line, ".,;:?!- ");
while(tokens.hasMoreTokens())
{
str = tokens.nextToken();
if(isNumber(str))
{
if(values1.containsKey(str))
values1.put(str, values1.get(str) + 1);
else
values1.put(str, 1);
}
else
{
if(words1.containsKey(str))
words1.put(str, words1.get(str) + 1);
else
words1.put(str, 1);
}
tokens1.add(str);
tokensCount1++;
}
sentencesCount1 += getSentCount(line);
}
while(infile2.hasNextLine())
{
line = infile2.nextLine();
linesCount2++;
tokens = new StringTokenizer(line, ".,;:?!- ");
while(tokens.hasMoreTokens())
{
str = tokens.nextToken();
if(isNumber(str))
{
if(values2.containsKey(str))
values2.put(str, values2.get(str) + 1);
else
values2.put(str, 1);
}
else
{
if(words2.containsKey(str))
words2.put(str, words2.get(str) + 1);
else
words1.put(str, 1);
}
tokens2.add(str);
tokensCount2++;
}
sentencesCount2 += getSentCount(line);
}
System.out.println("Number of lines in the file1: " + linesCount1);
System.out.println("Number of lines in the file2: " + linesCount2);
System.out.println("Number of tokens in the file1: " + tokensCount1);
System.out.println("Number of tokens in the file2: " + tokensCount2);
System.out.println("Number of sentences in the file1: " + sentencesCount1);
System.out.println("Number of sentences in the file2: " + sentencesCount2);
}
private static boolean isNumber(String str)
{
for(int i = 0; i < str.length(); i++)
{
if(!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
private static int getSentCount(String str)
{
int count = 0;
for(int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if(ch == '.' || ch == '?' || ch == '!')
count++;
}
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.