Question: Write a Python program that takes two filenames as inputs. The output of the program should be the Bhattacharyya distance between the single letter frequency

Write a Python program that takes two filenames as inputs. The output of the program should be the Bhattacharyya distance between the single letter frequency distributions resulting from each of the files, respectively.

The Python function that I have for the Bhattacharyya distance is as follows:

import math def bhatt_dist(D1, D2, n): BCSum = 0 for i in range(n): BCSum += math.sqrt(D1[i] * D2[i]) DBValue = - math.log(BCSum) return DBValue D1 = [1 , 2, 1, 2] D2 = [ 59 ,100, 41, 100] print (bhatt_dist(D1, D2, 2))

The Python function that I have that takes one file name and creates a single letter frequency distribution for all the chars (bytes) occuring in the file is as follows:

def charFreqDistribution(): """ Python Function that takes a file name. Creates a single letter frequency distribution for all the chars (i.e. bytes) occurring in the file. """ # Reading file name fileName = input(" Input file name: "); # Dictionary to store char frequency charDict = {};

# Counting total number of characters totalCharCount = 0; # Reading data from file try: # Opening file in read mode file = open(fileName, "r");

# Reading data from file for line in file: # Removing white spaces line = line.strip(); # Converting to lower case line = line.lower(); # Iterating over each character for ch in line: # Searching for character as key in dictionary if ch in charDict.keys(): # If already present just increment count charDict[ch] += 1; else: # Assign value 1 charDict[ch] = 1; # Incrementing total number of chars totalCharCount += 1; # Building frequency distribution for key in charDict.keys(): charDict[key] = (charDict[key] / totalCharCount) * 100; # Printing frequency distribution print(" Frequency Distribution: Key \t Frequency Distribution ______________________________ "); for key in charDict.keys(): print(" " + key + " \t " + str(charDict[key]) + "% "); print(" "); # Closing file file.close(); except exception as ex: print(ex); # Handling exceptions print(" Sorry!!! Unable to open file... ");

# Calling function charFreqDistribution();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!