kwic系统模拟

本文介绍了一个简单的KWIC(关键词上下文索引)程序实现方法,使用Java编程语言完成。该程序能够读取指定文件,对每一行进行词法分析并进行循环移位,最终输出所有可能的关键词上下文索引组合。

package org.bupt.kwic;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class mykwic {

private static BufferedReader input_file;
private ArrayList<String> kwicList;


public mykwic (String filename) //construct the index of file fname
{
kwicList = new ArrayList<String>();
String line="";
fileopen(filename);
while (line!= null)
{
line= readline();
if (line !=null)
{
parseLine(line, kwicList);
}
}

//Collections.sort(kwicList);

display ( kwicList );
}

public static void fileopen(String InputFilename) {
try {
input_file = new BufferedReader(new FileReader(InputFilename));
} catch (IOException e) {
System.err.println(("File not open" + e.toString()));
System.exit(1);
}
}

public static String readline() {
String line ="";
try {
line = input_file.readLine();
} catch (Exception e) {
e.getStackTrace();
}

return line;
}

public void parseLine(String line,ArrayList<String> list) {
StringTokenizer tokener = new StringTokenizer(line);
String token = new String();
int index;

ArrayList<String> tokens = new ArrayList<String>();
int count = tokener.countTokens();

for (int j = 0; j < count; j++) {//将一行解析,并且将解析的word加入ArrayList中
token = tokener.nextToken();
tokens.add(token);
}

//对ArrayList中的字进行循环移位,得出最后结果
for (int i = 0; i < count; i++) {
index=i;
StringBuffer linebuffer = new StringBuffer();
for (int j = 0; j < count; j++) {

if (index >= count)
index = 0;

linebuffer.append ( tokens.get(index) );
linebuffer.append (" ");
index++;
}
line = linebuffer.toString();
kwicList.add(line);
}
}

public static void display(ArrayList<String> List) {
System.out.println("Output is");
for (int count = 0; count < List.size(); count++) {
System.out.println (List.get (count) );
}
}

public static void main(String[] args) {
new mykwic("test.txt");
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值