import java.util.*;import java.io.*;import java.lang.*;public class Word { //Only non-static data field is the integer wordNo//which is allocated as new strings are made into words//Mapping from wordNo to String maintained by an array//Mapping the other way maintained by HashMap//Mappings inialized with stopwords and others//serving as fixed wordspublic Word(String s)   {//if s known, take old wordNo    Object foundWordNo;    if( (foundWordNo = table.get(s)) != null )       wordNo = ((Integer)foundWordNo).intValue();    else {      //allocate new wordNo      wordNo = wordCount++;      table.put(s, new Integer(wordNo));      stringTable[wordNo]=s;};    };public String toString() {return stringTable[wordNo];};// the following very inefficient way to compare two ints//  ... sic, Java :(public boolean equals(Object rhs){  if(rhs==null || getClass() != rhs.getClass())    return false;  return wordNo == ((Word)rhs).wordNo;  };private int wordNo;public int wordNo() {return wordNo;};private static int wordCount = 0;private static int maxStopwordCount;private static int theWordCount;private static int endoffileWordCount;public boolean isStopword()   {return wordNo <= maxStopwordCount;};public boolean isTheWord()   {return wordNo == theWordCount;};public boolean isEndoffileWord()   {return wordNo == endoffileWordCount;};public boolean isContentWord()   {return !isStopword() && !isTheWord();};public int hashCode() {return wordNo;};//constant below must be > total no of different words in corpusprivate static HashMap table = new HashMap(10000);private static String [] stringTable = new String [10000];static {  try //compiler seems to require this ugly "try" stuff     { new Word("endoffile"); endoffileWordCount = wordCount-1;        FileReader stopWordFile = new FileReader("stopwords");     Word w = readWord(stopWordFile);     while(!w.isEndoffileWord())       {w=readWord(stopWordFile);};     maxStopwordCount = wordCount-1;     new Word("the"); theWordCount = wordCount-1;   }  catch( IOException e )          {  System.out.println( e ); }};public static Word readWord(FileReader f){ try {    char c = (char) (f.read());    while(!Character.isLetter(c)) c = (char) f.read();    StringBuffer buf = new StringBuffer();    while(Character.isLetter(c)) {      buf.append(c);c = (char) f.read();};    return new Word(buf.toString());}  catch( IOException e )          {  System.out.println( e ); return null; }};  public static void main(String [] args) {  new Word("fido");  new Word("karo");  if(new Word("the").isTheWord())     System.out.println("The test OK");  else System.out.println("The test OK");  System.out.println(table);   }} 
