human
I am working on the file "MoL_June15/lab-inference.ipynb".
The first task is:
Counting derivations
Another interesting question is

how many analyses of a given sentence do we have?

This question is very simple to answer for acyclic hypergraphs and it turns out to be a special case of the inside recursion.
    N(v) = 
        1                           if v is terminal
        0                           if v is nonterminal and BS(v) is empty
        \sum_{e \in BS(v)} 1 * \prod_{u \in tail(e)} N(u)   
                                    otherwise

Compare the definition above with the inside recursion presented earlier.
Also compare the program below with the inside computation and comment on the differences.
Can you explain this recursion?
Can you write Python code for it?
assistant
#Here is Python code to compare the definition above with the inside recursion presented earlier.

def counting(forest, start):  # acyclic hypergraph
    N = dict()
    
    def get_count(symbol):
        w = N.get(symbol, None)
        if w is not None:
            return w
        incoming = forest.get(symbol, set())
        if len(incoming) == 0:  # terminals have already been handled, this must be a nonterminal dead end
            N[symbol] = w
            return 0
        w = 0
        for rule in incoming:
            k = 1
            for child in rule.rhs:
                k *= get_count(child)
            w += k
        N[symbol] = w
        return w
    
    # handles terminals
    for sym in forest.terminals:
        N[sym] = 1
    # handles nonterminals
    #for sym in forest.nonterminals:
    #    get_inside(sym)
    get_count(start)
        
    return N

N = counting(forest, goal)