主程序-子程序、面向对象、事件系统和管道-过滤软件体系结构实现KWIC

本文介绍了KWIC(Key Word In Context)索引系统的概念和实现,通过四种软件体系结构化实现:主程序-子程序、面向对象、事件系统和管道-过滤。在面向对象设计中,包括Input、Shift、Alphabetizer和Output类。事件系统采用观察者模式,而管道-过滤架构则涉及一系列过滤器类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

KWIC(Key Word In Context),Parnas (1972)索引系统

KWIC索引系统接受一些行,每行有若干字,每个字由若干字符组成;每行都可以循环移位,亦即重复地把第一个字删除,然后接到行末; KWIC把所有行的各种移位情况按照字母表顺序输出(不考虑大小写)。

输入:若干行字符语句

The sun is rising in the east
Flowers are blooming

中间过程:循环移位后形成下面的结果

The sun is rising in the east 
sun is rising in the east the 
is rising in the east the sun
rising in the east the sun is
in the east the sun is rising
the east the sun is rising in
east the sun is rising in the
Flowers are blooming
are blooming Flowers 
blooming Flowers are

输出:排序后的结果

are blooming Flowers 
blooming Flowers are
east the sun is rising in the
Flowers are blooming
in the east the sun is rising
is rising in the east the sun
rising in the east the sun is
sun is rising in the east the
the east the sun is rising in
The sun is rising in the east 

四种软件体系结构化实现

一、主程序-子程序软件体系结构:

在这里插入图片描述

public class demo1 {
   
    private ArrayList<String> kwicList = new ArrayList<String>();
    private ArrayList<String> lineTxt = new ArrayList<String>();
    private BufferedReader inputFile;
    private BufferedWriter outputFile;

    public static void main(String[] args) {
   

        demo1 kwic = new demo1();
        kwic.input("E:\\input.txt");
        kwic.shift();
        kwic.alphabetizer();
        kwic.output("E:\\output.txt");
    }


    public void input(String fileName) {
   
        try {
   
            inputFile = new BufferedReader(new FileReader(fileName));
        } catch (Exception e) {
   
            e.printStackTrace();
        }
        String line;
        try {
   
            while ((line = inputFile.readLine()) != null) {
   
                lineTxt.add(line);
            }
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }

    public void output(String filename){
   
        Iterator<String> it = kwicList.iterator();
        try {
   
            outputFile = new BufferedWriter(new FileWriter(filename));
            while (it.hasNext()) {
   
                outputFile.write(it.next()+"\n");
            }
        }catch (IOException e){
   
            e.printStackTrace();
        }finally {
   
            try {
   
                if (outputFile!=null) {
   
                    outputFile.close();
                }
            } catch (IOException e) {
   
                e.printStackTrace();
            }
        }
    }

    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) {
   
                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();
                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.toLowerCase().charAt(0); //忽略大小写
            char o2c = o2.toLowerCase().charAt(0); //忽略大小写
            compareValue = o1c - o2c;
            return compareValue;

        }

    }
}
二、面向对象软件体系结构

在这里插入图片描述

Input类

public class Input {
   
    private ArrayList<String> lineTxt = new ArrayList<String>();

    public ArrayList<String> getLineTxt() {
   
        return lineTxt;
    }

    public void input(String fileName) {
   
        BufferedReader inputFile = null;
        try {
   
            inputFile = new BufferedReader(new FileReader(
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值