Just starting out with Xapian and the Python bindings but I've not been able to get my code to open or create a database file to index the text using the simple index as a start. I'd be grateful for any advice on what I've done incorrectly in the _db code: import xapian import string class TextIndex (object): _db = None def get_db (): if _db == None: _db = xapian.WritableDatabase ('c:\\index', xapian.DB_CREATE_OR_OPEN) return _db def index (db): #need to add in time taken to index try: indexer = xapian.TermGenerator() stemmer = xapian.Stem("english") indexer.set_stemmer(stemmer) texts = open('C:\\webroot\\milton\\miltontest.txt') para = '' try: for line in texts: line = string.strip(line) if line == '': if para != '': # We've reached the end of a paragraph, so index it. doc = xapian.Document() doc.set_data(para) indexer.set_document(doc) indexer.index_text(para) # Add the document to the database. get_db().add_document(doc) para = '' else: if para != '': para += ' ' para += line except StopIteration: pass except Exception, e: print >> sys.stderr, "Exception: %s" % str(e)
On Mon, Jun 02, 2008 at 09:43:35PM +0100, Iain Emsley wrote:> Just starting out with Xapian and the Python bindings but I've not been able > to get my code to open or create a database file to index the text using the > simple index as a start. I'd be grateful for any advice on what I've done > incorrectly in the _db code:How does it fail? If there's an error, what is it? Also, what version of Xapian is this with? Cheers, Olly
On Mon, Jun 02, 2008 at 09:43:35PM +0100, Iain Emsley wrote:> class TextIndex (object): > _db = None > def get_db (): > if _db == None: > _db = xapian.WritableDatabase ('c:\\index', xapian.DB_CREATE_OR_OPEN) > return _dbThis isn't how to implement a singleton in python. At a minimum, you need to global _db inside the get_db() function. J -- /--------------------------------------------------------------------------\ James Aylett xapian.org james at tartarus.org uncertaintydivision.org