3
# axi-query-tags - Look for Debtags tags by keyword
5
# Copyright (C) 2007 Enrico Zini <enrico@debian.org>
6
# Copyright (C) 2008 Daniel Brumbaugh Keeney
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
OptionParser.new do |opts|
27
opts.program_name = 'axi-query-pkgtype.rb'
29
opts.release = '1203587714'
32
"Query the Apt Xapian index. Command line arguments can be keywords or Debtags tags"
35
opts.on '-t', '--type TYPE', "package type, one of 'game', 'gui', 'cmdline' or 'editor'" do |v|
39
opts.on_tail("-h", "--help", "Show this message") do
44
end.parse! rescue ( puts 'try axi-query-pkgtype.rb --help'; exit 2 )
46
args = ARGV.collect do |i| i.dup; end
49
# Import the rest here so we don't need dependencies to be installed only to
50
# print commandline help
52
require 'aptxapianindex'
54
# Instantiate a xapian.Database object for read only access to the index
55
db = Xapian::Database.new(XAPIANDB)
57
# Stemmer function to generate stemmed search keywords
58
stemmer = Xapian::Stem.new("english")
60
# Build the base query
61
query = Xapian::Query.new(Xapian::Query::OP_OR, terms_for_simple_query(args))
64
enquire = Xapian::Enquire.new(db)
67
# Now, instead of showing the results of the query, we ask Xapian what are the
68
# terms in the index that are most relevant to this search.
69
# Normally, you would use the results to suggest the user possible ways for
70
# refining the search. I instead abuse this feature to see what are the tags
71
# that are most related to the search results.
73
# Select the first 10 documents as the key ones to use to compute relevant
75
rset = Xapian::RSet.new
76
enquire.mset(0, 5).matches.each do |m|
77
# TODO: use adaptive quality threshold here
78
rset.add_document(m.docid)
81
# Xapian supports providing a filter object, to say that we are only interested
83
# This one filters out all the keywords that are not tags, or that were in the
84
# list of query terms.
85
class Filter < Xapian::ExpandDecider
87
# Return true if we want the term, else false
93
# This is the "Expansion set" for the search: the 10 most relevant terms that
95
eset = enquire.eset(10, rset, Filter.new)
97
# Print out the results
98
eset.terms.each do |res|
99
puts "%.2f %s" % [res.weight, res.name[2..-1]]