Shannon

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

Shannon

Postby Hero13 » Sat Mar 29, 2014 10:15 am

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 log
from 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 a
distribution.
         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 file
f_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

Return to General Coding Help

Who is online

Users browsing this forum: bobdx_cool, micseydel, W3C [Linkcheck] and 2 guests