生活随笔
收集整理的這篇文章主要介紹了
基于管道过滤器风格的-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
;
public class mypip { 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
) { e
. printStackTrace ( ) ; } byte [ ] out
= new byte [ 1024 ] ; byte [ ] in
= new byte [ 1024 ] ; String s
= new String ( "hello yolo i am boss" ) ; outPip
. write ( s
. getBytes ( ) ) ; outPip
. close ( ) ; int len
= 0 ; len
= inPip
. read ( in
) ; s
= new String ( in
, 0 , len
) ; inPip
. close ( ) ; StringTokenizer tokener
= new StringTokenizer ( s
, " " ) ; String token
= new String ( ) ; int index
; ArrayList
< String> tokens
= new ArrayList < String> ( ) ; int count
= tokener
. countTokens ( ) ; for ( int j
= 0 ; j
< count
; j
++ ) { token
= tokener
. nextToken ( ) ; tokens
. add ( token
) ; } ArrayList
< String> kwicList
= new ArrayList < String> ( ) ; 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
) ; } PipedInputStream inPip2
; PipedOutputStream outPip2
; inPip2
= new PipedInputStream ( ) ; outPip2
= new PipedOutputStream ( ) ; try { inPip2
. connect ( outPip2
) ; } catch ( IOException e
) { e
. 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 ( ) ; 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
;
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
) { e
. printStackTrace ( ) ; } try { while ( ( s
= bufread
. readLine ( ) ) != null
) { pip
. getOutPip ( ) . write ( s
. getBytes ( ) ) ; } bufread
. close ( ) ; } catch ( IOException e
) { e
. 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
;
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
) { e
. printStackTrace ( ) ; } s
= new String ( in
, 0 , len
) ; StringTokenizer tokener
= new StringTokenizer ( s
, " " ) ; String token
= new String ( ) ; int index
; ArrayList
< String> tokens
= new ArrayList < String> ( ) ; int count
= tokener
. countTokens ( ) ; for ( int j
= 0 ; j
< count
; j
++ ) { token
= tokener
. nextToken ( ) ; tokens
. add ( token
) ; } ArrayList
< String> kwicList
= new ArrayList < String> ( ) ; 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
) ; } 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
) { e
. printStackTrace ( ) ; } } }
3、再將數(shù)據(jù)從管道中取出,進(jìn)行首字母排序的處理,使用基于數(shù)組的容器ArrayList ,調(diào)用其方法sort進(jìn)行一鍵排序,將排好序的這些句子又重新放回管道。 InOrder類
package com
. ren
. cn
; 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
) { e
. printStackTrace ( ) ; } for ( int count1
= 0 ; count1
< list
. size ( ) ; count1
++ ) { s1
= s1
+ list
. get ( count1
) + "\n" ; } try { pip
. getOutPip ( ) . write ( s1
. getBytes ( ) ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; } }
}
4、最后再?gòu)墓艿乐腥〕鎏幚砗玫臄?shù)據(jù),寫(xiě)入到新的文件中,詳細(xì)看代碼注釋 mOutput類
package com
. ren
. cn
;
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
) ; s
= new String ( b
, 0 , len
) ; writer
. write ( s
) ; writer
. close ( ) ; } catch ( IOException e
) { e
. 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
;
public class Pipe { private PipedInputStream inPip
; private PipedOutputStream outPip
; public Pipe ( ) { outPip
= new PipedOutputStream ( ) ; inPip
= new PipedInputStream ( ) ; try { inPip
. connect ( outPip
) ; } catch ( IOException e
) { e
. 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 { 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" ; mInput input
= new mInput ( pip
) ; input
. Input ( filename
) ; Move mv
= new Move ( pip
) ; mv
. mymove ( ) ; InOrder inorder
= new InOrder ( pip
) ; inorder
. inorder ( ) ; mOutput oput
= new mOutput ( pip
) ; oput
. Output ( filenameout
, outlist
) ; try { pip
. getInPip ( ) . close ( ) ; pip
. getOutPip ( ) . close ( ) ; } catch ( IOException e
) { e
. 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ò),歡迎將生活随笔 推薦給好友。