grammar.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:but_sentiment 作者: MixedEmotions 项目源码 文件源码
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
#################################################################
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号