import glob from string import punctuation AvailableFiles = 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 returnwords def 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 filewords def 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() print "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 += line TextFiles = glob.glob('*.txt') AddFiles(TextFiles) loop = True while loop: Command = raw_input("Please enter search terms: ") CommandList = Command.split() for c in range(0,len(CommandList)): CommandList[c] = CommandList[c].lower() for i in CommandList: temporaryDict = set(SearchDictionary[i]) if i == "and": newDict = temporaryDict.intersection(SearchDictionary[i]) print newDict continue elif i == "or": newDict = set(temporaryDict).union(set(SearchDictionary[i])) print newDict continue else: newDict = SearchDictionary[i] print newDict continue if CommandList[c] == 'quit': print "Thank you for using my program." break