This commit is contained in:
parent
a904f76704
commit
7c5834de20
1 changed files with 5 additions and 6 deletions
11
ac_encode.py
11
ac_encode.py
|
@ -30,7 +30,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
|
||||||
tot0 = 0
|
tot0 = 0
|
||||||
tot1 = 0
|
tot1 = 0
|
||||||
if not adaptive:
|
if not adaptive:
|
||||||
p0 = c0 * 1 / (c0 + c1)
|
p0 = c0 / (c0 + c1)
|
||||||
ans = ""
|
ans = ""
|
||||||
charstack = [0] # how many undecided characters remain to print
|
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
|
w = b - a
|
||||||
if adaptive:
|
if adaptive:
|
||||||
cT = c0 + c1
|
cT = c0 + c1
|
||||||
p0 = c0 * 1 / cT
|
p0 = c0 / cT
|
||||||
boundary = a + int(p0 * w)
|
boundary = a + int(p0 * w)
|
||||||
|
|
||||||
# these warnings mean that some of the probabilities
|
# these warnings mean that some of the probabilities
|
||||||
|
@ -109,7 +109,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
|
||||||
return ans
|
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.
|
# must supply N, the number of source characters remaining.
|
||||||
assert c0 > 0
|
assert c0 > 0
|
||||||
assert c1 > 0
|
assert c1 > 0
|
||||||
|
@ -120,7 +120,7 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True):
|
||||||
tot1 = 0
|
tot1 = 0
|
||||||
model_needs_updating = True
|
model_needs_updating = True
|
||||||
if not adaptive:
|
if not adaptive:
|
||||||
p0 = c0 * 1 / (c0 + c1)
|
p0 = c0 / (c0 + c1)
|
||||||
ans = ""
|
ans = ""
|
||||||
|
|
||||||
u = 0
|
u = 0
|
||||||
|
@ -145,12 +145,11 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True):
|
||||||
# Then emulate the encoder's computations,
|
# Then emulate the encoder's computations,
|
||||||
# and tie (u,v) to tag along for the ride.
|
# and tie (u,v) to tag along for the ride.
|
||||||
while 1: # do-while
|
while 1: # do-while
|
||||||
firsttime = 0
|
|
||||||
if model_needs_updating:
|
if model_needs_updating:
|
||||||
w = b - a
|
w = b - a
|
||||||
if adaptive:
|
if adaptive:
|
||||||
cT = c0 + c1
|
cT = c0 + c1
|
||||||
p0 = c0 * 1 / cT
|
p0 = c0 / cT
|
||||||
boundary = a + int(p0 * w)
|
boundary = a + int(p0 * w)
|
||||||
if boundary == a:
|
if boundary == a:
|
||||||
boundary += 1
|
boundary += 1
|
||||||
|
|
Loading…
Reference in a new issue