一、实验目的
1.理解不同体系结构风格的具体内涵。
2.学习体系结构风格的具体实践。
二、实验环境
硬件: (依据具体情况填写)
软件:Java或任何一种自己熟悉的语言
三、实验内容
“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合;每一个单词又是字母的有序集合。通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被“循环地移动”。KWIC检索系统以字母表的顺序输出一个所有行循环移动的列表。
尝试用不同的策略实现这个系统。选择2-3种体系结构风格来实现。
四、实验步骤:
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; public class KWIC { private static ArrayList<String> CircularShift(ArrayList<String> lines) { ArrayList<String> tmp_lines = new ArrayList<String>(); for (String i : lines) { CircularShiftLine(i, tmp_lines); } return tmp_lines; } private static void CircularShiftLine(String curLine, ArrayList<String> tmp_lines) { String[] words = curLine.split(" +|\t+"); for (int i = 0; i < words.length; i++) { if (noiseWords.indexOf((words[i] + " ").toLowerCase()) != -1) continue; String shift = ""; for (int j = i; j < (words.length + i); j++) { shift += words[j % words.length]; if (j < (words.length + i - 1)) shift += " "; } tmp_lines.add(shift); } return; } private static ArrayList<String> Alphabetizer(ArrayList<String> lines) { ArrayList<String> tmp_lines = new ArrayList<String>(); for (String i : lines) { i = convertString(i); tmp_lines.add(i); } Collections.sort(tmp_lines); lines.clear(); for (String i : tmp_lines) { i = convertString(i); lines.add(i); } return lines; } static String convertString(String str) { String upStr = str.toUpperCase(); String lowStr = str.toLowerCase(); StringBuffer buf = new StringBuffer(str.length()); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == upStr.charAt(i)) { buf.append(lowStr.charAt(i)); } else { buf.append(upStr.charAt(i)); } } return buf.toString(); } private static ArrayList<String> input(String string) throws IOException { DataInputStream in = new DataInputStream(new FileInputStream(string)); ArrayList<String> lines = new ArrayList<String>(); String curLine = in.readLine(); while (curLine != null) { lines.add(curLine); curLine = in.readLine(); } in.close(); return lines; } static void output(ArrayList<String> lines, String output) throws FileNotFoundException { PrintWriter fileWriter = new PrintWriter(output); for (String i : lines) fileWriter.println(i); fileWriter.close(); } static void output(ArrayList<String> lines) { for (String i : lines) System.out.println(i); } }
(2)隐式调用
import java.io.CharArrayWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; public class Alphabetizer extends Filter { public Alphabetizer(Pipe input, Pipe output) { super(input, output); middleChange(); } protected void middleChange() { ArrayList<String> lines = new ArrayList<String>(); CharArrayWriter writer = new CharArrayWriter(); try { int c = -1; while ((c = input.read()) != -1) { writer.write(c); if (c == 10) { String curLine = writer.toString(); lines.add(curLine); writer.reset(); } } alpha(lines); for (String s : lines) { output.write(s); } input.closeReader(); output.closeWriter(); } catch (IOException e) { e.printStackTrace(); } } private static ArrayList<String> alpha(ArrayList<String> lines) { ArrayList<String> tmp_lines = new ArrayList<String>(); for (String i : lines) { i = convertString(i); tmp_lines.add(i); } Collections.sort(tmp_lines); lines.clear(); for (String i : tmp_lines) { i = convertString(i); lines.add(i); } return lines; } static String convertString(String str) { String upStr = str.toUpperCase(); String lowStr = str.toLowerCase(); StringBuffer buf = new StringBuffer(str.length()); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == upStr.charAt(i)) { buf.append(lowStr.charAt(i)); } else { buf.append(upStr.charAt(i)); } } return buf.toString(); } }
结果图
时间: 2024-11-09 10:33:04