日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/3/7 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于管道过滤器风格的-KWIC 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、題前分析

kwic是什么到低要解決什么問(wèn)題?


看這段英文的變化,輸入的值的是綠色圈出的部分,第一次處理后的結(jié)果是橙色圈出的部分,第三次處理的結(jié)果是紅色圈出的部分,第一次處理,先是對(duì)句話進(jìn)行了切分,是按照單詞之間的空格進(jìn)行切分,將一整句話切分成多個(gè)單詞,然后對(duì)這些單詞的進(jìn)行移位,每次都將第一個(gè)單詞放在末尾從而組成新的句子,這樣經(jīng)過(guò)第一次處理之后,HELLO WORLD 這句話就變成了兩句話HELLO WORLD 和WORLD HELLO。接下來(lái)進(jìn)行第二步處理,對(duì)這幾句話按照首字母進(jìn)行排序,從而得到最終的結(jié)果。

2、管道過(guò)濾器風(fēng)格
實(shí)現(xiàn)代碼如下
第一版:(具體的思路看代碼的注釋)

package com.bren.cn;import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.PipedInputStream; import java.io.PipedOutputStream;/*** * @author huihui 基于管道過(guò)濾器模式,這是第一版*/ public class mypip {/*** 第一步我應(yīng)該有一個(gè)管道 我需要導(dǎo)入java的管道流的包* @throws IOException */public static void main(String[] args) throws IOException {/*** 輸入管道流*/ PipedInputStream inPip;PipedOutputStream outPip;/*** 管道相連*/inPip= new PipedInputStream();outPip = new PipedOutputStream();try {inPip.connect(outPip);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}/*** 第二步我需要向管道中讀入數(shù)據(jù)"Hello Yolo I am Boss"*/byte[] out = new byte[1024];byte[] in = new byte[1024];String s = new String("hello yolo i am boss");//這里我可以按行讀取一個(gè)外部的文件outPip.write(s.getBytes());//向管道中寫(xiě)入數(shù)據(jù) outPip.close();/*** 第三步我需要使用過(guò)濾器對(duì)管道中的數(shù)據(jù)進(jìn)行切分* 1、先拿取管道中的數(shù)據(jù)* 2、進(jìn)行切分移位之后再放回管道中*/int len = 0;len=inPip.read(in);//從管道中拿取數(shù)據(jù)s = new String(in, 0, len);//得到字符串sinPip.close();//1、切分StringTokenizer tokener = new StringTokenizer(s," ");//2、進(jìn)行移位操作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<String> kwicList = new ArrayList<String>();//對(duì)ArrayList中的字進(jìn)行循環(huán)移位,得出最后結(jié)果 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++; }String line = linebuffer.toString(); kwicList.add(line); }/*** 3、移位之后再次寫(xiě)入到管道中,進(jìn)行下一個(gè)過(guò)濾器*/PipedInputStream inPip2;PipedOutputStream outPip2;/*** 管道相連*/inPip2= new PipedInputStream();outPip2 = new PipedOutputStream();try {inPip2.connect(outPip2);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}String s1="";for (int count1 = 0; count1 < kwicList.size(); count1++) {s1 =s1+kwicList.get (count1)+"\n";}System.out.println(s1);outPip2.write(s1.getBytes());outPip2.close();/*** 4、再次讀取管道中的數(shù)據(jù)、進(jìn)行首字母排序處理,按照換行符讀取形成字符串?dāng)?shù)組*/System.out.println("輸出in"+in[0]);len = inPip2.read(in);s = new String(in, 0, len);String []ss= s.split("\n").clone();ArrayList<String> list=new ArrayList<String>();for(String n:ss) {System.out.println("輸出"+n);}list.sort(null);for(int coun=0;coun<list.size();coun++){System.out.println(list.get(coun));}}}

第二版(對(duì)代碼進(jìn)行分離)
1、先是要向管道中放入數(shù)據(jù),所以創(chuàng)建一個(gè)mInput類。該類的方法Input(String filename)使用 BufferedReader bufread按行讀取文件的數(shù)據(jù),然后放入管道中pip.getOutPip().write(s.getBytes());。
mInput類

package com.ren.cn;import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;/*** * @author huihui* 按行讀取文件的數(shù)據(jù)并返回一個(gè)字符串的list*/ public class mInput {private BufferedReader bufread=null;private ArrayList<String> list=new ArrayList<String>();String s="";private Pipe pip=null;public mInput(Pipe pip) {this.pip=pip;}public void Input(String filename) {try {bufread= new BufferedReader(new FileReader(filename));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {while((s=bufread.readLine())!=null) { pip.getOutPip().write(s.getBytes());}bufread.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

2、將數(shù)據(jù)從管道中取出,進(jìn)行第一次的過(guò)濾,對(duì)數(shù)據(jù)進(jìn)行移位處理,從而形成一些移位后新的句子 Move類,詳細(xì)解釋看代碼中的注釋,移位完成之后,還要將數(shù)據(jù)再送回管道。
Move類

package com.ren.cn;import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.ArrayList; import java.util.StringTokenizer;/*** * @author huihui 這個(gè)類提供移位的方法*/ public class Move {byte[] out = new byte[1024];byte[] in = new byte[1024];int len = 0;String s = "";Pipe pip=null;public Move(Pipe pip) {this.pip=pip;}public void mymove() {try {len = pip.getInPip().read(in);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} // 從管道中拿取數(shù)據(jù)s = new String(in, 0, len);// 得到字符串s// 1、切分StringTokenizer tokener = new StringTokenizer(s, " ");// 2、進(jìn)行移位操作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<String> kwicList = new ArrayList<String>();// 對(duì)ArrayList中的字進(jìn)行循環(huán)移位,得出最后結(jié)果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++;}String line = linebuffer.toString();kwicList.add(line);}/*** 3、移位之后再次寫(xiě)入到管道2中,進(jìn)行下一個(gè)過(guò)濾器*/String s1 = "";for (int count1 = 0; count1 < kwicList.size(); count1++) {s1 = s1 + kwicList.get(count1) + "\n";}try {pip.getOutPip().write(s1.getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

3、再將數(shù)據(jù)從管道中取出,進(jìn)行首字母排序的處理,使用基于數(shù)組的容器ArrayList ,調(diào)用其方法sort進(jìn)行一鍵排序,將排好序的這些句子又重新放回管道。
InOrder類

package com.ren.cn;/*** * @author huihui*進(jìn)行排序并且再次寫(xiě)入到下一個(gè)管道中*/import java.io.IOException; import java.util.ArrayList;public class InOrder {private ArrayList<String> list = null;private byte[] in = new byte[1024];private String s1 = "";private Pipe pip=null;public InOrder(Pipe pip) {this.pip=pip;}public void inorder() {int len;try {len = pip.getInPip().read(in);String s = new String(in, 0, len);String[] ss = s.split("\n").clone();list = new ArrayList<String>();for (String n : ss) {list.add(n);}list.sort(null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}for (int count1 = 0; count1 < list.size(); count1++) {s1 = s1 + list.get(count1) + "\n";}try {pip.getOutPip().write(s1.getBytes());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

4、最后再?gòu)墓艿乐腥〕鎏幚砗玫臄?shù)據(jù),寫(xiě)入到新的文件中,詳細(xì)看代碼注釋
mOutput類

package com.ren.cn; /*** * @author huihui*將所得的結(jié)果放回到文件中*/import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList;public class mOutput {BufferedWriter writer=null;String s="";private Pipe pip;byte[] b = new byte[1024];public mOutput(Pipe pip) {this.pip=pip;}public void Output(String filename,ArrayList<String>list){try {writer =new BufferedWriter(new FileWriter(filename));int len = 0;len=pip.getInPip().read(b);//從管道中拿取數(shù)據(jù)s = new String(b, 0, len);writer.write(s);writer.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }}

5、當(dāng)然了使用管道,對(duì)管道中的數(shù)據(jù)進(jìn)行操作,怎么能沒(méi)有管道呢!!!管道類,專門(mén)負(fù)責(zé)創(chuàng)建管道
Pipe類

package com.ren.cn; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; /*** * @author huihui* 這是一個(gè)管道類,專門(mén)用于創(chuàng)建管道*/ public class Pipe {private PipedInputStream inPip;private PipedOutputStream outPip;public Pipe() {outPip=new PipedOutputStream();inPip= new PipedInputStream();try { inPip.connect(outPip);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public PipedInputStream getInPip() {return inPip;}public PipedOutputStream getOutPip() {return outPip;} }

6、在main方法中進(jìn)行測(cè)試

package com.ren.cn;import java.io.BufferedReader; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.ArrayList;public class test {/*** 進(jìn)行測(cè)試(這是最終版)* * @param args* @throws IOException*/public static void main(String[] args) {String out = "";Pipe pip=new Pipe(); BufferedReader list = null;ArrayList<String> outlist =new ArrayList<String>();String filename = "main.txt";//要讀取的文件String filenameout="test.txt";//最后寫(xiě)入的文件/*** 1、輸入文件*/mInput input = new mInput(pip);input.Input(filename);/*** 2、進(jìn)行移位*/Move mv = new Move(pip);mv.mymove();/*** 3、進(jìn)行排序*/InOrder inorder = new InOrder(pip);inorder.inorder();/*** 4、寫(xiě)出文件*/mOutput oput=new mOutput(pip);oput.Output(filenameout, outlist);/*** 5、關(guān)閉管道*/try {pip.getInPip().close();pip.getOutPip().close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

7、最后差點(diǎn)忘了,應(yīng)該貼上一張UML圖
8、使用eclipse,自動(dòng)生成類圖!!別著急走,繼續(xù)看
右擊項(xiàng)目,選擇new 選擇other
選擇這個(gè)

從而得到這樣一個(gè)文件

打開(kāi)這個(gè)newfile.cld文件然后選擇所有的java文件,往這個(gè)newfile.cld文件中拖拽

就自動(dòng)生成類圖了。
參考內(nèi)容:https://blog.csdn.net/it_man/article/details/5003836?locationNum=8

總結(jié)

以上是生活随笔為你收集整理的基于管道过滤器风格的-KWIC的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。