![簡單搜索引擎設計和 Java 源代碼_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/13/4228b559-7aca-4eeb-b375-e0b89071ac13/4228b559-7aca-4eeb-b375-e0b89071ac131.gif)
![簡單搜索引擎設計和 Java 源代碼_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/13/4228b559-7aca-4eeb-b375-e0b89071ac13/4228b559-7aca-4eeb-b375-e0b89071ac132.gif)
![簡單搜索引擎設計和 Java 源代碼_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/13/4228b559-7aca-4eeb-b375-e0b89071ac13/4228b559-7aca-4eeb-b375-e0b89071ac133.gif)
![簡單搜索引擎設計和 Java 源代碼_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/13/4228b559-7aca-4eeb-b375-e0b89071ac13/4228b559-7aca-4eeb-b375-e0b89071ac134.gif)
![簡單搜索引擎設計和 Java 源代碼_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/13/4228b559-7aca-4eeb-b375-e0b89071ac13/4228b559-7aca-4eeb-b375-e0b89071ac135.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、A simple search engine with the following features:Included in the package: source code: SimpleSearchEngine.java SimpleSearchEngineImpl.java SimpleSearchEngineTest.java readme: this file stopWords: the stop word file searchFiles/: a directory that contains a bunch of test files Usage: 1. SimpleSearc
2、hEngineTest.java can be modified to add more documents and add new queries. 1. compile the code 2. To run: java -cp . SimpleSearchEngineTest Features: 1. build inverted index for terms in documents and store in an index file. The index will be updated as more documents are added. And the index is lo
3、aded into memory during startup 2. examine stop words 3. simple query by splitting the query string into words and returning the list of the names of documents with one or more words in them 4. simple ranking of the search result based on the number of search words in the documents Preparation: 1. a
4、 document folder where all the documents resides, assuming searchFiles/ in the test. 2. the path of the index file. An index file has the inverted index of term mapped to a list of doc Ids. This index will be updated and the file will be updated as documents are added. 3. the path of a document name
5、 index file. This file has the docId to docName mapping. This file will be updated as documents are added.4. a stop word file with the stop words. An example is given.SimpleSearchEngine.javaimport java.util.List;/* * A simple search engine * * */public interface SimpleSearchEngine /* * simple query
6、by splitting the query into search terms and looking up the index, * ranking results by the number of search terms appearing in a document * * return list of document names * * */public List<String> query(String queryStr);/* * add a document and update the index * * param docName document name
7、 */public void addDoc(String docName);SimpleSearchEngineImpl.javaimport java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Comparator;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;im
8、port java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeMap;import java.util.TreeSet;/* * A simple search engine with the following features: * * 1. build inverted index for terms in documents and store in an index file. The index will be updated as more documents are adde
9、d. * And the index is loaded into memory during startup * 2. examine stop words * 3. simple query by splitting the query string into words and returning the list of the names of documents with one or more words in them * 4. simple ranking of the search result based on the number of search words in t
10、he documents * * * Preparation: * 1. a document folder where all the documents resides * 2. path of the index file. An index file has the inverted index of term mapped to a list of doc Ids. This index will be updated and * the file will be updated as documents are added. * 3. path of a document name
11、 index file. This file has the docId to docName mapping. This file will be updated as documents are added. * 4. a stop word file with the stop words * * author dennis li * */public class SimpleSearchEngineImpl implements SimpleSearchEngine private String docFolderPath = null;private String indexFile
12、Path = null;private String docNameIndexPath = null;private String stopWordsFilePath = null;/ search index map, mapping words to a set of document Idsprivate Map<String, Set<Integer>> searchIndex = new HashMap<String, Set<Integer>>();/ doc name index map, mapping the docId to
13、docNameprivate Map<Integer, String> docNames = new HashMap<Integer, String>();/ a set of stop wordsprivate Set<String> stopWords = new HashSet<String>();public SimpleSearchEngineImpl(String docFolderPath, String indexFilePath, String docNameIndexPath, String stopWordsFilePath
14、) if (docFolderPath.charAt(docFolderPath.length()-1) = '/')this.docFolderPath = docFolderPath;elsethis.docFolderPath = docFolderPath + "/"this.indexFilePath = indexFilePath;this.docNameIndexPath = docNameIndexPath;this.stopWordsFilePath = stopWordsFilePath;/* * initialize * * load
15、the search index, doc name index, stop words from files if they exist * */public void init() loadIndexFile();loadDocNameIndex();loadStopWords();/* * simple query by splitting the query into search terms and looking up the index, * ranking results by the number of search terms appearing in a document
16、 * * return the list of document names * */public List<String> query(String queryStr) / split the query string into query termsString terms = queryStr.split("s+");/ look up the search index and generate fildId -> count map HashMap<Integer,Integer> map = new HashMap<Intege
17、r,Integer>();for (int i=0; i<terms.length; i+) Set<Integer> docIds = searchIndex.get(termsi);if (docIds != null && docIds.size() > 0) for (Integer id : docIds) Integer count = map.get(id);if (count = null)map.put(id, new Integer(1);elsemap.put(id, count+1);/ rank the search re
18、sult, simply based on the number of query terms appearing in a document. The more the higher the rank. ValueComparator bvc = new ValueComparator(map); TreeMap<Integer,Integer> sortedMap = new TreeMap<Integer,Integer>(bvc); sortedMap.putAll(map); StringBuilder builder = new StringBuilder(
19、); Iterator<Integer> iter = sortedMap.keySet().iterator(); if (iter.hasNext() builder.append(docNames.get(iter.next(); while (iter.hasNext() builder.append(","+docNames.get(iter.next(); System.out.println(" search results: "+builder.toString();return null;/* * add a documen
20、t and update the index * * param docName */public void addDoc(String docName) BufferedReader br = null;try Integer fileId = docNames.size();/ find the next available file Idwhile (docNames.containsKey(fileId)fileId+;docNames.put(fileId, docName);String line;br = new BufferedReader(new FileReader(doc
21、FolderPath + docName);while(line = br.readLine() != null) line = line.toLowerCase();String terms = line.split("a-z+");for (int i = 0; i < terms.length; i+) / check stop wordif (termsi.length() <= 1 | stopWords.contains(termsi)continue;Set<Integer> docIds = searchIndex.get(terms
22、i);/ create docIds list and add to the search indexif (docIds = null) docIds = new TreeSet<Integer>();docIds.add(fileId);searchIndex.put(termsi, docIds);elsedocIds.add(fileId);/printSearchIndex();catch (IOException ex) System.err.println("error accessing doc: " +ex);finally if (br !=
23、 null) try br.close();catch (IOException ex) System.err.println("error closing doc: "+ ex);/printDocNameIndexFile();/* * load the search index from file. * * The format of each line of the index file is as follows: * word: docId1,docId2,docId3,. * * Example: * will: 0,1,2,3 * wise: 2 */pub
24、lic void loadIndexFile() BufferedReader br = null;try File file = new File(indexFilePath); / if file doesnt exists, then create itif (!file.exists() file.createNewFile();return;br = new BufferedReader(new FileReader(file);String line;while(line = br.readLine() != null) int i = line.indexOf(':
25、9;);String key = line.substring(0, i);String terms = line.substring(i+1).trim().split(",s+");Set<Integer> set = new TreeSet<Integer>();for (String term : terms) term = term.trim();if (term.length() > 0) try set.add(Integer.valueOf(term);catch (NumberFormatException ex) Syste
26、m.err.println("loadIndexFile: bad doc Id in line: "+line);searchIndex.put(key, set);catch (IOException ex) System.err.println("error accessing file: " +ex);finally if (br != null) try br.close();catch (IOException ex) System.err.println("error closing file: "+ ex);/* *
27、load the document name index from file * * The format is: * docId docName * * example: * 0 braveNewWord * 1 weAre * */public void loadDocNameIndex() BufferedReader br = null;try File file = new File(docNameIndexPath); / if file doesnt exists, then create itif (!file.exists() file.createNewFile();ret
28、urn;br = new BufferedReader(new FileReader(file);String line;while(line = br.readLine() != null) String terms = line.split("s+");if (terms0.length() > 0 && terms1.length() > 0) try docNames.put(Integer.valueOf(terms0), terms1);catch (NumberFormatException ex) System.err.print
29、ln("loadDocNameIndex: bad doc Id in line: "+line);catch (IOException ex) System.err.println("error accessing file: " +ex);finally if (br != null) try br.close();catch (IOException ex) System.err.println("error closing file: "+ ex);/* * load the stop words from file * th
30、e format is one word per line */public void loadStopWords() BufferedReader br = null;try br = new BufferedReader(new FileReader(stopWordsFilePath);String line;while(line = br.readLine() != null) line = line.trim();if (line.length() > 0)stopWords.add(line);catch (IOException ex) System.err.println
31、("error accessing file: " +ex);finally if (br != null) try br.close();catch (IOException ex) System.err.println("error closing file: "+ ex);/* * output the search index to file */public void printSearchIndex() Iterator<String> iterator = (new TreeSet<String>(searchInd
32、ex.keySet().iterator(); try FileWriter fw = new FileWriter(indexFilePath, false); / overwritewhile (iterator.hasNext() String key = iterator.next().toString(); Set<Integer> idSet = searchIndex.get(key); StringBuilder builder = new StringBuilder(); fw.write(key+": "); Iterator<Inte
33、ger> iter = idSet.iterator(); if (iter.hasNext() builder.append(iter.next(); while (iter.hasNext() builder.append(",").append(iter.next(); /System.out.println(builder.toString(); fw.write(builder.toString()+"n"); fw.close(); catch(IOException ioe) System.err.println("IOException: " + ioe.getMessage(); /* * output the document name index to file */public void printDocNameIndexFile() try FileWriter fw = new FileWriter(docNameIndexPath, false); /
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 未來十年移動支付的科技發(fā)展趨勢預測
- 標準化管理在生產現(xiàn)場的挑戰(zhàn)與對策
- 現(xiàn)代音樂文化的全球化傳播路徑
- 13人物描寫一組(說課稿)2023-2024學年統(tǒng)編版語文五年級下冊
- Unit 1 Playtime Lesson 3(說課稿)-2023-2024學年人教新起點版英語二年級下冊001
- 25 少年閏土 第二課時 說課稿-2024-2025學年語文六年級上冊 統(tǒng)編版
- Unit1 London is a big city(說課稿)2023-2024學年外研版(三起)四年級下冊
- 2024-2025學年高中生物 第七章 現(xiàn)代生物進化理論 第1節(jié) 現(xiàn)代生物進化理論的由來說課稿3 新人教版必修2
- Unit 2 Being a good language learner Exploring and Using 說課稿-2024-2025學年高中英語重大版(2019)必修第一冊
- 2025挖掘機勞動合同范文
- 北師大版五年級上冊四則混合運算100道及答案
- 專項債券在燃氣基礎設施建設中的融資作用
- 人教部編版道德與法治八年級下冊:6.3 《國家行政機關》說課稿1
- GE-LM2500+G4航改燃氣輪機在艦船和工業(yè)上的應用
- 2024山東能源集團中級人才庫選拔(高頻重點提升專題訓練)共500題附帶答案詳解
- 鋼鐵是怎樣煉成的讀后感作文700字
- 武漢市江夏區(qū)2022-2023學年七年級上學期期末數(shù)學試卷【帶答案】-109
- 學校物業(yè)服務合同范本專業(yè)版
- SL 288-2014 水利工程施工監(jiān)理規(guī)范
- 部編版八年級語文上冊期末考試卷
- 2024年02月中央軍委后勤保障部2024年公開招考專業(yè)技能崗位文職人員筆試參考題庫附帶答案詳解
評論
0/150
提交評論