0% found this document useful (0 votes)
5 views4 pages

Python Shannon-Fano Coding Implementation

The document outlines the implementation of the Shannon-Fano Coding algorithm in Python, detailing the steps involved in arithmetic coding. It includes source code for calculating character frequencies, building cumulative frequencies, and encoding a string into a single number. The program reads text from a file, processes it, and outputs the encoded value along with character frequencies.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Shannon-Fano Coding Implementation

The document outlines the implementation of the Shannon-Fano Coding algorithm in Python, detailing the steps involved in arithmetic coding. It includes source code for calculating character frequencies, building cumulative frequencies, and encoding a string into a single number. The program reads text from a file, processes it, and outputs the encoded value along with character frequencies.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

AIM:

To write a python program to implement Shannon-Fano Coding algorithm.

ALGORITHM:

STEP 1: Determine the probability of each string: before using arithmetic coding,you
need to know the probability of each string.
STEP 2: Encode the string into a number: Arithmetic coding encodes an entire string
into a single number, called a code, instead of encoding each character
individually.
STEP 3: Create a code string: The code string represents a fractional value on a
number line between 0 and 1.
STEP 4: Use adaptive techniques: Arithmetic coding uses adaptive techniques to
make it easier to achieve the entropy rate.
SOURCE CODE:
def calculate_frequencies(text):
frequency = {}
for char in text:
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
return frequency
def build_cumulative_frequency(frequency):
total = sum([Link]())
cumulative_freq = {}
cumulative_sum = 0
for char in sorted([Link]()):
cumulative_sum += frequency[char]
cumulative_freq[char] = cumulative_sum / total
return cumulative_freq
def arithmetic_encode(text, cumulative_freq):
low = 0.0
high = 1.0
for char in text:
range = high - low
high = low + range * cumulative_freq[char]
low = low + range * (cumulative_freq[char] - (frequency[char] /
sum([Link]())))
return (low + high) / 2
filename = input("Enter the filename: ")
try:
with open(filename, 'r') as file:
file_content = [Link]().strip()
except FileNotFoundError:
print("File not found. Please check the filename and try again.")
exit()
print("File content loaded for arithmetic encoding.")
frequency = calculate_frequencies(file_content)
cumulative_freq = build_cumulative_frequency(frequency)
encoded_value = arithmetic_encode(file_content, cumulative_freq)
print("Encoded value:", encoded_value)
print("Character Frequencies:")
for char, freq in [Link]():
print(f"'{char}': {freq}")
INPUT AND OUTPUT:

INPUT TEXT FILE – [Link]


CONTENT:
Despite the simplicity of use cases, it is difficult to identify the involved actors
and use cases. One of the common issue is the completeness of the involved
actors and relevant use cases. This is often due to a lack of understanding of
the system and its requirements. Hence, use cases help to discuss an high-level
structured view of the system, its functionality and the relevant actors around
the system. Another common difficulty is the identification of the trade-offs
between generality and specificity. On the one hand, general use cases could lack
information about the system functionalities. On the other hand, detailed use
cases could try to over specify some design aspects.
RESULT:
Thus the Arithmetic coding algorithm is implemented in python and the output is
verified.

You might also like