def encodeDataFromFile(inputFile, outputFile, markovInputFile, textFileFormat, wordsPerState=1): initTime = time.time() f = open(markovInputFile, 'r') jsonData = f.read() f.close() markovData = json.JSONDecoder().decode(jsonData) if (wordsPerState == 1 and type(markovData[0][0]) != str and type(markovData[0][0]) != unicode) or ( wordsPerState == 2 and type(markovData[0][0]) != list): raise RuntimeError( "error; markov chain structure doesn't match wordsPerState value") inputData = [] f = open(inputFile, 'rb') char = None while char != "": char = f.read(1) if char != "": inputData.append(ord(char)) f.close() initTimeCode = time.time() encodedData = encodeDataToWordList(inputData, 4, markovData, wordsPerState) # save if textFileFormat: outputData = utils.wordListToText(encodedData) else: outputData = json.JSONEncoder().encode(encodedData) endTimeCode = time.time() f = open(outputFile, 'w') f.write(outputData) f.close() print "wrote " + repr(len(inputData) * 8) + " bits" print "elapsed time: " + repr(time.time() - initTime) + " seconds" print " - encoding time: " + repr(endTimeCode - initTimeCode) + " seconds"
def encodeDataFromFile(inputFile, outputFile, markovInputFile, textFileFormat, wordsPerState = 1): initTime = time.time() f = open(markovInputFile, 'r') jsonData = f.read() f.close() markovData = json.JSONDecoder().decode(jsonData) if (wordsPerState == 1 and type(markovData[0][0]) != str and type(markovData[0][0]) != unicode) or (wordsPerState == 2 and type(markovData[0][0]) != list): raise RuntimeError("error; markov chain structure doesn't match wordsPerState value") inputData = [] f = open(inputFile, 'rb') char = None while char != "": char = f.read(1) if char != "": inputData.append(ord(char)) f.close() initTimeCode = time.time() encodedData = encodeDataToWordList(inputData, 4, markovData, wordsPerState) # save if textFileFormat: outputData = utils.wordListToText(encodedData) else: outputData = json.JSONEncoder().encode(encodedData) endTimeCode = time.time() f = open(outputFile, 'w') f.write(outputData) f.close() print "wrote " + repr(len(inputData) * 8) + " bits" print "elapsed time: " + repr(time.time() - initTime) + " seconds" print " - encoding time: " + repr(endTimeCode - initTimeCode) + " seconds"
if wordsPerState == None: wordsPerState = 1 if mode == "createMarkov": print "creating markov chain" print "using wordsPerState = " + repr(wordsPerState) markov.createMarkovChainFromFile(inputFile, outputFile, wordsPerState) print "done" elif mode == "testMarkov": print "testing markov chain" markov.testMarkovChain(inputFile) print "done" elif mode == "genTextWithMarkov": print "generating text using markov chain" print "using wordsPerState = " + repr(wordsPerState) print( utils.wordListToText( markov.generateTextUsingMarkovChain(inputFile, wordsPerState))) print "done" elif mode == "encode": markovInputFile = args.markovInput print "encoding file (number to text) using markov chain, saving as json" variableSizeCode.encodeDataFromFile(inputFile, outputFile, markovInputFile, False, wordsPerState) print "done" elif mode == "decode": markovInputFile = args.markovInput print "decoding file (json text to number) using markov chain" variableSizeCode.decodeDataFromFile(inputFile, outputFile, markovInputFile, False, wordsPerState) print "done" elif mode == "encodeFullText": markovInputFile = args.markovInput
if wordsPerState == "1" or wordsPerState == "2": wordsPerState = int(wordsPerState) if wordsPerState == None: wordsPerState = 1 if mode == "createMarkov": print "creating markov chain" print "using wordsPerState = " + repr(wordsPerState) markov.createMarkovChainFromFile(inputFile, outputFile, wordsPerState) print "done" elif mode == "testMarkov": print "testing markov chain" markov.testMarkovChain(inputFile) print "done" elif mode == "genTextWithMarkov": print "generating text using markov chain" print "using wordsPerState = " + repr(wordsPerState) print(utils.wordListToText(markov.generateTextUsingMarkovChain(inputFile, wordsPerState))) print "done" elif mode == "encode": markovInputFile = args.markovInput print "encoding file (number to text) using markov chain, saving as json" variableSizeCode.encodeDataFromFile(inputFile, outputFile, markovInputFile, False, wordsPerState) print "done" elif mode == "decode": markovInputFile = args.markovInput print "decoding file (json text to number) using markov chain" variableSizeCode.decodeDataFromFile(inputFile, outputFile, markovInputFile, False, wordsPerState) print "done" elif mode == "encodeFullText": markovInputFile = args.markovInput print "encoding file (number to text) using markov chain, saving as txt" variableSizeCode.encodeDataFromFile(inputFile, outputFile, markovInputFile, True, wordsPerState)