一个简单的搜索引擎程序

一个简单的索引和搜索引擎程序,简单到让你无法相信

package chapter2;
import java.io.*;


import java.util.*;


public class WordIndex {


/**
* @param args
*/
static Hashtable<String, infoItem> KeywordIdx;//哈希表用来建立倒排索引
static String[] FileList = {"北 京 师 范 大 学","北 师 大 附 属 实 验 小 学","北 师 大 第 二 附 属 中 学"};//处理好的文本
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
try
{
index();
while(true)//等待查询输入
{
System.out.println("enter a key for serrch");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
search(str);
}
}
catch (Exception e)
{
System.err.println("下载失败,请检查地址是否正确!");
System.exit(1);
}
}
/**************************************************
* 搜索函数
***************************************************/
public static void search(String keyword) throws Exception
{
infoItem item;
System.out.println("search:------------------begin-------------------");
if (null==KeywordIdx)
{
return;
}
try
{
item = (infoItem)KeywordIdx.get(keyword);
while(item!=null)
{
System.out.println("search: File number : "+item.get_id());
System.out.println("search: File offset : "+item.get_pos());
System.out.println("search: File content : "+FileList[item.get_pos()]);
item = item.get_next();
}
}
catch(Exception e)
{
throw e;
}
System.out.println("seach: ----------------end---------------------");
}
/***************************************
* 索引函数
*****************************************/
public static void index() throws Exception
{
infoItem item2;
System.out.println("index:-----------begin------------");
KeywordIdx = new Hashtable<String, infoItem>();

try
{
System.out.println("index: Hash Table initial size: "+KeywordIdx.size());

for(int i = 0; i < 3; i++)
{
int len = FileList[i].length();
for(int j = 0; j < len; j++)
{
infoItem item = new infoItem(i,i);
String key = FileList[i].substring(j,j+1);
System.out.print(key);
if(!KeywordIdx.containsKey(key))
{
KeywordIdx.put(key,item);
}
else
{
item2 = (infoItem)KeywordIdx.get(key);
item.set_next(item2);
KeywordIdx.put(key, item);
}
}
System.out.println("");
}
System.out.println("index: hash table finish size: "+KeywordIdx.size());
}
catch (Exception e)
{
throw e;
}
System.out.println("index:---------------end----------------");
}


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值