码迷,mamicode.com
首页 > 其他好文 > 详细

130242014050-池国雄-第3次实验

时间:2017-11-19 13:32:11      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:ges   int   有序   ring   实验目的   name   列表   sys   new   

一、实验目的

1.理解不同体系结构风格的具体内涵。

2.学习体系结构风格的具体实践。

二、实验环境

硬件: (依据具体情况填写)

软件:Java或任何一种自己熟悉的语言

三、实验内容

 

“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每一个单词又是字母的有序集合。通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被“循环地移动”。KWIC检索系统以字母表的顺序输出一个所有行循环移动的列表。

尝试用不同的策略实现这个系统。选择2-3种体系结构风格来实现。

四、实验步骤:

采用管道过滤器风格

1、体系结构图:

技术分享图片

2、简述体系结构各部件的主要功能,实现思想。

上述的主程序/子程序的方法,将问题分解为文件打开(fileopen)、读取(fileopen)、循环移动和按字母表排序(parseLine)、输出(dispaly)。

3、写出主要的代码

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 KWIC {
    private static BufferedReader input_file;
    private ArrayList<String> kwicList;
    public KWIC (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 KWIC("D://test.txt");
    }
}

test.text内容

技术分享图片

显示结果:

技术分享图片

130242014050-池国雄-第3次实验

标签:ges   int   有序   ring   实验目的   name   列表   sys   new   

原文地址:http://www.cnblogs.com/cgx11/p/7859436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!