pylucene构建索引_搜索引擎–Python文本文件分割、PyLucene建立索引和索引搜索

主機平台:Ubuntu 13.04

Python版本:2.7.4

PyLucene版本:4.4.0

最近想使用Python來做一個小的搜索引擎,一來是強化學習一下Pyhton語言,而來是學習一下搜索引擎實現原理。

在網上搜索了好久,網上的資料和書籍都是使用JAVA平台下的Lucene,而PyLucene是Lucene的Python實現。下面是官網的說明:

PyLucene is aPythonextension for accessing JavaLuceneTM. 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:

if not filename.endswith(‘.txt’):

continue

txtname=

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值