1. 程式人生 > >資料結構(嚴蔚敏)第四章 串 例項——索引表

資料結構(嚴蔚敏)第四章 串 例項——索引表

package index;

import java.io.*;
import java.util.*;

class IdxTermType{  // 索引項型別
    StringBuffer key = new StringBuffer();    // 關鍵詞
    LinkedList<StringBuffer> bnolist = new LinkedList<StringBuffer>();  // 存放書號索引的連結串列
}
class Ret{
    boolean b = false;
    int lct = 0;
}

//class IdxListType extends define{  // 索引表型別(有序表)��
// IdxTermType item[] = new IdxTermType[9]; // 包含索引項� // int last; // 索引表長度 //} //class WordListType extends LinkedList{ // 詞表型別(順序表),存放一本書的書名中若干關鍵詞 // String item; //} public class index extends define{ // static IdxListType idxlist = new IdxListType(); // 定義索引表 public static LinkedList<IdxTermType> idxlist = new
LinkedList<IdxTermType>(); public static void ExtractKeyWord( LinkedList<String> wdlist,String line,StringBuffer BookNo){ // 從 line 中提取關鍵詞到詞表,書號存入 BookNo String arrs[] = line.split(" "); // 將 line 以空格為斷點分為數個子字串(將句子轉化為多個單詞) for(int i=0; i<arrs.length; i++){ int
ucount = 0; for(int j=0; j<usual.length; j++) if((arrs[i].equalsIgnoreCase(usual[j]))) // 如果 arrs[i] 不屬於常用詞彙 ucount ++; if(ucount == 0){ for(int k=0; k<arrs[i].length(); k++){ char c = arrs[i].charAt(k); if(c<'0' || c>'9'){ // 如果 arrs[i] 不是書號,則為關鍵詞,插入詞表wdlist,書號存入 BookNo if(!CheckRpt(wdlist,arrs[i])) wdlist.add(arrs[i]); // 插入詞表 if(BookNo.length() == 0) // 如果書號為空,則存入 BookNo.append(arrs[0]); break; } } } } } public static boolean CheckRpt(LinkedList<String> wdlist,String wd){ //判斷 詞表 wdlist 中是否已有 wd,無返回為 false ,有返回為 true boolean jd = false; for(int i=0; i<wdlist.size();i++){ if(wd.equalsIgnoreCase(wdlist.get(i))) // 判斷是否相等,不區分大小寫 jd = true; else jd = false; } return jd; } public static void InsIdxList( LinkedList<String> wdlist,String line,StringBuffer BookNo){ String wd; Ret j = new Ret(); for(int i=0; i<wdlist.size(); ++i){ wd = wdlist.get(i); Locate(j,wd); if(!j.b) InsertNewKey(j.lct,wd); InsertBook(j.lct,BookNo); } } public static void InsertBook(int i,StringBuffer BookNo){ // 在索引表的第 i 項中插入書號為 BookNo 的索引 // 定義 j 為即將插入書號索引連結串列的位置 if(idxlist.get(i).bnolist.get(0).toString().equals(" ")) idxlist.get(i).bnolist.set(0, BookNo); else idxlist.get(i).bnolist.add(BookNo); } public static void InsertNewKey(int i,String wd){ // 在索引表 idxlist 的第 i 項上插入新關鍵詞 wd,並初始化書號索引的連結串列為空 // if(idxlist.last == 0){ // 如果 idxlist 為空,則將 wd 直接插入 // idxlist.item[0].key = wd; // idxlist.item[0].bnolist = null; // ++idxlist.last; // } StringBuffer bno = new StringBuffer(); bno.append(" "); IdxTermType elem = new IdxTermType(); // 定義即將插入的索引元素 elem.key.append(wd); elem.bnolist.add(bno); if(i == 0){ //idxlist.get(0).key = wd; //idxlist.get(i).key = wd; idxlist.add(elem); } else{ // for(int j=idxlist.size()-1; j>=i; --j) { // 後移索引項 ?為什麼後移索引項? // idxlist.get(j+1).key = idxlist.get(j).key; // //插入新的索引項 // //idxlist.get(i).key = wd; // 將 wd 插入 idxlist 的第 i 項 // //idxlist.get(i).bnolist = null; // 初始化書號索引表為空 // idxlist.add(elem); // } idxlist.add(i, elem); } } // ??? public static void Locate(Ret j,String wd){ // 在索引表 idxlist 中查詢是否存在與 wd 相等的關鍵詞。若存在, // 則返回其在索引表中的位置,且 b 取值true,否則返回插入位置, // 且 b 取值false // 定義函式的返回值 // for(int i=idxlist.size()-1;i>=0;--i){ // 將索引表中的關鍵詞與 wd 按字典順序比較 // int m = idxlist.get(i).key.toString().compareTo(wd); // if(m == 0){ // 如果索引表中已存在 wd // b = true; // lct = i; // } // else{ // 否則 // b = false; // lct = i+1; // } // } for(int i=0; i<idxlist.size(); i++){ int m = idxlist.get(i).key.toString().compareTo(wd); if(m == 0){ j.b = true; j.lct = i; break; } else{ j.b = false; j.lct = i+1; } } } public static void main(String[] args) throws IOException{ int c1;int c2; try{ FileInputStream fin1 = new FileInputStream("Book/Bookinfo.txt"); // 定義 Bookinfo 輸入流物件 FileInputStream fin2 = new FileInputStream("Book/BookIdx.txt"); // 定義 BookIdx 輸入流物件 // FileOutputStream fout = new FileOutputStream("Book/BookIdx.txt"); //定義 BookIdx 輸出流物件 FileReader fr = new FileReader("Book/Bookinfo.txt"); // 定義 Bookinfo 讀取物件 FileWriter fw = new FileWriter("Book/BookIdx.txt"); // 定義 BookIdx 寫入物件 BufferedReader br = new BufferedReader(fr); // 讀取 Bookinfo 的緩衝區 BufferedWriter bw = new BufferedWriter(fw); // 寫入 BookIdx 的緩衝區 PrintWriter out = new PrintWriter(bw); // 格式化寫入過程 String line = ""; // 從 Bookinfo 中讀取的一行書目 while((line=br.readLine())!=null){ LinkedList<String> wdlist = new LinkedList<String>(); // 詞表 StringBuffer BookNo = new StringBuffer(); // 書號 ExtractKeyWord(wdlist,line,BookNo); // 從 line 中提取關鍵詞到詞表,書號存入 BookNo InsIdxList(wdlist,line,BookNo); // 將書號為 BookNo 的關鍵詞插入索引表 //out.println(idxlist); // 將生成的索引表 idxlist 輸出到檔案 BookIdx } while((c1=fin1.read())!=-1){ // 測試 Bookinfo 內容 System.out.print((char)c1); } fin1.close(); // 關閉輸入流 String s1[] = new String[10]; String s2[][] = new String[10][10]; for(int i=0; i<idxlist.size(); i++){ s1[i] = idxlist.get(i).key.toString(); for(int j=0; j<idxlist.get(i).bnolist.size(); j++) s2[i][j] = idxlist.get(i).bnolist.get(j).toString(); } for(int i=0; i<10; i++){ if(s1[i] != null) out.print(s1[i]+" "); for(int j=0; j<10; j++) if(s2[i][j] != null) out.print(s2[i][j]+" "); out.println(); } // for(ListIterator it1 = idxlist.listIterator();it1.hasNext();){ // for(int i=) // s = (String) it1.next(); // out.println(s); // } out.close(); while((c2=fin2.read())!=-1){ //測試 BookIdx 內容 System.out.print((char)c2); } fin2.close(); }catch (FileNotFoundException e){System.err.println(e);} catch (IOException e){System.err.println(e);} } }