This commit is contained in:
Connor 2018-09-10 13:30:55 -07:00 committed by GitHub
parent a904f76704
commit 7c5834de20

View File

@ -30,7 +30,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
tot0 = 0
tot1 = 0
if not adaptive:
p0 = c0 * 1 / (c0 + c1)
p0 = c0 / (c0 + c1)
ans = ""
charstack = [0] # how many undecided characters remain to print
@ -38,7 +38,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
w = b - a
if adaptive:
cT = c0 + c1
p0 = c0 * 1 / cT
p0 = c0 / cT
boundary = a + int(p0 * w)
# these warnings mean that some of the probabilities
@ -109,7 +109,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
return ans
def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True):
def decode(string, N, c0=BETA0, c1=BETA1, adaptive=True):
# must supply N, the number of source characters remaining.
assert c0 > 0
assert c1 > 0
@ -120,7 +120,7 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True):
tot1 = 0
model_needs_updating = True
if not adaptive:
p0 = c0 * 1 / (c0 + c1)
p0 = c0 / (c0 + c1)
ans = ""
u = 0
@ -145,12 +145,11 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True):
# Then emulate the encoder's computations,
# and tie (u,v) to tag along for the ride.
while 1: # do-while
firsttime = 0
if model_needs_updating:
w = b - a
if adaptive:
cT = c0 + c1
p0 = c0 * 1 / cT
p0 = c0 / cT
boundary = a + int(p0 * w)
if boundary == a:
boundary += 1