搜索引擎开发笔记一

本文详细介绍了开发搜索引擎所需的关键工具和步骤,包括使用Eclipse作为集成开发环境,配置Java开发工具包,引入Lucene和Heritrix库等。通过构建一个简单的txt文件索引实例,读者可以理解搜索引擎内部机制,如索引建立和文本预处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


       哇,开发一个搜素引擎,对于一个我这个刚上大一的新童鞋听到这个还是感觉鸭梨山大的。。。不过,借用王国维的《热爱生命》中的一句话,“既然选择了远方,便只顾

风雨兼程”。我相信我们团队sunshine studio一定会交一份满意答卷的。话说工欲善其事,必先利其器。那么开发搜索擎,我们需要什么工具呢?      (参考书籍为人民邮电

出版社的《开发自己的搜索引擎Lucene+Heritrix》)

 

Tools.

      1.首先你需要一个强大的java集成开发环境—Eclipse,它能极高提高开发效率。我用的是Eclipse版本(Version: 3.5.2)

      (Eclipse的下载地址:http://www.eclipse.org/downloads/index.php)

      2.但Eclipse并不包含Java开发工具包(JDK),需要自己下载。配置环境变量暂不介绍。

      3.Jar包暂时需要的有两个(je-analysis-1.4.0.jar(分词包)和Lucene-core-2.0.0.jar(Lucene的API))百度一下就有

 

Now,worktime.

      网络上的信息每天都按指数级增长,怎样在茫茫信息中快速找到用户想要的信息。那么就需要一种有效的机制,使得搜索引擎(search engine )能够将各网站的内容进行检索,这种机制就是索引。现在我们就开始小试牛刀,对一篇txt文件建立索引。

     打开Eclipse-〉workspace-〉选择工程存放位置-〉进入主界面

     因为之前已经安装JDK(内置JRE),配置好环境变量后,故不再配置Eclipse中的JRE即可使用Eclipse。

     File-〉New-〉Project-〉Java Project-〉点击Next-〉输入工程名firstProject-〉Next-〉单击Finish  欧了,一个工程就建好了。

     右键单击位于Package Explorer的工程名“firstProject”-〉new-〉package-〉进入创建Package的界面-〉Name文本栏里输入firstProject.lucenedemo.preprocess->

Finish.   这个包就建好了。同理建立firstProject.lucenedemo.process包和first.lucenedemo.test包

     下面为工程添加上面提到的两个Jar包 这两个包将被放在工程目录下的“lib”目录下(右键单击工程名字-〉new-〉Folder-〉输入名字:lib-〉)

      建好lib文件夹后,要导入包详细教程http://blog.youkuaiyun.com/justinavril/article/details/2783182

      右键单击first.lucenedemo.preprocess-〉new-〉class 创建新类FilePreprocess 

      在此类里添加如下代码

       

 

package firstProject.lucenedemo.preprocess;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class FilePreprocess {
   
  public static void main(String[] args){
      
      //设置需要被预处理的源文件位置
      String inputFile = "D:\TDDOWNLOAD\\DaughterOfSea.txt";
      //注意:txt文件名不要加中文,Eclipse不能识别      
      //设置处理后的文件存放位置
      String outputDir = "D:\\TDDOWNLOAD\\niceFolder\\";
      
      //判断处理后文件存放文件夹是否存在,如果不存在则创建文件夹
      if(!new File(outputDir).exists())
      new File(outputDir).mkdirs();
      
      //创建一个FilePreprocess类,并调用preprocess方法进行预处理
      FilePreprocess filePreprocess = new FilePreprocess();
      filePreprocess.preprocess(new File(inputFile),outputDir);
  }


	
	/**
	 *两个参数,一个是要被处理的源文件
	 * 另一个是处理后的文件输出路径
	 */
	public static void preprocess(File file, String outputDir) {
		try {
			splitToSmallFiles(charactorProcess(file, outputDir + "output.all"), outputDir);
	} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *对文件字符进行全角/半角处理
	 */
	public static File charactorProcess(File file, String destFile)
			throws Exception {

		BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));

		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = reader.readLine();

		while (line != null) {
				String newline = replace(line);
				writer.write(newline);
				writer.newLine();
			line = reader.readLine();
		}

		reader.close();
		writer.close();

		return new File(destFile);

	}

	/** 
	 *拆分成小文件
	 */
	public static void splitToSmallFiles(File file, String outputpath) throws IOException {
		
		int filePointer = 0;
		
		int MAX_SIZE = 10240;
		
		BufferedWriter writer = null;
		BufferedReader reader = new BufferedReader(new FileReader(file));
		StringBuffer buffer = new StringBuffer();
		String line = reader.readLine();
		
		while (line != null) {
			buffer.append(line).append("\r\n");
			if (buffer.toString().getBytes().length >= MAX_SIZE)
			{
				writer = new BufferedWriter(new FileWriter(outputpath + "output" + filePointer + ".txt"));
				writer.write(buffer.toString());
				writer.close();
				filePointer++;
				
				buffer = new StringBuffer();
			}
			line = reader.readLine();
		}
		
		writer = new BufferedWriter(new FileWriter(outputpath + "output" + filePointer + ".txt"));
		writer.write(buffer.toString());
		writer.close();
		
		
	}
	
	/** 
	 *全角/半角的转换
	 */

	private static String replace(String line) {

		HashMap map = new HashMap();
		map.put(",", ",");
		map.put("。", ".");
		map.put("〈", "<");
		map.put("〉", ">");
		map.put("‖", "|");
		map.put("《", "<");
		map.put("》", ">");
		map.put("〔", "[");
		map.put("〕", "]");
		map.put("﹖", "?");
		map.put("?", "?");
		map.put("“", "\"");
		map.put("”", "\"");
		map.put(":", ":");
		map.put("、", ",");
		map.put("(", "(");
		map.put(")", ")");
		map.put("【", "[");
		map.put("】", "]");
		map.put("—", "-");
		map.put("~", "~");
		map.put("!", "!");
		map.put("‵", "'");
		map.put("①", "1");
		map.put("②", "2");
		map.put("③", "3");
		map.put("④", "4");
		map.put("⑤", "5");
		map.put("⑥", "6");
		map.put("⑦", "7");
		map.put("⑧", "8");
		map.put("⑨", "9");

		int length = line.length();
		for (int i = 0; i < length; i++) {
			String charat = line.substring(i, i + 1);
			if (map.get(charat) != null) {
				line = line.replace(charat, (String) map.get(charat));
			}
		}

		return line;
	}
}


 


 


右键单击FilePreprocess类-〉run as-〉java application 这时D:\TODOWNLOAD\niceFolder就有新文件了。下一节进行与Lucene有关的工作。

 

 

 

 

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值