⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexfiles.py

📁 Harvestman-最新版本
💻 PY
字号:
# -- coding: utf-8#!/usr/bin/env pythonimport sys, os, PyLucene, threading, timefrom datetime import datetime"""This class is loosely based on the Lucene (java implementation) demo class org.apache.lucene.demo.IndexFiles.  It will take a directory as an argumentand will index all of the files in that directory and downward recursively.It will index on the file path, the file name and the file contents.  Theresulting Lucene index will be placed in the current directory and called'index'."""class Ticker(object):    def __init__(self):        self.tick = True    def run(self):        while self.tick:            sys.stdout.write('.')            sys.stdout.flush()            time.sleep(1.0)class IndexFiles(object):    """Usage: python IndexFiles <doc_directory>"""    def __init__(self, root, storeDir, analyzer):        if not os.path.exists(storeDir):            os.mkdir(storeDir)        store = PyLucene.FSDirectory.getDirectory(storeDir, True)        writer = PyLucene.IndexWriter(store, analyzer, True)        writer.setMaxFieldLength(1048576)        self.indexDocs(root, writer)        ticker = Ticker()        print 'optimizing index',        threading.Thread(target=ticker.run).start()        writer.optimize()        writer.close()        ticker.tick = False        print 'done'    def indexDocs(self, root, writer):        for root, dirnames, filenames in os.walk(root):            for filename in filenames:                #if not filename.endswith('.txt'):                #    continue                print "adding", filename                try:                    path = os.path.join(root, filename)                    file = open(path)                    contents = unicode(file.read(), 'iso-8859-1')                    file.close()                    doc = PyLucene.Document()                    doc.add(PyLucene.Field("name", filename,                                           PyLucene.Field.Store.YES,                                           PyLucene.Field.Index.UN_TOKENIZED))                    doc.add(PyLucene.Field("path", path,                                           PyLucene.Field.Store.YES,                                           PyLucene.Field.Index.UN_TOKENIZED))                    if len(contents) > 0:                        doc.add(PyLucene.Field("contents", contents,                                               PyLucene.Field.Store.YES,                                               PyLucene.Field.Index.TOKENIZED))                    else:                        print "warning: no content in %s" % filename                    writer.addDocument(doc)                except Exception, e:                    print "Failed in indexDocs:", eif __name__ == '__main__':    if len(sys.argv) < 2:        print IndexFiles.__doc__        sys.exit(1)    print 'PyLucene', PyLucene.VERSION, 'Lucene', PyLucene.LUCENE_VERSION    start = datetime.now()    try:        IndexFiles(sys.argv[1], "index", PyLucene.StandardAnalyzer())        end = datetime.now()        print end - start    except Exception, e:        print "Failed: ", e

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -