liuqiang5151
BlogJava
::
首页
::
联系
::
聚合
::
管理
0 Posts :: 23 Stories :: 1 Comments :: 0 Trackbacks
留言簿
(18)
给我留言
查看公开留言
查看私人留言
我参与的团队
文章分类
Ajax(2)
Hibernate(3)
java(10)
Lucene(2)
Spring(3)
数据库(1)
设计模式(2)
文章档案
2008年7月 (3)
2008年5月 (3)
2008年4月 (5)
2007年12月 (1)
2007年11月 (1)
2007年10月 (1)
2007年9月 (9)
搜索
最新评论
1. re: 转:DWR util.js工具包的使用[未登录]
关于useLoadingImage的使用,我都无法写成功,您能否把源代码发到我的邮箱?
wl_polo@163.com
不胜感谢!
--王冰
获取关键词出现的频率及位置
终于清楚如何用读Lucene的索引 :-)。本文要介绍一下如何利用IndexReader获取信息。为什么要读索引呢?因为我需要实现这些功能:
(1) 统计term在整个collection中的文档频度(document frequency, DF);
(2) 统计term在整个collection中出现的词次(term frequency in whole collection);
(3) 统计term在某个文档中出现的频度(term frequency, TF);
(4) 列出term在某文档中出现的位置(position);
(5) 整个collection中文档的个数;
Lucene要加上新的功能,要获取关键词出现的频率,以前都是对Hits进行操作,但是hits只能获取相关的Document id 以及内容。
上网搜了一下,通过IndexReader直接操作索引文件。
public
class
Search
{
public
static
void
main(String[] args)
throws
CorruptIndexException, IOException
{
IndexSearcher is
=
new
IndexSearcher(
"
c:\\test
"
);
IndexReader ir
=
IndexReader.open(
"
c:\\test
"
);
Term term
=
new
Term(
"
content
"
,
"
我
"
);
Query query
=
new
TermQuery(term);
Hits hits
=
is.search(query);
printFreq(ir,hits);
}
private
static
void
printFreq(IndexReader reader,Hits hits)
throws
CorruptIndexException, IOException
{
reader.numDocs();
Term term
=
new
Term(
"
content
"
,
"
我
"
);
TermPositions termPositions
=
reader.termPositions(term);
for
(
int
i
=
0
;i
<
hits.length()
&&
termPositions.next();i
++
)
{
Document doc
=
hits.doc(i);
System.out.println(
"
'我'在
"
+
"
<<
"
+
doc.get(
"
title
"
)
+
"
>>
"
+
"
里出现的
"
+
"
次数是:
"
+
termPositions.freq());
for
(
int
j
=
0
;j
<
termPositions.freq();j
++
)
System.out.println(
"
出现位置:
"
+
termPositions.nextPosition());
}
}
}
输出结果:
'我'在<<2.txt>>里出现的次数是:1
出现位置:1
'我'在<<3.txt>>里出现的次数是:3
出现位置:0
出现位置:5
出现位置:6
posted on 2007-10-15 17:54
刘强
阅读(647)
评论(0)
编辑
收藏
所属分类:
Lucene
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
相关文章:
Lucene高亮显示
获取关键词出现的频率及位置
Copyright @ 刘强
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster