管道过滤体系架构风格实现 KWIC 关键词索引系统

本文介绍了如何运用管道过滤器模式实现KWIC(Keyword in Context)关键词索引系统。该系统由一系列过滤组件构成,包括Alphabetizer、CircularShift和Filter等,它们通过Pipe进行连接,从Input读取内容,经过处理后通过Output展示结果。程序运行成功,有效展示了管道过滤体系架构在处理文本数据方面的应用。
摘要由CSDN通过智能技术生成

Alphabetizer.java:

/**
 * 
 */
package pipestyle;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12know
 *
 */
public class Alphabetizer extends Filter {

	public Alphabetizer(Pipe input, Pipe output) {
		super(input, output);
	}

	/**
	 * 
	 */
	@Override
	protected void transform() {
		List<String> lines = new ArrayList<String>();
		CharArrayWriter writer = new CharArrayWriter();
		try {
			int c = -1;
			while((c = input.read()) != -1) {
				writer.write(c);
		//		System.out.print((char) c);
				if(c == 10) {
					String curLine = writer.toString();
					lines.add(curLine);
					writer.reset();
				}
			}
			
			
			sort(lines);
			
			for(String s : lines) {
				output.write(s);
			}
			
			input.closeReader();
			output.closeWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void sort(List<String> lines) { //堆排序
		int size = lines.size();

		for (int i = (size / 2 - 1); i >= 0; i--)
			siftDown(lines, i, size);

		for (int i = (size - 1); i >= 1; i--) {
			Object tmp = lines.get(0);
			lines.set(0, lines.get(i));
			lines.set(i, (String) tmp);
			siftDown(lines, 0, i);
		}
	}

	private void siftDown(List<String> lines, int root, int bottom) {
		int max_child = root * 2 + 1;

		while (max_child < bottom) {
			if ((max_child + 1) < bottom)
				if (((String) lines.get(max_child + 1))
						.compareTo((String) lines.get(max_child)) > 0)
					max_child++;

			if (((String) lines.get(root)).compareTo((String) lines
					.get(max_child)) < 0) {
				Object tmp = lines.get(root);
				lines.set(root, lines.get(max_child));
				lines.set(max_child, (String) tmp);
				root = max_child;
				max_child = root * 2 + 1;
			} else
				break;
		}
	}

}

CircularShift.java:

/**
 * 
 */
package pipestyle;

import java.io.CharArrayWriter;
import java.io.IOException;

/**
 * 第一行数据到来后开始运作
 * 把原始数据行循环移位,将原始行和新的移位后的行输出给下一模块
 * @author 12know
 * @versio
  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X-jazz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值