“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統嘗試
阿新 • • 發佈:2017-11-19
運行結果截圖 lob break 依據 關鍵字 () 命令行 int 重復
一、實驗目的
1.理解不同體系結構風格的具體內涵。
2.學習體系結構風格的具體實踐。
二、實驗環境
硬件: (依據具體情況填寫)
軟件:Java或任何一種自己熟悉的語言
三、實驗內容
“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統接受有序的行集合:每一行是單詞的有序集合;每一個單詞又是字母的有序集合。通過重復地刪除航中第一個單詞,並把它插入行尾,每一行可以被“循環地移動”。KWIC檢索系統以字母表的順序輸出一個所有行循環移動的列表。
嘗試用不同的策略實現這個系統。選擇2-3種體系結構風格來實現。
四、實驗步驟:
要求寫具體實現代碼,並根據實際程序,畫出程序的總體體系結構圖和算法結構圖,以及運行結果截圖。
①采用主/子程序的風格
a.體系結構圖
b.簡述體系結構各部件的主要功能,實現思想。
文本輸入:通過命令行提示輸入文本
循環移位:把輸入的文本進行循環位移
輸出位移過程:將位移的每個過程結果輸出
排序:對移位後的結果進行排序
輸出:輸出排序後的結果
c.代碼:
<?php //定義全局共享變量 $data = []; $all = []; $index = 0; $yield = null; /** * * 行輸入 * **/ function input() { global $data; while(1){ $string = trim(fgets(STDIN)); //按ctrl+p 結束輸入 if(16 === ord($string)){ break; } array_push($data,$string); } return; } /** * * 記錄位移過程,累加到到叠代器 * **/ function shifter() { global $data; global $index; while($index<count($data)){ $tempArr= explode(‘ ‘,$data[$index]); $count = count($tempArr); yield implode(‘ ‘,$tempArr)."\n"; for($i=0;$i<$count-1;++$i){ $shift = array_shift($tempArr); array_push($tempArr,$shift); yield implode(‘ ‘,$tempArr)."\n"; } $index+=1; } } /** * * 位移過程輸出 * **/ function pcOutput() { global $yield; $collector = []; foreach($yield as $k=>$v){ array_push($collector,$v); echo $v; } return $collector; } /** * * 按字母排序 * **/ function alphabetizer() { //使用全局變量 global $all; //排序 return natcasesort($all); } /** * * 排序結果輸出 * **/ function output() { global $all; foreach($all as $v) { print($v); } } /** * * 主函數 * **/ function main() { global $yield; global $all; print("----------input---------\n"); //輸入 input(); //循環位移 $yield = shifter(); //過程輸出 print("\n---------process--------\n"); $all = pcOutput(); //按字母表排序 alphabetizer(); //排序結果輸出 print("\n----------output---------\n"); output(); } main(); ?>
d.結果
②采用管道過濾器的風格
a.體系結構圖
b.簡述體系結構各部件的主要功能,實現思想。
文本輸入:通過命令行提示輸入文本,並經過輸入過濾器,轉換成字符數組
循環移位:把,轉換成的字符數組,通過循環移位過濾器,生成移位過程行文本
排序:對移位後的結果進行排序
輸出:輸出排序後的結果
c.代碼
1 <?php 2 /** 3 * 4 * 過濾器 5 * 6 **/ 7 abstract class Filter{ 8 protected abstract function trans(); 9 } 10 /** 11 * 12 * 13 * 管道類 14 * 15 **/ 16 class Pipe{ 17 private $inputPipe = []; 18 private $outputPipe = []; 19 private function writeLine(){ 20 $input = new Input(); 21 print("--------input---------\n"); 22 while(1){ 23 $buffer = $input->input(); 24 if($buffer === false) 25 break; 26 array_push($this->inputPipe,$buffer); 27 } 28 return $this->inputPipe; 29 } 30 private function readLine(){ 31 $CircleShift = new CircleShift($this->inputPipe); 32 $outputPipe = $CircleShift->trans(); 33 $Alpha = new Alphabetizer($outputPipe); 34 $outputPipe = $Alpha->trans(); 35 return $outputPipe; 36 } 37 public function start(){ 38 return $this->writeLine(); 39 } 40 public function stop(){ 41 $this->outputPipe = $this->readLine(); 42 $output = new Output($this->outputPipe); 43 $output->trans(); 44 } 45 } 46 47 /** 48 * 49 * 輸入,並將輸入過濾生成數組 50 * 51 **/ 52 class Input extends Filter{ 53 private $input = ‘‘; 54 public function input(){ 55 $this->input = trim(fgets(STDIN)); 56 if(ord($this->input) === 16){ 57 return false; 58 } 59 return $this->trans(); 60 } 61 62 public function trans(){ 63 return explode(‘ ‘,$this->input); 64 } 65 } 66 /** 67 * 68 * 輸出 69 * 70 **/ 71 class Output extends Filter{ 72 private $output = null; 73 function __construct($output){ 74 $this->output = $output; 75 } 76 function trans(){ 77 foreach($this->output as $k=>$v){ 78 print($v); 79 } 80 } 81 } 82 /** 83 * 84 * 循環移位 85 * 86 **/ 87 class CircleShift extends Filter{ 88 private $input = null; 89 private $output = []; 90 private $index = 0; 91 function __construct($input){ 92 $this->input = $input; 93 } 94 //循環移動單詞的位置 95 public function trans(){ 96 97 foreach($this->input as $value){ 98 $count = count($value); 99 array_push($this->output,implode(‘ ‘,$value)."\n"); 100 for($i=0;$i<$count-1;++$i){ 101 $shift = array_shift($value); 102 array_push($value,$shift); 103 array_push($this->output,implode(‘ ‘,$value)."\n"); 104 } 105 } 106 107 return $this->output; 108 } 109 } 110 /** 111 * 112 * 按照字母排序 113 * 114 **/ 115 class Alphabetizer extends Filter{ 116 private $data = null; 117 function __construct($data){ 118 $this->data = $data; 119 } 120 public function trans(){ 121 natcasesort($this->data); 122 return $this->data; 123 } 124 } 125 class KWIC{ 126 public function main(){ 127 //實例化管道 128 $Pipe = new Pipe(); 129 $input = $Pipe->start(); 130 $output = $Pipe->stop(); 131 } 132 } 133 134 $kwic = new KWIC(); 135 $kwic->main(); 136 ?>
d.結果
“上下文關鍵字”KWIC(Key Word in Context,文本中的關鍵字)檢索系統嘗試