#!/usr/bin/env python
#
# Simple command-line search script.
#
# Copyright (C) 2003 James Aylett
# Copyright (C) 2004,2007,2009 Olly Betts
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
import sys
import xapian
# We require at least two command line arguments.
if len(sys.argv) < 3:
print >> sys.stderr, "Usage: %s PATH_TO_DATABASE QUERY" % sys.argv[0]
sys.exit(1)
try:
# Open the database for searching.
database = xapian.Database(sys.argv[1])
# Start an enquire session.
enquire = xapian.Enquire(database)
# Combine the rest of the command line arguments with spaces between
# them, so that simple queries don't have to be quoted at the shell
# level.
query_string = str.join(' ', sys.argv[2:])
# Parse the query string to produce a Xapian::Query object.
qp = xapian.QueryParser()
stemmer = xapian.Stem("english")
qp.set_stemmer(stemmer)
qp.set_database(database)
qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
query = qp.parse_query(query_string)
print "Parsed query is: %s" % str(query)
# Find the top 10 results for the query.
enquire.set_query(query)
matches = enquire.get_mset(0, 10)
# Display the results.
print "%i results found." % matches.get_matches_estimated()
print "Results 1-%i:" % matches.size()
for m in matches:
print "%i: %i%% docid=%i [%s]" % (m.rank + 1, m.percent, m.docid, m.document.get_value(0))
except Exception, e:
print >> sys.stderr, "Exception: %s" % str(e)
sys.exit(1)
没有合适的资源?快使用搜索试试~ 我知道了~
python xapian 简单应用
共2个文件
py:2个
5星 · 超过95%的资源 需积分: 20 51 下载量 71 浏览量
2010-02-20
19:16:09
上传
评论 2
收藏 2KB ZIP 举报
温馨提示
由于xapian的Python版中的示例只是简单的从命令行获取输入而不是文件系统,我在网上搜集了一些资料,终于写出一个可用的python+xapian,可以对整个目录进行索引。
资源推荐
资源详情
资源评论
收起资源包目录
python_xapian.zip (2个子文件)
simpleindex.py 1KB
simplesearch.py 2KB
共 2 条
- 1
资源评论
- liangkazhe2012-11-15这个小工具还不错
- 举杯邀明月_2013-01-11这方面中文资料不多 谢楼主分享
charloco
- 粉丝: 3
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功