如何構(gòu)建搜索引擎_第1頁
如何構(gòu)建搜索引擎_第2頁
如何構(gòu)建搜索引擎_第3頁
如何構(gòu)建搜索引擎_第4頁
如何構(gòu)建搜索引擎_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、如何構(gòu)建搜索引擎思路:網(wǎng)頁采集(網(wǎng)絡(luò)爬蟲) 網(wǎng)頁分析 網(wǎng)頁索引 網(wǎng)頁檢索 索引創(chuàng)建1. 網(wǎng)絡(luò)爬蟲從指定的網(wǎng)址或IP下載網(wǎng)頁,保存到本地臨時(shí)文件。package com.yjb.a;import java.io.BufferedReader;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;import .UnknownHostException;pub

2、lic class WebCrawler private static String Text_File_Path = "D:Project_2010chhtmlsrc.html"public static void main(String args) tryFile file = new File(Text_File_Path);/ 文件可以沒有,但文件夾一定要有FileWriter fpWriter = new FileWriter(file);Socket webclient = new Socket("",80);PrintWriter resu

3、lt = new PrintWriter(webclient.getOutputStream(),true);BufferedReader receiver = new BufferedReader(new InputStreamReader(webclient.getInputStream();result.println("GET / HTTP/1.1");result.println("Host:");result.println("Connection:Close");result.println();boolean bRet

4、 = true;StringBuffer sb = new StringBuffer(8096);while(bRet)if(receiver.ready()int idx = 0;while(idx != -1)idx = receiver.read();if(idx = '<')break;while(idx != -1)sb.append(char)idx);idx = receiver.read();bRet = false;System.out.println(sb.toString(); fpWriter.write(sb.toString(); webcli

5、ent.close(); fpWriter.close();catch(UnknownHostException e)System.out.println(" 無法訪問指定主機(jī) ");System.exit(1);catch(IOException e)System.out.println(" 下載失敗,請檢查輸入的地址是否正確 "); System.exit(1);2. 網(wǎng)頁分析程序讀取本地臨時(shí)文件,通過篩選器篩選,保留與關(guān)鍵字匹配的內(nèi)容,刪掉無 用內(nèi)容。package com.yjb.a;import java.io.File;import java

6、.io.FileReader;import java.io.FileWriter;import java.io.IOException;import .UnknownHostException;public class WebParserFilter private static String src_File_Path = "D:Project_2010chhtmlsrc.html" private static String dst_File_Path = "D:Project_2010chpuresrcfilter.txt" public stat

7、ic void main(String args) tryParserFilter();catch(IOException e)System.out.println(" 下載失敗,請檢查輸入的地址是否正確 "); System.exit(1);public static void ParserFilter() throws IOExceptiontryinf j H o八 boo-ean bf-ag Hfrup boo-ean bconCDnf HfrupSiring Buffer sBuffer H new Siring Buffer(8096*2)八 char-1 eB

8、uffer H new char-8096*2一八 char= dssuffer H new char-8096*2一八 inf nCounf H 0-Fi-e srcfi-e H new Fi-e(srclFi-elpafh)八 Fi-eReader fpReader H new Fi-eReader(srcfi-e)八 Fi-e dsffi-e H new Fi-e-dsflFi-elpafh)八 Fi-ewriCDr fpwriCDr H new Fi-ewriCDr(dsffi-e)八 nCounf H fpReader.read(CBUffer)八 for(inf i H 0= A

9、ncounf.+)宀 if(bconCDnf HH fa-sex ifoBuffer 三 HH -V-)宀 bconCDnf Hfrupose 宀 continue-)e-se 宀 ifoBuffer 三 HH A)宀bconCDnf H fa-spcontinue-ose ifoBuffer三 HH 三-CBUffesHH - - - CBU=fter三 HH - -CBUffemTH-50/ 回<福continue-ose ifoBuffer三 HH g-QOQOCBUfferT二 HH -n-QOQOCBUffer=+21 HH -b-奚 CBUffer=+3 一 HH -s-奚

10、CBUfferT4 一 HH -p-奚 CBUffer=+5 一 HHi H i + 5八continue-ds60ufferu+上 H eBuffer 三八bf-ag H frupfor(inf 3 H A j-m +)宀if(ds60uffer-ml AH -N-QOQOds60uffer-m;u -A-)=(dssufferm AH -N-QOQOds60uffer-ml AH -a-)=(dssufferm AH 9QOQOds5uffer-m;Ho-)宀if(bflag HH fa-sexSBUffer.appendz)八s B uffe r a p pe nd (dssuffe r

11、m)八elseif(dstBufferm = ' 、 ' | dstBufferm = '|' | dstBufferm = '"' | dstBufferm = ':'| dstBufferm = '' | dstBufferm = '.')sBuffer.append(' ');continue;if(bflag = true) sBuffer.append(' ');sBuffer.append(dstBufferm); sBuffer.append(

12、' ');bflag = false;System.out.println(sBuffer.toString(); fpWriter.write(sBuffer.toString();fpReader.close();fpWriter.close();catch(UnknownHostException e) System.out.println(" 無法指定主機(jī) ");System.exit(1);catch (IOException e) throw e;3. 網(wǎng)頁索引根據(jù)篩選器篩選過的文件來創(chuàng)建鏈表格式的索引。package com.yjb.a;/ 網(wǎng)

13、頁索引程序public class InfoItem publicint fileIDpublicint offsetInfoItem next ;public InfoItem() fileID= 0;offset = 0; next = nullpublic InfoItem( fileID = id;int id, int pos)offset = pos; n ext =n ull ;public voidset_ next(l nfoltem param)n ext = param;public int get_id()return fileID ;public int get_po

14、s()return offset ;publicIn foItem get_ next()returnnext ;4. 網(wǎng)頁檢索程序是在索引的基礎(chǔ)上來檢索的,它將返回你要檢索的信息。 package com.yjb.a;import java.util.Hashtable;/網(wǎng)頁檢索程序public class Wordln dex static Hashtable keywordIdx ;static Stri ngFileList = "北京師范大學(xué)“,"北師大附屬實(shí)驗(yàn)小學(xué)”,"北師大第二附屬中學(xué)“;public static voidmain( Stri n

15、g args) try in dex ();search ("北“); catch (Exception e)System. out .println(”索引檢索失敗,請檢查");System. exit (1);public static void search(Stri ng keyword)throws Excepti onIn foItem item;System. out .println("search:-begin-");if ( null= keywordldx )return ; try item = (I nfoItem)keywor

16、dIdx.get(keyword);/獲取關(guān)鍵字while (item !=n ull )System.out.pr in tl n("search:File n umber:"+item.get_id();System.out.pr in tl n("search:File offset:"+item.get_pos();System.out.pr in tl n("search:File Conten t:"+FileList item.get_pos();item = item.get_ next(); catch (Excep

17、tion e)throw e;System. out .println("search:-end-");throwsExcepti onpublic static voidin dex()In foItem item,item2;System. out .println("in dex:-begi n-");keywordIdx = new Hashtable();try System. out .println("index:Hash Table initial Size:"keywordIdx .size();for ( int

18、i = 0;i < 3;i +)int len = FileListi .len gth();for (int j = 0;j < len;j +)item = new In foltem(i,i);Stri ng key =FileList i.substri ng(j,j+1);System. out .print(key);if (! keywordIdx.containsKey(key)keywordIdx .put(key, item); else item2 = (In foItem)keywordIdx .get(key);item.set_ next(item2);keywordIdx .put(key, item);System. out .println( "" );System. out .println( "index:Hash Table f

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論