// Assignment 4 - Pipe-and-Filter KWIC Indexing.
// Concurrent Programming, CS 498-598
// Summer 2002
//
// This code is unmaintained and may be obsolete (as well as being incorrect).
// See the answer to assignment 4 for possible updates.
import java.util.StringTokenizer;
class
KWICIndexEntry {
final private String words[];
final private int index_word;
static KWICIndexEntryGroup
entries(String description) {
// Return a group of index entries derived from the given description.
// Return null if there are no index entries. The group entries are stored
// in no particular order.
final String tokens[] = tokenize(description);
if (tokens == null)
return null;
final KWICIndexEntry rotations[] = new KWICIndexEntry[tokens.length];
for (int i = tokens.length - 1; i >= 0; i-- )
rotations[i] = new KWICIndexEntry(tokens, i);
return new KWICIndexEntryGroup(rotations);
}
private
KWICIndexEntry(String words[], int index_word) {
assert words != null;
assert (0 <= index_word) && (index_word < words.length);
this.words = words;
this.index_word = index_word;
}
boolean
less(KWICIndexEntry kie) {
// Return true iff this entry is less than the given entry.
return words[index_word].compareTo(kie.words[kie.index_word]) < 0;
}
private String
rotated_output() {
// Create a star-form KWIC index entry.
String sep = "";
StringBuffer str = new StringBuffer();
for (int i = 0; i < words.length; i++) {
final int j = (index_word + i) % words.length;
if (j == 0 && i != 0) sep = sep + "* ";
str.append(sep).append(words[j]);
sep = " ";
}
return str.toString();
}
private static String []
tokenize(String str) {
// Return the list of tokens in the given input string. Return null if
// there are no tokens.
final StringTokenizer tokenizer = new StringTokenizer(str);
final int tkn_cnt = tokenizer.countTokens();
if (tkn_cnt < 1)
return null;
final String tokens[] = new String[tkn_cnt];
for (int i = 0; i < tkn_cnt; i++)
tokens[i] = tokenizer.nextToken();
return tokens;
}
public String
toString() {
return rotated_output();
}
private String
unrotated_output() {
// Create a capital-form KWIC index entry.
String sep = "";
StringBuffer str = new StringBuffer();
for (int i = 0; i < words.length; i++) {
str.append(sep).append(i == index_word ? words[i].toUpperCase() : words[i]);
sep = " ";
}
return str.toString();
}
}
syntax highlighted by Code2HTML, v. 0.9