~ubuntu-branches/ubuntu/precise/apt-xapian-index/precise-security

« back to all changes in this revision

Viewing changes to examples/ruby/axi-query-tags.rb

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-06-17 10:51:30 UTC
  • mfrom: (15.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110617105130-zrb03qthrg3l51mv
Tags: 0.43ubuntu1
* Merge from debian unstable.  Remaining changes:
  - when upgrading, ensure the index is fully rebuild (in the
    background) to ensure that we get updated information in
    /var/lib/apt-xapian-index/{index.values} and that the index
    fully utilizes the new plugins (LP: #646018)
  - use ionice for the index building
  - do not crash if the DB is already locked (LP: #590998)
  - data/org.debian.AptXapianIndex.conf: fix policy
  - move to dh_python2
  - update-apt-xapian-index-dbus:
    + fix type of "start-time" for policykit (LP: #675533)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
# axi-query-tags - Look for Debtags tags by keyword
 
4
#
 
5
# Copyright (C) 2007  Enrico Zini <enrico@debian.org>
 
6
# Copyright (C) 2008  Daniel Brumbaugh Keeney
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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
 
21
 
 
22
require 'optparse'
 
23
 
 
24
type = nil
 
25
OptionParser.new do |opts|
 
26
 
 
27
  opts.program_name = 'axi-query-pkgtype.rb'
 
28
  opts.version = '0.1'
 
29
  opts.release = '1203587714'
 
30
 
 
31
  opts.banner =
 
32
    "Query the Apt Xapian index.  Command line arguments can be keywords or Debtags tags"
 
33
 
 
34
 
 
35
  opts.on '-t', '--type TYPE', "package type, one of 'game', 'gui', 'cmdline' or 'editor'" do |v|
 
36
    type = v.to_sym
 
37
  end
 
38
 
 
39
  opts.on_tail("-h", "--help", "Show this message") do
 
40
    puts opts
 
41
    exit
 
42
  end
 
43
 
 
44
end.parse! rescue ( puts 'try axi-query-pkgtype.rb --help'; exit 2 )
 
45
 
 
46
args = ARGV.collect do |i| i.dup; end
 
47
 
 
48
 
 
49
# Import the rest here so we don't need dependencies to be installed only to
 
50
# print commandline help
 
51
require 'xapian'
 
52
require 'aptxapianindex'
 
53
 
 
54
# Instantiate a xapian.Database object for read only access to the index
 
55
db = Xapian::Database.new(XAPIANDB)
 
56
 
 
57
# Stemmer function to generate stemmed search keywords
 
58
stemmer = Xapian::Stem.new("english")
 
59
 
 
60
# Build the base query
 
61
query = Xapian::Query.new(Xapian::Query::OP_OR, terms_for_simple_query(args))
 
62
 
 
63
# Perform the query
 
64
enquire = Xapian::Enquire.new(db)
 
65
enquire.query = query
 
66
 
 
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.
 
72
 
 
73
# Select the first 10 documents as the key ones to use to compute relevant
 
74
# terms
 
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)
 
79
end
 
80
 
 
81
# Xapian supports providing a filter object, to say that we are only interested
 
82
# in some terms.
 
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
 
86
 
 
87
  # Return true if we want the term, else false
 
88
  def __call__ term
 
89
    term[0..1] == "XT"
 
90
  end
 
91
end
 
92
 
 
93
# This is the "Expansion set" for the search: the 10 most relevant terms that
 
94
# match the filter
 
95
eset = enquire.eset(10, rset, Filter.new)
 
96
 
 
97
# Print out the results
 
98
eset.terms.each do |res|
 
99
  puts "%.2f %s" % [res.weight, res.name[2..-1]]
 
100
end