diff options
author | User <yvesf@x3.localnet.cc> | 2010-11-19 21:47:48 +0100 |
---|---|---|
committer | User <yvesf@x3.localnet.cc> | 2010-11-19 21:47:48 +0100 |
commit | 0d9a0f4a893e3060f960ec52ccf3effd2ea43674 (patch) | |
tree | 17b65005347a893179d9a9ea7255e3dd5f7aa72f /query.py | |
parent | 020353897db29636d362f459d6a61ad363e7d8f6 (diff) | |
download | booksearch-0d9a0f4a893e3060f960ec52ccf3effd2ea43674.tar.gz booksearch-0d9a0f4a893e3060f960ec52ccf3effd2ea43674.zip |
blah
Diffstat (limited to 'query.py')
-rw-r--r-- | query.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/query.py b/query.py new file mode 100644 index 0000000..f7f70f3 --- /dev/null +++ b/query.py @@ -0,0 +1,54 @@ +#!/usr/bin/python2.6 +# coding: utf-8 +import os +import sys +import pyPdf +from whoosh.index import create_in +import whoosh.fields as fields +import time + +schema = fields.Schema( + title=fields.TEXT(stored=True), + path=fields.ID(stored=True), + content=fields.TEXT(stored=True), + createtime=fields.NUMERIC() ) + +if not os.path.exists("index"): + os.mkdir("index") + +index = create_in(u"index", schema, "books") +writer = index.writer() + + +# extract +directory = "/tank/share/books/isbn" + +try: + for path, directories, files in os.walk(directory): + for filename in files: + if filename.endswith(".pdf"): + filepath = os.path.join(path, filename) + print u"Process {0}".format(filepath) + inputfile = pyPdf.PdfFileReader(file(filepath, 'r')) + title = inputfile.getDocumentInfo().title + content = u"" + i=1 + numpages = inputfile.getNumPages() + for page in inputfile.pages: + sys.stdout.write("\rPage {0}/{1}".format(i, numpages)) + sys.stdout.flush() + content += page.extractText() + i+=1 + print u"" + writer.add_document(title=title, path=filepath, content=content, createtime=time.time()) +except KeyboardInterrupt: + writer.commit() + +from whoosh.qparser import QueryParser + +searcher = index.searcher() + +query = QueryParser("content").parse("world") + +results = searcher.search(query) +print results |