All pastes #746361 Raw Edit

Search Engine Solution

public python v1 · immutable
·published 2007-10-23 05:17 UTC
rendered paste body
import globfrom string import punctuationAvailableFiles = set()SearchDictionary = dict()StopList = []def ExtractLineWords(line):    returnwords = set()    linesplit = line.split()    numsplit = len(linesplit)    for i in range(numsplit):        linesplit[i] = linesplit[i].lower().strip(punctuation)        if linesplit[i] != "":            returnwords.add(linesplit[i])    return returnwordsdef ExtractFileWords(textFile):    filewords = set()    File = open(textFile, "r")    for line in File:        linewords = ExtractLineWords(line)        for word in linewords:            if word in StopList:                continue            if word in filewords:                continue            filewords.add(word)    File.close()    return filewordsdef IndexFile(textFile):    filewords = ExtractFileWords(textFile)    for word in filewords:        if word not in SearchDictionary:            SearchDictionary[word] = [textFile]        elif textFile not in SearchDictionary[word]:            SearchDictionary[word].append(textFile)def AddFiles(TextFiles):    for File in TextFiles:        AvailableFiles.add(str(File))        IndexFile(str(File))def find(SearchDictionary, CommandList):    if CommandList in SearchDictionary:        return SearchDictionary(CommandList)    else:        return set()def IsStoppedWord(word):    found = False    for stoppedword in StopList:        if word == stoppedword:            found = True    return foundprint "Welcome to compu-search.  This program will search through your files for any terms you input and tell you which .txt files they are located in.  Please use and and or to seperate multiple words depending on whether you want files with both words or either word.  Type 'quit' to quit the program."StopWords = open("stopwords.txt", "r")for line in StopWords:    StopList.append(line.split())print "Contains THE (sglquote):" , (str('the') in StopList[1])print "Contains THE (dblquote):" , (str("the") in StopList)print "IsStoppedWord(the)", IsStoppedWord("the")TextFiles = glob.glob('*.txt')AddFiles(TextFiles)loop = Truewhile loop:    Command = raw_input("Please enter search terms: ")    CommandList = Command.split()    for c in range(0,len(CommandList)):        CommandList[c] = CommandList[c].lower()    if CommandList[c] == 'quit':        print "Thank you for using my program."        break    # What we're expecting next    state = "searchterm"    # What operator to use    operator = "none"    # A counter variable    wordnumber = 1    for i in CommandList:        if state == "searchterm":            #If we're expecting a search term and get an operator, give an error            if i == "and" or i=="or":                print "AND and OR are operators, and cannot be used as search terms."                break            #For the first term, just set up the dictionary            if operator == "none":                if i in SearchDictionary:                    temporaryDict = set(SearchDictionary[i])                else:                    temporaryDict = dict() #Empty dictionary                        #For operators, do what you gotta do            elif operator == "and":                if i in SearchDictionary:                    temporaryDict = temporaryDict.intersection(SearchDictionary[i])                else:                    temporaryDict = dict() #Empty dictionary            elif operator == "or":                if i in SearchDictionary:                    temporaryDict = set(temporaryDict).union(set(SearchDictionary[i]))                #No else.                #If it doesn't have an entry, then the results are unaffected            #This should only happen if you've accepted an invalid operator            else:                print "Error: invalid operator -", operator            state = "operator"        elif state == "operator":            if i == "and" or i == "or":                operator = i            else:                print "Search terms should be seperated by the AND or OR operators"                print "Expected operator at:", i            state = "searchterm"        #If we've finished parsing the list and haven't been broken out, then print the results        if wordnumber == len(CommandList):            if len(temporaryDict) == 0:                print "No documents matched your search criteria."            else:                for filename in temporaryDict:                    print filename        wordnumber += 1