예제 #1
0
def decodeWordListToBits(words,
                         maxDigits,
                         markovChain,
                         previousWord=config.startSymbol,
                         wordsPerState=1):

    bitsRange = ["0", "1"]
    bitsField = bigBitField.BigBitField()
    wordsUsed = 0
    markovChainDict = utils.markovChainToDictionary(markovChain)
    lastTime = time.time()
    secondsForStatusPrint = 20

    for word in words:
        bitsRange = decodeWordToBitsRange(
            word, previousWord, markovChainDict,
            maxDigits - bitsField.totalFieldLen(), bitsRange)
        wordsUsed = wordsUsed + 1

        # compute previous word (or bigram) for next iteration
        if wordsPerState == 1:
            previousWord = word
        elif wordsPerState == 2:
            if word == config.startSymbol:
                previousWord = (config.startSymbol, config.startSymbol)
            else:
                previousWord = (previousWord[1], word)

        # simplify range, remove bits and add them to the field
        bitsRange2 = utils.removeCommonBitsInRange(bitsRange)
        bitsRemovedLen = len(bitsRange[0]) - len(bitsRange2[0])
        if bitsRemovedLen + bitsField.totalFieldLen() > maxDigits:
            bitsRemovedLen = maxDigits - bitsField.totalFieldLen()
        bitsField.pushQueueNBits(bitsRange[0][0:bitsRemovedLen])
        bitsRange = bitsRange2

        if time.time() - lastTime > secondsForStatusPrint:
            print(" - decoded bits so far: " + repr(bitsField.totalFieldLen()))
            lastTime = time.time()

        # we exit when our range describes only one number
        if bitsField.totalFieldLen() == maxDigits:
            break
        if bitsField.totalFieldLen(
        ) == maxDigits - 1 and bitsRange[0][0] == bitsRange[1][0]:
            bitsField.pushQueueNBits(bitsRange[0][0])
            break

    return (bitsField, wordsUsed)
def encodeBitsToWordList(bitsField,
                         markovChain,
                         startWord=config.startSymbol,
                         wordsPerState=1):

    bitsField = bitsField.copy()
    lastTime = time.time()
    secondsForStatusPrint = 20

    words = []
    nextRange = ["0", "1"]
    markovChainDict = utils.markovChainToDictionary(markovChain)

    while True:
        # encode to one word
        (word, nextRange) = encodeBitsToWord(bitsField, nextRange, startWord,
                                             markovChainDict)
        words.append(word)

        # compute previous word (or bigram) for next iteration
        if wordsPerState == 1:
            startWord = word
        elif wordsPerState == 2:
            if word == config.startSymbol:
                startWord = (config.startSymbol, config.startSymbol)
            else:
                startWord = (startWord[1], word)

        # optimization, remove start of range when it is identical in both sides
        nextRange2 = utils.removeCommonBitsInRange(nextRange)
        bitsField.popFirstNBits(len(nextRange[0]) - len(nextRange2[0]))
        nextRange = nextRange2

        if time.time() - lastTime > secondsForStatusPrint:
            print " - remaining bits: " + repr(bitsField.totalFieldLen())
            lastTime = time.time()

        # we exit when our range describes only to our number
        if bitsField.totalFieldLen() == 0 or (bitsField.totalFieldLen() == 1
                                              and nextRange[0][0]
                                              == nextRange[1][0]):
            break

    return words
예제 #3
0
def decodeWordListToBits(words, maxDigits, markovChain, previousWord = config.startSymbol, wordsPerState = 1):

	bitsRange = ["0", "1"]
	bitsField = bigBitField.BigBitField()
	wordsUsed = 0
	markovChainDict = utils.markovChainToDictionary(markovChain)
	lastTime = time.time()
	secondsForStatusPrint = 20

	for word in words:
		bitsRange = decodeWordToBitsRange(word, previousWord, markovChainDict, maxDigits - bitsField.totalFieldLen(), bitsRange)
		wordsUsed = wordsUsed + 1

		# compute previous word (or bigram) for next iteration
		if wordsPerState == 1:
			previousWord = word
		elif wordsPerState == 2:
			if word == config.startSymbol:
				previousWord = (config.startSymbol, config.startSymbol)
			else:
				previousWord = (previousWord[1], word)


		# simplify range, remove bits and add them to the field
		bitsRange2 = utils.removeCommonBitsInRange(bitsRange)
		bitsRemovedLen = len(bitsRange[0]) - len(bitsRange2[0])
		if bitsRemovedLen + bitsField.totalFieldLen() > maxDigits:
			bitsRemovedLen = maxDigits - bitsField.totalFieldLen()
		bitsField.pushQueueNBits(bitsRange[0][0:bitsRemovedLen])
		bitsRange = bitsRange2

		if time.time()-lastTime > secondsForStatusPrint:
			print " - decoded bits so far: " + repr(bitsField.totalFieldLen())
			lastTime = time.time()

		# we exit when our range describes only one number
		if bitsField.totalFieldLen() == maxDigits:
			break
		if bitsField.totalFieldLen() == maxDigits - 1 and bitsRange[0][0] == bitsRange[1][0]:
			bitsField.pushQueueNBits(bitsRange[0][0])
			break

	return (bitsField, wordsUsed)
예제 #4
0
def encodeBitsToWordList(bitsField, markovChain, startWord = config.startSymbol, wordsPerState = 1):

	bitsField = bitsField.copy()
	lastTime = time.time()
	secondsForStatusPrint = 20

	words = []
	nextRange = ["0", "1"]
	markovChainDict = utils.markovChainToDictionary(markovChain)

	while True:
		# encode to one word
		(word, nextRange) = encodeBitsToWord(bitsField, nextRange, startWord, markovChainDict)
		words.append(word)

		# compute previous word (or bigram) for next iteration
		if wordsPerState == 1:
			startWord = word
		elif wordsPerState == 2:
			if word == config.startSymbol:
				startWord = (config.startSymbol, config.startSymbol)
			else:
				startWord = (startWord[1], word)

		# optimization, remove start of range when it is identical in both sides
		nextRange2 = utils.removeCommonBitsInRange(nextRange)
		bitsField.popFirstNBits(len(nextRange[0])-len(nextRange2[0]))
		nextRange = nextRange2

		if time.time()-lastTime > secondsForStatusPrint:
			print " - remaining bits: " + repr(bitsField.totalFieldLen())
			lastTime = time.time()

		# we exit when our range describes only to our number
		if bitsField.totalFieldLen() == 0 or (bitsField.totalFieldLen() == 1 and nextRange[0][0] == nextRange[1][0]):
			break

	return words
            lastTime = time.time()

        # we exit when our range describes only to our number
        if bitsField.totalFieldLen() == 0 or (bitsField.totalFieldLen() == 1
                                              and nextRange[0][0]
                                              == nextRange[1][0]):
            break

    return words


if __name__ == '__main__':
    print "testing fixedSizeCode.py"

    testMarkov = config.testMarkov
    testMarkovDict = utils.markovChainToDictionary(testMarkov)

    testMarkov2 = config.testMarkov2
    testMarkovDict2 = utils.markovChainToDictionary(testMarkov2)

    # this is "01000110 01011010 11111111"
    testBitField = bigBitField.BigBitField([70, 90, 255])

    print "A:"
    print encodeBitsToWord(testBitField, ["0", "1"], config.startSymbol,
                           testMarkovDict) == ('A', ('00', '01'))
    print "A2:"
    print encodeBitsToWord(testBitField, ["0", "1"],
                           (config.startSymbol, config.startSymbol),
                           testMarkovDict2) == ('A', ('00', '01'))
    print "B:"
예제 #6
0
		if time.time()-lastTime > secondsForStatusPrint:
			print " - remaining bits: " + repr(bitsField.totalFieldLen())
			lastTime = time.time()

		# we exit when our range describes only to our number
		if bitsField.totalFieldLen() == 0 or (bitsField.totalFieldLen() == 1 and nextRange[0][0] == nextRange[1][0]):
			break

	return words


if __name__ == '__main__':
	print "testing fixedSizeCode.py"

	testMarkov = config.testMarkov
	testMarkovDict = utils.markovChainToDictionary(testMarkov)

	testMarkov2 = config.testMarkov2
	testMarkovDict2 = utils.markovChainToDictionary(testMarkov2)

	# this is "01000110 01011010 11111111"
	testBitField = bigBitField.BigBitField([70, 90, 255])

	print "A:"
	print encodeBitsToWord(testBitField, ["0", "1"], config.startSymbol, testMarkovDict) == ('A', ('00', '01'))
	print "A2:"
	print encodeBitsToWord(testBitField, ["0", "1"], (config.startSymbol, config.startSymbol), testMarkovDict2) == ('A', ('00', '01'))
	print "B:"
	print encodeBitsToWord(testBitField, ["0", "1"], "A", testMarkovDict) == ('B', ('01', '01'))
	print "B2:"
	print encodeBitsToWord(testBitField, ["0", "1"], (config.startSymbol, "A"), testMarkovDict2) == ('B', ('01', '01'))