// Home       Next      Previous

Class: cs312
Lab: 5b
Description: Counts frequencies of words using a hash table
*/
import java.util.Hashtable; 
import java.util.StringTokenizer;
import java.util.Enumeration;
/* An object that implements the Enumeration interface 
* generates a series of elements, one at a time. 
* Successive calls to the nextElement method return 
* successive elements of the series. */
import java.io.FileReader;
import java.io.BufferedReader;

public class CountWords1 {

public static void main( String args[] ){

if( args.length > 0 ){
try{
/* Open file */
BufferedReader in = new BufferedReader( new FileReader(args[0]) );
Hashtable count = new Hashtable(500);

String inLine = null;

while( (inLine = in.readLine()) != null ){
StringTokenizer words = new StringTokenizer
( inLine, " - . , ' : ; ? ! @ < > # $ % ^ & * ( ) + = / 1 2 3 4 5 6 7 8 9 0 " );
while( words.hasMoreTokens() ){

Object w = words.nextToken().toLowerCase();

/* Seen the word before? Incrememnt counter */
if( count.containsKey(w) ) // the function containsKey();
// Test whether the specified key 
//has an associated value
// in the table.
{
Integer c = (Integer)(count.get(w)); 
count.put( w, new Integer( c.intValue() + 1 ));
}
else{
// The function put(); is from Hashtable.java 
// Associate the specified value with the specified key.
// Precondition: The key is not null.
count.put( w, new Integer(1) );
}
}
}

in.close();

/* Display result */
System.out.println("Word Freq" );
System.out.println("------ ----- ");
Enumeration words = count.keys();
/* The function hasMoreElements(); is from util.Enumeration;
Tests if this enumeration 
contains more elements return true if and only if this 
enumeration object contains at least one more element to provide;
false otherwise.
*/
while( words.hasMoreElements() )
{ /**
* The function nextElement(); Returns the next element of 
* this enumeration if this enumeration object has at
* least one more element to provide. return the
* next element of this enumeration. exception
* NoSuchElementException if no more elements exist.
*/
Object w = words.nextElement(); 
// The function get() is from util.Hashtable.java 
// Retrieve the value associated with the specified key
// in the table, if there is any. If not, the value
// null will be returned.
System.out.println(count.get(w)+ " " + w );
}
}
catch( Exception e ){
System.err.println(e);
System.exit(-1);
}
}
else{
System.err.println( "Usage: java CountWords filename\n" );
System.exit(-1);
}
}

}
/* OUTPUT FOR SAMPLE 1
Z:\lab5>java CountWords1 sample1.txt 
Word Freq
------ -----
2 object
1 teaches
1 using
2 java
1 and
1 rigorous
1 makes
2 oriented
1 approach
1 informal
1 includes
Word Freq
------ -----
1 it
2 structures
1 of
1 use
1 constr
1 with
1 concepts
1 yet
1 classic
1 an
3 the
2 appropriate
2 data



*/
/* OUTPUT FOR SAMPLE 2

Z:\lab5>java CountWords1 sample2.txt
Word Freq
------ -----
1 relates
1 support
1 mandates
1 tech
1 throughout
2 three
1 related
1 feel
1 was
3 oals
1 privacy
1 always
1 memory
1 took
2 rules
1 assistance
1 into
1 added
1 workstations
6 links
2 resources
1 visitors
3 here
4 new
1 part
1 they
1 magazines
2 ensure
1 seven
1 lablink
1 work
1 after
1 system
2 running
1 upgrading
7 campus
1 these
1 advanced
1 enhanced
1 thanks
1 visit
1 macintosh
1 home
1 out
1 standard
1 operating
1 broken
1 designed
2 our
1 process
1 break
1 microsoft
2 job
1 there
1 space
3 lab
1 found
1 six
1 donning
1 windows
1 ranging
1 may
1 faculty
1 news
1 want
1 stuff
1 processor
4 is
2 important
1 understanding
1 map
1 art
1 processors
4 in
4 information
1 bookmark
3 open
4 all
1 quarters
1 jobs
1 less
1 cd
1 www
1 place
1 might
2 contains
1 policies
1 professional
1 includes
2 guidelines
1 capability
1 shows
1 gigabyte
1 staff
1 possible
1 educational
1 well
1 headings
1 included
1 far

Word Freq
------ -----
3 be
3 categories
1 sports
1 users
3 access
1 range
1 machines
2 at
2 your
7 as
1 devoted
3 if
1 stone
1 enter
3 are
1 productivity
5 link
1 catalog
1 los
1 statement
1 tower
4 which
5 you
1 reading
1 ghz
1 were
11 that
1 helpdesk
1 than
1 internet
1 pcs
1 guide
2 hit
1 when
2 services
1 baseline
2 pages
1 please
2 provide
1 recommendations
50 the
1 mail
1 years
1 labs
1 opened
1 below
1 anything
2 initiative
1 entertainment
2 search
1 run
7 we
1 oal
2 from
2 community
2 edu
2 computing
1 other
1 schedule
2 pentium
1 searching
1 classes
2 right
1 now
1 coming
2 csla
1 people
4 online
1 left
1 consists
2 iii
1 set
1 best
2 ve
1 winter
1 can
1 cal
1 via
2 any
1 personalized
1 us
1 theme
7 have
2 wide
4 with
1 network
1 laboratories
14 and
1 surfing
4 student
1 provides
1 searches
1 homepages
1 software
1 technology
3 provided
30 to
1 addition
1 engines

Word Freq
------ -----
1 library
1 shopping
1 what
1 houses
1 none
1 workshops
6 sites
1 also
1 sstp
1 angeles
1 would
3 listing
3 hyperlinks
1 helpful
1 california

    .

    .

   .

// Home       Next      Previous

New Page 1