“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每一个单词又是字母的有序集合。通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被“循环地移动”。KWIC检索系统以字母表的顺序输出一个所有行循环移动的列表。
一. 主程序/子程序的方法:
import
java.io.BufferedReader;
import java.util.StringTokenizer;
public class Kwic2 {
private ArrayList<String> kwicList=new ArrayList<String>();
private ArrayList<String> lineTxt=new ArrayList<String>();
private BufferedReader inputFile;
public static void main(String[] args) {
Kwic2 kwic2=new Kwic2();
kwic2.input("D:
\\test.txt");
kwic2.shift();
kwic2.alphabetizer();
kwic2.output();
}
public void input(String fileName) {
try {
inputFile = new BufferedReader(new FileReader(fileName));
} catch (Exception e) {
System.err.print("wrong");
System.exit(1);
}
String line;
try {
while ((line = inputFile.readLine()) != null) {
lineTxt.add(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void output(){
Iterator<String> it=kwicList.iterator();
System.out.println("the output is ");
while(it.hasNext()){
System.out.println(it.next());
}
}
public void shift(){
//获取每个单词,存入tokens
Iterator<String> it=lineTxt.iterator();
while(it.hasNext()){
StringTokenizer token=new StringTokenizer(it.next());
ArrayList<String>tokens=new ArrayList<String>();
int i=0;
//循环添加单词
int count=token.countTokens();
while(i<count){
//System.out .println(token.nextToken().toString());
tokens.add(token.nextToken());
i++;
}
//display(tokens);
//切割各个单词,不断改变起始值和利用loop实现位移。
for(i=0;i<count;i++){
StringBuffer lineBuffer=new StringBuffer();
int index=i;
for(int f=0;f<count;f++){
//从头继续位移
if(index>=count)
index=0;
//存入StringBuffer
lineBuffer.append(tokens.get(index));
lineBuffer.append(" ");
index++;
}
String tmp=lineBuffer.toString();
//System.out .println("the buffer is "+tmp);
kwicList.add(tmp);
}
}
}
public void alphabetizer(){
Collections.sort(this.kwicList,new AlphabetizerComparator());
}
private class AlphabetizerComparator implements Comparator<String>{
@Override
public int compare(String o1,String o2){
if(o1==null&&o2==null){
throw new NullPointerException();
}
int compareValue=0;
char o1c=o1.charAt(0);
char o2c=o2.charAt(0);
compareValue=o1c-o2c;
return compareValue;
}
}
}