Question: do with a python frequency list is to compute the frequencies of each prefix. A prefix of a string is each substring that starts at
do with a python frequency list is to compute the frequencies of each prefix. A prefix of a string is each substring that starts at the start of the string, including the string itself. For example, the prefixes of "house" are "h", "ho", "hou", "hous", and "house". Write a function get_prefix_frequencies(frequencies) which takes a list of (token, count) pairs as produced by Task 1, and creates a similar list containing all the prefix counts. If you did this correctly, you should be able to do this on your interactive console:
>>> prefix_frequencies = get_prefix_frequencies ( frequencies )
>>> prefix_frequencies . sort ()
>>> prefix_frequencies [10383:10387] [( bist , 691) , (bistr , 576) , (bistro , 576) , (bit , 337003)]
def read_frequency_file(freq_file_name): """ Loads (token,frequency) pairs from a file with line format "". """ frequencies = [] with open(freq_file_name, encoding='utf8') as f: for j, line in enumerate(f): token = line.split() frequencies.append((token[0], int(token[1]))) return frequencies
def get_prefix_frequencies(frequencies): """ Computes a list of (prefix,frequency) pairs from a list of (token,frequency) pairs by counting every prefix [0:i] of each token, and adding the counts for each prefix. """
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
