免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7475 | 回复: 4
打印 上一主题 下一主题

python2.6 +jcc + pylucen (lucene2.4)安装 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-04 23:14 |只看该作者 |倒序浏览
附件中的两个文件是 mingw32下面编译好的 jcc和pylucen的安装包
python通过 jni调用 lucene api感觉还比较好的,
jre1.6.0.3

lucene-2.4.0-py2.6-win32.rar

3.31 MB, 下载次数: 183

pylucene 安装包

JCC-2.1.win32-py2.6.rar

207.55 KB, 下载次数: 170

论坛徽章:
0
2 [报告]
发表于 2009-01-05 09:02 |只看该作者
期待楼主分享点实例出来看看

论坛徽章:
0
3 [报告]
发表于 2009-01-05 21:58 |只看该作者
下面的代码是 pylucene sample的代码
创建索引

import sys, os, lucene, threading, time
from 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 argument
and 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.  The
resulting 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 = lucene.FSDirectory.getDirectory(storeDir, True)
        writer = lucene.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') and not filename.endswith(".py"):
                    continue
                print "adding", filename
                try:
                    path = os.path.join(root, filename)
                    file = open(path)
                    contents = unicode(file.read(), 'iso-8859-1')
                    file.close()
                    doc = lucene.Document()
                    doc.add(lucene.Field("name", filename,
                                         lucene.Field.Store.YES,
                                         lucene.Field.Index.UN_TOKENIZED))
                    doc.add(lucene.Field("path", path,
                                         lucene.Field.Store.YES,
                                         lucene.Field.Index.UN_TOKENIZED))
                    if len(contents) > 0:
                        doc.add(lucene.Field("contents", contents,
                                             lucene.Field.Store.NO,
                                             lucene.Field.Index.TOKENIZED))
                    else:
                        print "warning: no content in %s" % filename
                    writer.addDocument(doc)
                except Exception, e:
                    print "Failed in indexDocs:", e

if __name__ == '__main__':
    sys.argv.append(".")
    if len(sys.argv) < 2:
        print IndexFiles.__doc__
        sys.exit(1)
    lucene.initVM(lucene.CLASSPATH)
    print 'lucene', lucene.VERSION
    start = datetime.now()
    try:
        IndexFiles(sys.argv[1], "index", lucene.StandardAnalyzer())
        end = datetime.now()
        print end - start
    except Exception, e:
        print "Failed: ", e

评分

参与人数 1可用积分 +5 收起 理由
xiaoyu9805119 + 5 实例

查看全部评分

论坛徽章:
0
4 [报告]
发表于 2009-01-05 21:59 |只看该作者
from lucene import \
&nbsp;&nbsp;&nbsp;&nbsp;QueryParser, IndexSearcher, StandardAnalyzer, FSDirectory, Hit, \
&nbsp;&nbsp;&nbsp;&nbsp;VERSION, initVM, CLASSPATH


"""
This script is loosely based on the Lucene (java implementation) demo class
org.apache.lucene.demo.SearchFiles.  It will prompt for a search query, then it
will search the Lucene index in the current directory called 'index' for the
search query entered against the 'contents' field.  It will then display the
'path' and 'name' fields for each of the hits it finds in the index.  Note that
search.close() is currently commented out because it causes a stack overflow in
some cases.
"
""
def run(searcher, analyzer):
&nbsp;&nbsp;&nbsp;&nbsp;while True:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Hit enter with no input to quit."
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command = raw_input("Query:")
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if command == '':
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "Searching for:", command
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query = QueryParser("contents", analyzer).parse(command)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hits = searcher.search(query)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "%s total matching documents." % hits.length()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for hit in hits:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doc = Hit.cast_(hit).getDocument()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print 'path:', doc.get("path"), 'name:', doc.get("name")


if __name__ == '__main__':
&nbsp;&nbsp;&nbsp;&nbsp;STORE_DIR = "index"
&nbsp;&nbsp;&nbsp;&nbsp;initVM(CLASSPATH)
&nbsp;&nbsp;&nbsp;&nbsp;print 'lucene', VERSION
&nbsp;&nbsp;&nbsp;&nbsp;directory = FSDirectory.getDirectory(STORE_DIR, False)
&nbsp;&nbsp;&nbsp;&nbsp;searcher = IndexSearcher(directory)
&nbsp;&nbsp;&nbsp;&nbsp;analyzer = StandardAnalyzer()
&nbsp;&nbsp;&nbsp;&nbsp;run(searcher, analyzer)
&nbsp;&nbsp;&nbsp;&nbsp;searcher.close()

论坛徽章:
0
5 [报告]
发表于 2009-06-06 02:01 |只看该作者
能分享下mingw win下怎么编译的吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP