def induce_pcfg(start, productions):
"""
Induce a PCFG grammar from a list of productions.
The probability of a production A -> B C in a PCFG is:
| count(A -> B C)
| P(B, C | A) = --------------- where \* is any right hand side
| count(A -> \*)
:param start: The start symbol
:type start: Nonterminal
:param productions: The list of productions that defines the grammar
:type productions: list(Production)
"""
# Production count: the number of times a given production occurs
pcount = {}
# LHS-count: counts the number of times a given lhs occurs
lcount = {}
for prod in productions:
lcount[prod.lhs()] = lcount.get(prod.lhs(), 0) + 1
pcount[prod] = pcount.get(prod, 0) + 1
prods = [ProbabilisticProduction(p.lhs(), p.rhs(),
prob=pcount[p] / lcount[p.lhs()])
for p in pcount]
return PCFG(start, prods)
#################################################################
# Helper functions for reading productions
#################################################################
评论列表
文章目录