In: Computer Science
A concordance is a table that tells how often a word appears in a text (or where in a text it appears; but we are not using this definition).
You are going to write a program for a concordance that:
The next step in writing this program is to write the Concordance class with:
To simplify the assignment, assume that a word is any collection of consecutive characters separated by white space.
Your input should be a text file (or equivalent) and your output should be a listing of the words (in alphabetical order) and the number of times that they appear in the input.
You will submit a program listing (properly commented), your sample document (at least 40 lines of text) and the print-out.
Program
import java.util.*;
import java.io.*;
public class Concordance
{
int count;
String words[] = new String[200];
int countwords[] = new int[1200];
//constructor
public Concordance()
{
this.count = 0;
}
public void addWords(String s)
{
s = s.toLowerCase();
int n = s.length();
char c = s.charAt(n-1);
if (c < 97 || c > 122)
s =
s.substring(0,n-1);
int i = search(s);
if(i==-1)
{
words[count] =
s;
countwords[count] = 1;
count++;
}
else
{
countwords[i]++;
}
}
//method ti\o sort the words
public void sort()
{
for(int i=0; i<count-1;
i++)
{
for(int j=i+1;
j<count; j++)
{
if(words[i].compareTo(words[j])>0)
{
String t = words[i];
words[i] = words[j];
words[j] = t;
int k = countwords[i];
countwords[i] =
countwords[j];
countwords[j] = k;
}
}
}
}
//method to search a word
public int search(String s)
{
for(int i=0; i<count; i++)
{
if(words[i].equals(s)==true)
{
return i;
}
}
return -1;
}
//method to print the list of words and the number of
occurences
public void print()
{
System.out.println("Words\tNumber
of occurences");
System.out.println("---------------------------");
for(int i=0; i<count; i++)
{
System.out.printf("%-15s\t%d\n",words[i],countwords[i]);
}
}
}
class Driver
{
public static void main(String args[]) throws
IOException
{
Scanner in = new
Scanner(System.in);
System.out.print("Enter input Text
File: ");
String textfile =
in.nextLine();
File file = new
File(textfile);
Scanner sc = new
Scanner(file);
Concordance cc = new
Concordance();
while(sc.hasNext())
{
String s = sc.next();
cc.addWords(s);
}
cc.sort();
cc.print();
}
}
Sample output:
Enter input Text File: inputdata.txt
Words Number of Occurences
-----------------------------------------
a 5
about 2
afterwards 1
again 1
all 3
and 3
appropiate 1
as 3
at 1
basis 1
be 2
begin 1
being 1
but 1
comes 1
could 1
daily 1
day 1
delete 1
developer 1
dream 1
embrace 1
end 1
enough 1
experience 1
feel 1
figured 1
first 1
for 2
forgive 1
fortunate 1
free 1
graduated 1
guess 1
have 1
i 7
i'm 1
if 2
insecurities 1
is 2
it 4
it's 1
job 1
junior 2
just 1
know 1
land 1
low 1
me 3
more 1
much 2
my 2
need 3
not 2
noted 1
of 4
on 1
one 1
only 1
personal 1
please 2
position 1
post 1
potentially 1
pretty 1
really 2
redirect 1
right 1
second 1
seeing 1
should 1
show 1
stress 1
stresses 1
sub 3
summer 1
that 2
the 3
this 7
to 9
tolerance 1
too 1
vent 2
was 1
which 1
with 1