Shannon

This is the place for queries that don't fit in any of the other categories.

Shannon

Hi,

I'm trying to complete this skeleton based on the Shannon's experience :

Code: Select all
`#!/usr/bin/env python# -*- coding: utf-8 -*-from math import logfrom random import random#==================== First part ==============def IncFreq(distribution:dict, letter:str):    """ increments the frequency of all letters of a text in a distribution.    """    if letter in distribution :        distribution[letter] += 1    else:        distribution[letter] = 1#______________________________________________________________________________def AnalFreq(distribution:dict, text:str):    """ computes a distribution of letters from a character string.    """    for letter in text:        distribution[letter] = IncFreq(distribution, letter)#______________________________________________________________________________def string2distribution(text:str) -> dict:    """ computes the sum of all frequencies in a distribution.    """    distribution = dict()    AnalFreq(distribution, text)    return distribution#______________________________________________________________________________def TotalFreq(distribution:dict) -> int:    """ computes the sum of all frequencies in a distribution.    """    return sum(distribution.values())#______________________________________________________________________________def string2information(text:str) -> float:    """ computes information conveyed by every letter from a character string.    """    info = dict()    distribution = string2distribution(text)    TotalFreq = TotalFreq(dict)    for letter in distribution :        info[letter] = log(TotalFreq/distribution[letter], 2)    return info #______________________________________________________________________________def string2entropy(text:str) -> float:    """ computes the entropy of the character set of a character string.    """        dist = string2distribution(text)    entropie = 0    for letter in dist :         entropie -= ((dist(letter)/TotalFreq(dist))*log(dist(letter)/TotalFreq(dist), 2))    return entropie     #______________________________________________________________________________def CumulDistrib(distribution:dict) -> dict:    """ computes the cumulative distribution of adistribution.         The order of the letters in the cumul is the ASCII increasing order.     """        T = TotalFreq(distribution)    Frequence = dict()    cumul = dict()    old = 0    for letter in sorted(cumul.keys()) :        Frequence[letter] = distribution[letter]/T            for letter in sorted(distribution.keys()) :        cumul[letter] = distribution[letter] + old        old = cumul[letter]        return cumul    #_________________________________________________________________________________def FindFirstGreaterInCD(cumuldistribution:dict, freq:float) -> str:    """ searches the first letter whose cumulated distribution is bigger than a given value.    """    for letter in cumuldistribution :        if cumuldistribution[letter] >= freq :            return letter         #_________________________________________________________________________________def RandLetter(cumuldistribution:dict) -> str:    """ Generates a random letter following a given cumulative distribution.    """    random.randint(1,cumuldistribution)    for letter in cumuldistribution:        return letter#_________________________________________________________________________________def RandText(cumuldistribution:dict, length:int) -> list:    """ Generates a random text following a given cumulative distribution.    """    pass#_________________________________________________________________________________def distribution2string(distribution:dict, length:int) -> str:    """ computes a string following a given distribution.    """    pass#================== PROGRAMME PRINCIPAL =============if __name__ == '__main__':                file = open('fable1.txt', 'r')    text = file.read()# close filef_in.close()`

I think that the function FindFirstGreaterInCD and RandLetter are not good. Anyone can help me please ?

Best regards.
Last edited by stranac on Sat Mar 29, 2014 11:30 am, edited 1 time in total.
Reason: First post lock.
Hero13

Posts: 1
Joined: Sat Mar 29, 2014 10:10 am