主機平台: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=