home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-xapian / examples / simplesearch.py < prev   
Encoding:
Python Source  |  2009-02-22  |  2.2 KB  |  69 lines

  1. #!/usr/bin/env python
  2. #
  3. # Simple command-line search script.
  4. #
  5. # Copyright (C) 2003 James Aylett
  6. # Copyright (C) 2004,2007 Olly Betts
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License as
  10. # published by the Free Software Foundation; either version 2 of the
  11. # License, or (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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
  21. # USA
  22.  
  23. import sys
  24. import xapian
  25.  
  26. # We require at least two command line arguments.
  27. if len(sys.argv) < 3:
  28.     print >> sys.stderr, "Usage: %s PATH_TO_DATABASE QUERY" % sys.argv[0]
  29.     sys.exit(1)
  30.  
  31. try:
  32.     # Open the database for searching.
  33.     database = xapian.Database(sys.argv[1])
  34.  
  35.     # Start an enquire session.
  36.     enquire = xapian.Enquire(database)
  37.  
  38.     # Combine the rest of the command line arguments with spaces between
  39.     # them, so that simple queries don't have to be quoted at the shell
  40.     # level.
  41.     query_string = sys.argv[2]
  42.     for arg in sys.argv[3:]:
  43.         query_string += ' '
  44.         query_string += arg
  45.  
  46.     # Parse the query string to produce a Xapian::Query object.
  47.     qp = xapian.QueryParser()
  48.     stemmer = xapian.Stem("english")
  49.     qp.set_stemmer(stemmer)
  50.     qp.set_database(database)
  51.     qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
  52.     query = qp.parse_query(query_string)
  53.     print "Parsed query is: %s" % query.get_description()
  54.  
  55.     # Find the top 10 results for the query.
  56.     enquire.set_query(query)
  57.     matches = enquire.get_mset(0, 10)
  58.  
  59.     # Display the results.
  60.     print "%i results found." % matches.get_matches_estimated()
  61.     print "Results 1-%i:" % matches.size()
  62.  
  63.     for m in matches:
  64.         print "%i: %i%% docid=%i [%s]" % (m.rank + 1, m.percent, m.docid, m.document.get_data())
  65.  
  66. except Exception, e:
  67.     print >> sys.stderr, "Exception: %s" % str(e)
  68.     sys.exit(1)
  69.