43 lines
1.0 KiB
Python
43 lines
1.0 KiB
Python
import logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.FileHandler('arithmetic_coding.log'),
|
|
logging.StreamHandler()
|
|
]
|
|
)
|
|
dict={
|
|
"A":[0,0.1],
|
|
"R":[0.1,0.2],
|
|
"I":[0.2,0.4],
|
|
"T":[0.4,0.6],
|
|
"H":[0.6,0.7],
|
|
"M":[0.7,0.8],
|
|
"E":[0.8,0.9],
|
|
"C":[0.9,1],
|
|
}
|
|
|
|
def count(word):
|
|
low = 0.0
|
|
high = 1.0
|
|
|
|
for char in word:
|
|
if char not in dict:
|
|
raise ValueError(f"Character '{char}' not in probability dictionary")
|
|
|
|
# Calculate new range
|
|
range_ = high - low
|
|
new_low = low + dict[char][0] * range_
|
|
new_high = low + dict[char][1] * range_
|
|
|
|
# Update range
|
|
low, high = new_low, new_high
|
|
print(f'({low:.15f}, {high:.15f})')
|
|
logging.info(f" Range updated to: [{new_low:.15f}, {new_high:.15f}] (width: {new_high-new_low:.15f})")
|
|
# Return the final range
|
|
return (low + high) / 2
|
|
|
|
if __name__ == "__main__":
|
|
count("ARITHMETIC")
|