130242014074 林澤民 第3次實驗
阿新 • • 發佈:2017-11-18
images uppercase 實踐 [] exce 體系 lower spl loser
一、實驗目的
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(); } }
結果圖
130242014074 林澤民 第3次實驗