python搜索引擎pylucene_搜索引擎–Python文本文件分割、PyLucene建立索引和索引搜索...

主机平台:Ubuntu 13.04

Python版本:2.7.4

PyLucene版本:4.4.0

最近想使用Python来做一个小的搜索引擎,一来是强化学习一下Pyhton语言,而来是学习一下搜索引擎实现原理。

在网上搜索了好久,网上的资料和书籍都是使用JAVA平台下的Lucene,而PyLucene是Lucene的Python实现。下面是官网的说明:

PyLucene is a Python extension for accessing Java Lucene TM . Its goal is to allow you to use Lucene’s text indexing and searching capabilities from Python. It is API compatible with the latest version of Java Lucene, version 4.4.0

Pylucene是一个可以使用Lucene的扩展。它的目标是让你能够在Python中使用Lucene的索引和搜索。它的API和最新的JAVA Lucene是兼容的。

虽然官网说API是兼容的,但是对Python还不是很熟悉的情况下,感觉还是有点怵,慢慢摸索吧。

splitFiles.py

它实现了将大文本文件切割成50行的小文本文件。

#!/usr/bin/env python

import os

import sys

import os.path

def split(file):

“”"split file to some small ones”"”

if not os.path.isfile(file):

print file,”is not a file”

exit(1)

txtfile=open(file,”r”)

dirname=os.path.dirname(file)

file_index=0

line_cnt = 0

outfile=open(dirname+”/output_%d”%file_index+’.txt’,’w’)

for line in txtfile:

if line_cnt < 50:

outfile.write(line)

line_cnt+=1

else:

outfile.close()

file_index+=1

outfile=open(dirname+”/output_%d”%file_index+’.txt’,’w’)

line_cnt=0

outfile.close()

txtfile.close()

if __name__ == “__main__”:

base_dir=os.path.dirname(os.path.abspath(sys.argv[0]))

root=os.path.join(base_dir,”txtfiles”)

#print root

for rootdir,dirnames,filenames in os.walk(root):

for filename in filenames:</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值