版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年哈尔滨银行七台河分行招聘外包员工5人备考题库完整答案详解
- 2025年中国航空工业集团凯天岗位招聘备考题库及答案详解参考
- 2025年龙岩市上杭县人民法院招聘编外人员的备考题库及1套完整答案详解
- 2026年深空探测数据使用合同
- 2025年北京西城区高二(上)期末历史试题和答案
- 监管协管员面试题及答案解析(2025版)
- 有色金属行业2025Q3总结:Q3盈利同比继续上行拥抱资源新周期
- 中国社会科学院世界经济与政治研究所2026年度公开招聘第一批专业技术人员6人备考题库及答案详解一套
- 来宾市公安局2025年第三次招聘辅警备考题库及参考答案详解一套
- 崇左凭祥市应急管理局招聘考试真题2024
- 柔性引才合同协议
- 医学影像云存储:容灾备份与数据恢复方案
- 2025中原农业保险股份有限公司招聘67人笔试考试参考试题及答案解析
- 2025年卫生系统招聘(临床专业知识)考试题库(含答案)
- 基建工程索赔管理人员索赔管理经典文献
- 工业机器人专业大学生职业生涯规划书
- 农贸市场消防安全管理制度
- 良品铺子营运能力分析及对策研究
- 2025年战略投资专员岗位招聘面试参考试题及参考答案
- 2025年小学教师素养大赛试题(含答案)
- 特种设备应急处置课件
评论
0/150
提交评论