home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / apt-xapian-index / examples / axi-query-expand.py < prev    next >
Encoding:
Python Source  |  2008-04-23  |  3.9 KB  |  116 lines

  1. #!/usr/bin/python
  2.  
  3. # axi-query-expand - Query and show possible expansions
  4. #
  5. # Copyright (C) 2007  Enrico Zini <enrico@debian.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21. from optparse import OptionParser
  22. import sys
  23.  
  24. VERSION="0.1"
  25.  
  26. # Let's start with a simple command line parser with help
  27. class Parser(OptionParser):
  28.     def __init__(self, *args, **kwargs):
  29.         OptionParser.__init__(self, *args, **kwargs)
  30.  
  31.     def error(self, msg):
  32.         sys.stderr.write("%s: error: %s\n\n" % (self.get_prog_name(), msg))
  33.         self.print_help(sys.stderr)
  34.         sys.exit(2)
  35.  
  36. parser = Parser(usage="usage: %prog [options] keywords",
  37.                 version="%prog "+ VERSION,
  38.                 description="Query the Apt Xapian index.  Command line arguments can be keywords or Debtags tags")
  39. parser.add_option("-t", "--type", help="package type, one of 'game', 'gui', 'cmdline' or 'editor'")
  40.  
  41. (options, args) = parser.parse_args()
  42.  
  43.  
  44. # Import the rest here so we don't need dependencies to be installed only to
  45. # print commandline help
  46. import os
  47. import xapian
  48. from aptxapianindex import *
  49.  
  50.  
  51. # Instantiate a xapian.Database object for read only access to the index
  52. db = xapian.Database(XAPIANDB)
  53.  
  54. # Build the base query as seen in axi-query-simple.py
  55. query = xapian.Query(xapian.Query.OP_OR, termsForSimpleQuery(args))
  56.  
  57. # Add the simple user filter, if requeste
  58. query = addSimpleFilterToQuery(query, options.type)
  59.  
  60. # Perform the query
  61. enquire = xapian.Enquire(db)
  62. enquire.set_query(query)
  63.  
  64. # Retrieve the top 20 results
  65. matches = enquire.get_mset(0, 20)
  66.  
  67. # Display the results
  68. show_mset(matches)
  69.  
  70. # Now, we ask Xapian what are the terms in the index that are most relevant to
  71. # this search.  This can be used to suggest to the user the most useful ways of
  72. # refining the search.
  73.  
  74. # Select the first 10 documents as the key ones to use to compute relevant
  75. # terms
  76. rset = xapian.RSet()
  77. for m in matches:
  78.     rset.add_document(m[xapian.MSET_DID])
  79.  
  80. # This is the "Expansion set" for the search: the 10 most relevant terms
  81. eset = enquire.get_eset(10, rset)
  82.  
  83. # Print it out.  Note that some terms have a prefix from the database: can we
  84. # filter them out?  Indeed: Xapian allow to give a filter to get_eset.
  85. # Read on...
  86. print
  87. print "Terms that could improve the search:",
  88. print ", ".join(["%s (%.2f%%)" % (res.term, res.weight) for res in eset])
  89.  
  90.  
  91. # You can also abuse this feature to show what are the tags that are most
  92. # related to the search results.  This allows you to turn a search based on
  93. # keywords to a search based on semantic attributes, which would be an
  94. # absolutely stunning feature in a GUI.
  95.  
  96. # We can do it thanks to Xapian allowing to specify a filter for the output of
  97. # get_eset.  This filter filters out all the keywords that are not tags, or
  98. # that were in the list of query terms.
  99. class Filter(xapian.ExpandDecider):
  100.     def __call__(self, term):
  101.         """
  102.         Return true if we want the term, else false
  103.         """
  104.         return term[:2] == "XT"
  105.  
  106. # This is the "Expansion set" for the search: the 10 most relevant terms that
  107. # match the filter
  108. eset = enquire.get_eset(10, rset, Filter())
  109.  
  110. # Print out the resulting tags
  111. print
  112. print "Tags that could improve the search:",
  113. print ", ".join(["%s (%.2f%%)" % (res.term[2:], res.weight) for res in eset])
  114.  
  115. sys.exit(0)
  116.