#!/usr/bin/python -t #$Date: 2006-11-16 22:02:45 +0800 (Thu, 16 Nov 2006) $ def funcLhsContextTrimOrPad(strLhsContext, intNumCharsBufferContext): '''Takes a string that prepresents all of the characters on a line to the LEFT of a key word and the number of characters to show on either side of the keyword. If len(strLhsContext) > intNumCharsBufferContext, the string is trimmed to fit. If it's too short, spaces are added to the left side to pad it to the proper length.''' strLhsContextTrimedOrPadded = strLhsContext intLengthLhsContext = len(strLhsContext) if intLengthLhsContext > intNumCharsBufferContext: strLhsContextTrimedOrPadded = strLhsContext[-(intNumCharsBufferContext+1):-1] elif intLengthLhsContext < intNumCharsBufferContext: strLhsContextTrimedOrPadded = strLhsContext.rjust(intNumCharsBufferContext) return strLhsContextTrimedOrPadded def funcRhsContextTrimOrPad(strRhsContext, intNumCharsBufferContext): '''Takes a string that prepresents all of the characters on a line to the RIGHT of a key word and the number of characters to show on either side of the keyword. If len(strRhsContext) > intNumCharsBufferContext, the string is trimmed to fit. If it's too short, spaces are added to the right side to pad it to the proper length.''' strRhsContextTrimedOrPadded = strRhsContext intLengthLhsContext = len(strRhsContext) if intLengthLhsContext > intNumCharsBufferContext: strRhsContextTrimedOrPadded = strRhsContext[:(intNumCharsBufferContext+1)] elif intLengthLhsContext < intNumCharsBufferContext: strRhsContextTrimedOrPadded = strRhsContext.ljust(intNumCharsBufferContext) return strRhsContextTrimedOrPadded def funcReturnPaddedStrNumber(strThisNum, strMaxNum): '''Takes two strings: the numeric string to process and the longest numeric string being processed. Leading zeroes will be added to strThisNum so that it has the sae number of characters as strMaxNum.''' intLenStrMaxNum = len(strMaxNum) if len(strThisNum) < intLenStrMaxNum: strThisNum = strThisNum.rjust(intLenStrMaxNum, '0') return strThisNum def funcWriteStrToFile(strToWrite, strDestinationFilePath): '''Writes a string to a file.''' fileToWrite = open(strDestinationFilePath, 'w+') fileToWrite.write(strToWrite) fileToWrite.close() def funcRtnStringKwicDisplay(strTextFilePath, strNeedle, intNumCharsBufferContext=20): ''' Processes strTextFilePath for key words in strNeedle. ''' strKwicDisplay = '' #---------------------------------------------- fileHaystack = open(strTextFilePath) #---------------------------------------------- # Used within next for loop #---------------------------------------------- intNumMatchedLines = 0 intLineNum = 0 listLinesAndLineInfo = [] #---------------------------------------------- # Find and process matching lines, fill up listLinesAndLineInfo, pad line # and match tallies so that columns will line up. #---------------------------------------------- for line in fileHaystack: intLineNum += 1 line = line.strip() intIndexStartNeedleString = line.count(strNeedle) if intIndexStartNeedleString > 0: intNumMatchedLines += 1 #print line+"\n" listLhsContextRhsContext = line.split(strNeedle) strLhsContext = funcLhsContextTrimOrPad(listLhsContextRhsContext[0], intNumCharsBufferContext) strRhsContext = funcRhsContextTrimOrPad(listLhsContextRhsContext[1], intNumCharsBufferContext) listThisLine = [str(intNumMatchedLines), str(intLineNum), strLhsContext, strRhsContext] listLinesAndLineInfo.append(listThisLine) fileHaystack.close() #---------------------------------------------- # Create strKwicDisplay #---------------------------------------------- for listOneLineAndLineInfo in listLinesAndLineInfo: strNumMatch = funcReturnPaddedStrNumber(listOneLineAndLineInfo[0], str(intNumMatchedLines)) strLineNum = funcReturnPaddedStrNumber(listOneLineAndLineInfo[1], str(intLineNum)) strKwicDisplay += '('+strNumMatch+'.) line #'+strLineNum+' '+listOneLineAndLineInfo[2]+' '+strNeedle+' '+listOneLineAndLineInfo[3]+'\n' return strKwicDisplay strKwicDisplay = funcRtnStringKwicDisplay('../lexical-resources/The-Adventures-of-Tom-Sawyer/The-Adventures-of-Tom-Sawyer-noheader.txt', 'showed') funcWriteStrToFile(strKwicDisplay, 'kwic-display.txt')