import java.util.*;
import java.io.*;
/**
*
* read file track every word and the line
* numbers on which each word occurs
*
* @author $Author: ola $
* ola@cs.duke.edu
* @version $Id: WordTracker.java,v 1.2 1999/10/13 06:32:28 ola Exp ola $
*
* $Date: 1999/10/13 06:32:28 $
*/
public class WordTracker
{
public WordTracker(String filename)
{
myFileName = filename;
myMap = new HashMap();
}
/**
* reads a file (specified at construction time)
* and tracks every word and the line numbers on which the word
* occurs. Words are converted to lower case with leading/trailing
* non-letter chars removed
*/
public void read()
{
WordIterator wit = null;
try{
wit = new WordIterator(myFileName);
}
catch (FileNotFoundException e){
System.err.println("***ERROR opening " + myFileName);
}
// more words available?
while (wit.hasNext())
{
Integer lineNum = new Integer(wit.getLineNumber());
String s = ((String) wit.next()).toLowerCase();
s = stripPunc(s);
// get/create vector associated with word
Vector lines = (Vector) myMap.get(s);
if (lines != null)
{
if (! lines.lastElement().equals(lineNum))
{
lines.add(lineNum);
}
}
else
{
lines = new Vector();
lines.add(lineNum);
myMap.put(s,lines);
}
}
}
/**
* print list of words with line numbers as from
* previous call to read
* @see read
*/
public void print()
{
Iterator allKeys = myMap.keySet().iterator(); // each word
while (allKeys.hasNext())
{
String key = (String) allKeys.next();
System.out.print(key + "\t");
Iterator lines = ((Vector) myMap.get(key)).iterator();
while (lines.hasNext())
{
System.out.print((Integer) lines.next()+" ");
}
System.out.println();
}
}
/**
* creates a copy of parameter s with leading and trailing
* non-letter characters removed
* @parameter s string copied with no leading/trailing non-letters
* @return copy of s with leading/trailing non-letter chars removed
*/
private String stripPunc(String s)
{
int k;
int len = s.length();
int start = 0;
int end = len;
for(k=0; k < len; k++)
{
if (Character.isLetter(s.charAt(k))) break;
start++;
}
for(k=len-1; k >= 0; k--)
{
if (Character.isLetter(s.charAt(k))) break;
end--;
}
return s.substring(start,end);
}
public static void main(String args[])
{
if (args.length != 1)
{
System.out.println("usage: WordTracker ");
System.exit(0);
}
WordTracker s = new WordTracker(args[0]);
s.read();
s.print();
}
private String myFileName;
private Map myMap;
}