主机平台: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:</