2019独角兽企业重金招聘Python工程师标准>>>

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

在网上找了一个基于管道过滤器的实现,但发现有好象错误,修改了一下使之正确,以下是代码:

Filter类

package com.jason.kwic;import java.io.IOException;public abstract class Filter implements Runnable {// 定义输入管道protected Pipe input;// 定义输出管道protected Pipe output;private boolean isStart = false;Filter(Pipe input, Pipe output) {this.input = input;this.output = output;}// 防止多次调用,调用之后线程开始执行public void start() {if (!isStart) {isStart = true;Thread thread = new Thread(this);thread.start();}}// 线程的 run 方法public void run() {try {this.transform();} catch (IOException e) {e.getMessage();}}// 将输入数据转换为所需数据并写入输出管道// 由子类实现抽象方法protected abstract void transform() throws IOException;
}

Pipe类

package com.jason.kwic;import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
import java.util.Scanner;public class Pipe {//输入管道private Scanner pipereader;//输出管道private PrintWriter pipewriter;public Pipe(){PipedWriter pw = new PipedWriter();PipedReader pr = new PipedReader();try{pw.connect(pr);} catch (IOException e){e.getMessage();}pipewriter = new PrintWriter(pw);pipereader = new Scanner(pr);}//读入一行数据到管道//@return 读入的数据public String readerLine() throws IOException{if (pipereader.hasNextLine()) {return pipereader.nextLine();}return null;}//从管道输出一行数据public void writerLine(String strline) throws IOException{pipewriter.println(strline);}//将读管道关闭,调用该方法后,不能再从管道中读数据//如不能关闭则抛出异public void closeReader() throws IOException{pipereader.close();}//先刷新数据,在将写管道关闭,调用该方法后,不能向管道中写数据//如不能关闭则抛出异常public void closeWriter() throws IOException{pipewriter.flush();pipewriter.close();}
}

Input类:

package com.jason.kwic;import java.io.File;
import java.io.IOException;
import java.util.Scanner;public class Input extends Filter{//输入文件的文件名private File infile;Input(File file, Pipe output){super(null, output);this.infile = file;}@Override//读取数据protected void transform() throws IOException {Scanner sc = new Scanner(infile);String templine = "";while(sc.hasNextLine()){templine = sc.nextLine();//System.out.println("Input:" + templine);output.writerLine(templine);}output.closeWriter();sc.close();}
}

Shift类:

package com.jason.kwic;import java.io.IOException;
import java.util.ArrayList;
public class Shift extends Filter{//单词的列表private ArrayList<String> wordlist = new ArrayList<String>();//重组后的行的列表private ArrayList<String> linelist = new ArrayList<String>();Shift(Pipe input, Pipe output){super(input, output);}@Overrideprotected void transform() throws IOException {String templine = "";//读数据while((templine = input.readerLine()) != null){//将数据拆分为不同单词this.lineSplitWord(templine);//将单词重组为句子this.recombination();//输出重组结果for(int i = 0; i < linelist.size(); i++){//System.out.println("linelist:" + linelist.get(i));output.writerLine(linelist.get(i));}//清空wordlist、linelist和templinewordlist.clear();linelist.clear();templine = "";}input.closeReader();output.closeWriter();}//从一行中提取单词存入单词表中private void lineSplitWord(String line){String word = "";int i = 0;while(i < line.length()){if(line.charAt(i) != ' '){word += line.charAt(i);}else{wordlist.add(word);word = "";}i++;}if (word.length() > 0) {wordlist.add(word);}}private void recombination(){for(int j = 0; j < wordlist.size(); j++){String templine = "";for (int k = wordlist.size() - 1 - j; k < wordlist.size(); k++){templine += wordlist.get(k) + " ";}for (int m = 0; m < wordlist.size() - 1 - j; m++){if(m != wordlist.size() - j - 2){templine += wordlist.get(m) + " ";}else{templine += wordlist.get(m);}}linelist.add(templine);}}
}

Alphabetizer类:

package com.jason.kwic;import java.io.IOException;
//import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//import java.util.Locale;
public class Alphabetizer extends Filter{private ArrayList<String> al = new ArrayList<String>();Alphabetizer(Pipe input, Pipe output){super(input, output);}//对读入的数据进行排序protected void transform() throws IOException {String templine = null;//读入数据while((templine = input.readerLine()) != null){al.add(templine);}//按字母表排序Collections.sort(al, new AlphaabetizerComparator());//对排序后的数据进行输出for(int i = 0; i < al.size(); i++){output.writerLine(al.get(i));}input.closeReader();output.closeWriter();}//使用java提供的Collator类来实现比较
//    private class AlphaabetizerComparator implements Comparator<String> {
//
//      private Collator collator;
//      AlphaabetizerComparator(){
//          this.collator = Collator.getInstance(Locale.ENGLISH);
//      }
//
//      @Override
//      public int compare(String o1, String o2) {
//          return this.collator.compare(o1, o2);
//      }
//
//    }//自己写代码实现比较(使用字母的ascii值来进行比较)private class AlphaabetizerComparator implements Comparator<String> {@Overridepublic int compare(String o1, String o2) {if (o1 == null || o2 == null) {throw new NullPointerException();}int compareValue = 0;char o1FirstCharacter = o1.charAt(0);char o2FirstCharacter = o2.charAt(0);if(this.isLetter(o1FirstCharacter) && this.isLetter(o2FirstCharacter)) {//如果是小写的字母的值,则转成对应的大写的字母的值o1FirstCharacter = this.toUpperCase(o1FirstCharacter);o2FirstCharacter = this.toUpperCase(o2FirstCharacter);compareValue = o1FirstCharacter - o2FirstCharacter;} else {throw new RuntimeException("必须是字母");}return compareValue;}private boolean isLetter(char c) {return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);}private char toUpperCase(char c) {if (Character.isLowerCase(c)) {return Character.toUpperCase(c);}return c;}}
}

Output类:

package com.jason.kwic;import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;public class Output extends Filter{//输出文件的文件名private File file;Output(Pipe input, File file){super(input, null);this.file = file;}//输出数据protected void transform() throws IOException {PrintWriter pw = new PrintWriter(file);String templine = "";while((templine = input.readerLine()) != null){pw.write(templine);pw.write("\n");}pw.flush();pw.close();input.closeReader();}
}

Main主程序

package com.jason.kwic;import java.io.File;
import java.util.Scanner;public class Main {public static void main(String[] args) {File infile = new File("d:\\temp\\mykwic_in.txt");File outfile = new File("d:\\temp\\mykwic_out.txt");Scanner inputfile;Scanner outputfile;try {inputfile = new Scanner(infile);outputfile = new Scanner(outfile);// 定义三个管道Pipe pipe1 = new Pipe();Pipe pipe2 = new Pipe();Pipe pipe3 = new Pipe();// 定义四种过滤器Input input = new Input(infile, pipe1);Shift shift = new Shift(pipe1, pipe2);Alphabetizer alph = new Alphabetizer(pipe2, pipe3);Output output = new Output(pipe3, outfile);// 启动四种过滤器的线程
//          input.start();
//          shift.start();
//          alph.start();
//          output.start();//不启用线程,顺序执行四个过滤器input.transform();shift.transform();alph.transform();output.transform();// 直接输出结果System.out.println("-----  infile   -----");String str = null;while (inputfile.hasNextLine()) {str = inputfile.nextLine();System.out.println(str);}System.out.println("input  end");//启用线程时要让当前线程睡一段时间.//Thread.sleep(3000);System.out.println("-----  outfile  -----");while (outputfile.hasNextLine()) {str = outputfile.nextLine();System.out.println(str);}inputfile.close();outputfile.close();} catch (Exception e) {// e.getMessage();e.printStackTrace();}}
}

注意其中的infile和outfile对应的路径要修改成实际的路径,并且必须存在这两个文件。

转载于:https://my.oschina.net/u/914897/blog/408947

基于管道过滤器实现的kwic实现相关推荐

  1. 基于管道过滤器风格的-KWIC

    1.题前分析 kwic是什么到低要解决什么问题? 看这段英文的变化,输入的值的是绿色圈出的部分,第一次处理后的结果是橙色圈出的部分,第三次处理的结果是红色圈出的部分,第一次处理,先是对句话进行了切分, ...

  2. 管道 过滤器风格 java_完成基于管道过滤器风格的KWI实现.doc

    完成基于管道过滤器风格的KWI实现.doc 实验2:软件体系结构风格实现 一.实验目的 初步了解不同的体系结构风格 掌握不同体系结构风格的实现 二.实验学时 4学时. 三.实验方法 根据KWIC的描述 ...

  3. 系统程序员成长计划-管道过滤器(Pipe-And-Filter)模式

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 系统程序 ...

  4. 130242014013+杨俊杰+第3次实验

    一.实验目的 1.理解不同体系结构风格的具体内涵. 2.学习体系结构风格的具体实践. 二.实验环境 硬件: (依据具体情况填写) 软件:Java或任何一种自己熟悉的语言 三.实验内容 "上下 ...

  5. 【软考系统架构设计师】2022下案例分析历年真题

    [软考系统架构设计师]2022下案例分析历年真题 [软考系统架构设计师]2022下案例分析历年真题 [软考系统架构设计师]2022下案例分析历年真题 2022下案例分析历年真题第一题(25分) 202 ...

  6. 图解“管道过滤器模式”应用实例:SOD框架的命令执行管道

    管道和过滤器 管道和过滤器是八种体系结构模式之一,这八种体系结构模式是:层.管道和过滤器.黑板.代理者.模型-视图-控制器(MVC) 表示-抽象-控制(PAC).微核.映像. 管道和过滤器适用于需要渐 ...

  7. 基于netty实现的socks5代理协议

    基于netty实现的socks5代理协议 socks5协议 简介 socks5协议是一个标准的代理协议,工作在网络的四层,理论上可以代理任意应用层协议.协议标准RFC1928,用户/密码鉴权标准RFC ...

  8. 基于Golang的简单web服务程序开发——CloudGo

    基于Golang的简单web服务程序开发--CloudGo[阅读时间:约10分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 (1)基本要求 (2)扩展要求 三.具体 ...

  9. 【ReactiveX】基于Golang pmlpml/RxGo程序包的二次开发

    基于Golang pmlpml/RxGo程序包的二次开发[阅读时间:约20分钟] 一.ReactiveX & RxGo介绍 1.ReactiveX 2.RxGo 二.系统环境&项目介绍 ...

最新文章

  1. 这个“大脑”收获一份大奖!
  2. silverlight中的socket编程注意事项
  3. 用于python环境下的数据操作_数据分析(一):环境搭建,以及初步操作文件
  4. [云炬创业基础笔记]第二章创业者测试16
  5. C++语言函数重载详解和示例
  6. 如何调试SharePoint中XsltListViewWebPart的XSL
  7. Python安装时import matplotlib.pyplot as plt报错
  8. 大样本OLS模型假设及R实现
  9. QTableView自定义拖拽行
  10. 回溯算法高效解标准数独(MarkDown)
  11. 工业对5G网络的应用需求和通信过程
  12. flex: 1到底是什么意思?
  13. 单页面模式和多页面模式详解
  14. 外行转it_外行人员的微服务容器
  15. 安大计算机学院汤进,“CCF合肥庐州论坛——认知计算研讨会”成功举办
  16. 银联的跨行清算体系架构分析
  17. KMP字符串模式匹配算法【精简代码模板】
  18. 交付管理——怎样管控项目成本
  19. 一个定制CFileDialog对话框的实例
  20. 基于python的管理系统_基于ssm的管理系统_基于python管理系统

热门文章

  1. Deepin笔记本WIFI速度过慢问题
  2. 分类性能度量指标:ROC曲线、AUC值、正确率、召回率、敏感度、特异度
  3. 现在银行基金还有希望吗?
  4. php外翻截骨术,楔形截骨术与V形截骨术治疗拇外翻效果相似
  5. C++ 实现两个向量之间的夹角
  6. E: 仓库 没有Release 文件
  7. 上海交通大学计算机学院录取分数线,湖南省多少名可以进上海交大?附上海交通大学近三年录取分数线...
  8. Prim算法简易教程(~简单易懂,附最详细注释代码)
  9. 计算机辐射测试,无线路由器辐射测试方法
  10. android能播放4k视频格式,安卓APP,无广告支持多种格式的万能视频播放器