MCPcopy Create free account
hub / github.com/ActiveState/code / train

Function train

recipes/Python/306626_arithmetic_coder/recipe-306626.py:20–43  ·  view source on GitHub ↗

text -> 0-order probability statistics as a dictionary Text must not contain the NUL (0x00) character because that's used to indicate the end of data.

(text)

Source from the content-addressed store, hash-verified

18R = Rational.rational
19
20def train(text):
21 """text -> 0-order probability statistics as a dictionary
22
23 Text must not contain the NUL (0x00) character because that's
24 used to indicate the end of data.
25 """
26 assert "\x00" not in text
27 counts = {}
28 for c in text:
29 counts[c]=counts.get(c,0)+1
30 counts["\x00"] = 1
31 tot_letters = sum(counts.values())
32
33 tot = 0
34 d = {}
35 prev = R(0)
36 for c, count in counts.items():
37 next = R(tot + count, tot_letters)
38 d[c] = (prev, next)
39 prev = next
40 tot = tot + count
41 assert tot == tot_letters
42
43 return d
44
45
46def encode(text, probs):

Callers 1

recipe-306626.pyFile · 0.85

Calls 4

sumFunction · 0.50
getMethod · 0.45
valuesMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected