/* Data structure to hold a frequency List of words (e.g., from a text) Written 2/98, revised 2/04 by Kim Bruce */ import structure.*; public class FreqList { // vector of associations holding words and their frequencies Vector flist; int totalOccurrences; // total # of instances of words held in list // Create empty list of word frequencies public FreqList(){ totalOccurrences = 0; flist = new Vector(); } // post: if word was not in frequency list it is added w/ frequency of 1, // if already in, then increase frequency by 1 public void add(String word){ // Association w/ word Association assoc = new Association(word); int wordNo = flist.indexOf(assoc); // location of word in list if (wordNo == -1){ // word was not in list, insert it assoc.setValue(new Integer(1)); flist.add(assoc); } else { // word was in list, bump frequency Association element = (Association)flist.get(wordNo); int newValue = ((Integer)(element.getValue())).intValue() + 1; element.setValue(new Integer(newValue)); } totalOccurrences++; } // pre: 0 <= prob <= 1 // post: Returns a word chosen randomly from the words in the list. // The probability of a word being chosen is proportional to its // frequency stored in the list. public String get(double prob){ int count = 0; // sum of frequencies seen so far // number between 0 and size of list - 1 (inclusive) int target = (int)Math.round(prob * (totalOccurrences-1)); Association element=null; // word, frequency pairs from list // search until count exceeds random target for(int wordNo = 0; count <= target && wordNo < flist.size(); wordNo++) { element = (Association)flist.get(wordNo); // # of occurrences of elt int numOccurrences = ((Integer)element.getValue()).intValue(); count = count + numOccurrences; } return (String)element.getKey(); } // return string describing all words and frequencies in the list public String toString() { String answer = ""; // string to accumulate description for (int wordNo=0; wordNo < flist.size(); wordNo++) { Association temp = (Association)flist.get(wordNo); answer = answer + "The word \"" + temp.getKey() +"\" occurs "+temp.getValue()+" times.\n"; } return answer; } public static void main(String args[]) { FreqList list = new FreqList(); String testString = "The quick brown fox jumped over the lazy dog " +"the fox was too quick for the dog"; String[] substring = testString.toLowerCase().split(" "); // Add words to frequency list for (int wordNo = 0; wordNo < substring.length; wordNo++){ list.add(substring[wordNo]); } System.out.println(list); // Pick some words w/ carefully chosen "random" numbers to make sure "get" // works properly. Note advantage of passing in parameter in testing! System.out.println(list.get(0.1)); System.out.println(list.get(1.0)); System.out.println(list.get(0.5)); } }