Home > Programming > A simple search engine using Python

A simple search engine using Python


Here is  a simple search engine using python. Python has many internal functions which enable searching much easier. In this program, I’ve considered the number of times the search item is present. Still this can be improved based on giving different weights based on the place the search item is found, for example if the search word is found in the title the more weight has to be given and including number of hits that document has got already etc., It works in Linux machines since I’ve used linux based commands.
# Author: Vasanth Raja Chittampally



import re
import sys
import os
def extractURLs():
	os.system('ls ./html>files.txt')
	f=open('files.txt','r')
	l=f.readlines()
	f.close()
	db={}
	for fl in l:
		filename="html/"+fl.strip()
		f=open(filename,'r')
		text= f.readlines()
		f.close()
		length1=0
		if len(sys.argv)>0 :
			if sys.argv[len(sys.argv)-1]=='--noicase':
				for i in range(1,len(sys.argv)-1):
					if len(sys.argv[i])>2:
						parse=re.findall(sys.argv[i],str(text))
						if parse:
							length1=length1+len(parse)
				if length1>0:
					db[fl.strip()]=length1
			else:
				for i in range(1,len(sys.argv)):
					if len(sys.argv[i])>2:
						parse=re.findall(sys.argv[i],str(text),re.IGNORECASE)
						if parse:
							length1=length1+len(parse)	

				if length1>0:
					db[fl.strip()]=length1

		#print filename,len(parse)

	sorteddic= sorted([(value,key) for (key,value) in db.items()],reverse=True)
	if len(db)>15 :
		length=15
	else:
		length=len(db)
	for i in range(length):
		print sorteddic[i][1]

def main():
	if len(sys.argv)==1:
		print 'provide the search entries'
	else:
		extractURLs()
if __name__=='__main__':
	main()
Advertisements
  1. May 23, 2013 at 11:35 pm

    You ought to be a part of a contest for one of the greatest
    blogs on the net. I’m going to recommend this website!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: