1、实验目的java
1.理解不一样体系结构风格的具体内涵。学习
2.学习体系结构风格的具体实践。this
2、实验环境spa
硬件: (依据具体状况填写)code
软件:Java或任何一种本身熟悉的语言对象
3、实验内容blog
“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每个单词又是字母的有序集合。经过重复地删除航中第一个单词,并把它插入行尾,每一行能够被“循环地移动”。KWIC检索系统以字母表的顺序输出一个全部行循环移动的列表。排序
尝试用不一样的策略实现这个系统。选择2-3种体系结构风格来实现。get
4、实验步骤:input
KWIC 的主/子程序体系结构风格示意图以下所示:
KWIC 主程序/子程序风格的 Java 语言实现:
1 public static void alphabetize(){ 2 String[] tmpArray = new String[shiftedLineIndexes.size()]; 3 shiftedLineIndexes.toArray(tmpArray); Arrays.sort(tmpArray); sortedLineIndexes=tmpArray; 4 } 5 public static void Output(String outputAddress){ FileWriter fw = null; 6 try { 7 fw = new FileWriter(outputAddress); 8 } catch (IOException e) { 9 // TODO Auto-generated catch block e.printStackTrace(); 10 } 11 BufferedWriter bw=new BufferedWriter(fw); 12 for(int i=0;i<sortedLineIndexes.length;i++){ 13 try { 14 bw.write(sortedLineIndexes[i]); bw.newLine(); 15 16 } catch (IOException e) { 17 // TODO Auto-generated catch block e.printStackTrace(); 18 } 19 } try { 20 bw.close(); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block e.printStackTrace(); 23 } 24 } 25 public static void Input(String iFile){ FileReader fr=null; try { fr=new FileReader(iFile); 26 27 } catch (FileNotFoundException e) { e.printStackTrace(); 28 } 29 BufferedReader br=new BufferedReader(fr); textLines=new ArrayList<String>(); 30 try { while(br.ready()){ 31 textLines.add(br.readLine()); 32 } 33 } catch (IOException e) { e.printStackTrace(); 34 } 35 } 36 public static void CircularShift(){ shiftedLineIndexes=new ArrayList<String>(); for(int i=0;i<textLines.size();i++){ String orinLine=textLines.get(i); String sarray[]=orinLine.split(" "); for(int j=0;j<sarray.length;j++){ String newLine=sarray[j]; if(sarray.length>1){ 37 if(j==sarray.length-1){ 38 39 for(int k=0;k<(sarray.length-1);k++){ 40 newLine=newLine+" "+sarray[k]; 41 } 42 } 43 else{ 44 for(int k=j+1;k<sarray.length;k++){ 45 newLine=newLine+" "+sarray[k]; 46 } 47 48 for(int m=0;m<j;m++){ newLine=newLine+" "+sarray[m]; 49 } 50 } 51 } 52 shiftedLineIndexes.add(newLine); 53 } } }
KWIC 的面向对象程序体系结构风格示意图以下所示:
基本的计算模型:
Input 模块从文本文件 input.txt 中一行一行读取单
Shift 模块用于将单词移位。 Sort 模块将单词进行排序。
Output 模块将最终结果写到文本文件 output.txt 中
KWIC 面向对象风格的 Java 语言实现:
1 //InputStore.java import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class InputStore { public ArrayList<String> ls; 2 public InputStore(ArrayList<String> ls){ this.ls=ls; 3 } 4 public void input(String inputFile){ FileReader fr=null; try { 5 fr=new FileReader(inputFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); 6 } 7 BufferedReader br=new BufferedReader(fr); try { while(br.ready()){ 8 ls.add(br.readLine()); 9 } 10 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); 11 } 12 } 13 } //Output.java import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; public class Output { public ArrayList<String> ls; 14 public Output(ArrayList<String> ls){ this.ls=ls; 15 } 16 public void output(String outputAddress){ 17 18 FileWriter fw = null; try { 19 fw = new FileWriter(outputAddress); 20 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); 21 } 22 BufferedWriter bw=new BufferedWriter(fw); 23 24 for(int i=0;i<ls.size();i++){ 25 26 try { 27 bw.write(ls.get(i)); bw.newLine(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block e.printStackTrace(); 30 } 31 } try { 32 bw.close(); 33 } catch (IOException e) { 34 // TODO Auto-generated catch block e.printStackTrace(); 35 } 36 } 37 38 } 39 40 //Alphabetizer.java import java.util.ArrayList; import java.util.Arrays; public class Alphabetizer { public ArrayList<String> ls; public Alphabetizer(ArrayList<String> ls){ this.ls=ls; 41 } 42 43 public void alpha(){ 44 45 String[] tmpArray = new String[ls.size()]; 46 ls.toArray(tmpArray); 47 Arrays.sort(tmpArray); for(int i=0;i<ls.size();i++){ ls.set(i, tmpArray[i]); 48 } 49 } 50 } 51 52 //CircularShifter.java import java.util.ArrayList; public class CircularShifter { public ArrayList<String> ls; public CircularShifter(ArrayList<String> ls){ this.ls=ls; 53 } 54 public void shift(){ 55 ArrayList<String> shiftedLineIndexes=new ArrayList<String>(); for(int i=0;i<ls.size();i++){ 56 String orinLine=ls.get(i); String sarray[]=orinLine.split(" "); for(int j=0;j<sarray.length;j++){ String newLine=sarray[j]; if(sarray.length>1){ if(j==sarray.length-1){ for(int k=0;k<(sarray.length-1);k++){ 57 newLine=newLine+" "+sarray[k]; 58 } 59 } 60 else{ 61 for(int k=j+1;k<sarray.length;k++){ 62 newLine=newLine+" "+sarray[k]; 63 } 64 65 for(int m=0;m<j;m++){ newLine=newLine+" "+sarray[m]; 66 } 67 } 68 } 69 shiftedLineIndexes.add(newLine); 70 } 71 } 72 ls=shiftedLineIndexes; 73 } 74 } 75 //Main.java import java.util.ArrayList; public class Main { 76 public static void main(String[] args) { 77 // TODO Auto-generated method stub 78 ArrayList<String>ls=new ArrayList<String>(); 79 80 InputStore inputStore=new InputStore(ls); 81 inputStore.input("input.txt"); 82 83 CircularShifter cs=new CircularShifter(ls); 84 cs.shift(); 85 86 Alphabetizer alp=new Alphabetizer(cs.ls); 87 alp.alpha(); 88 89 Output output=new Output(alp.ls); output.output("output.txt"); 90 } 91 } 92 93 94