词汇丰富性如香农熵;Python

2024-09-28 03:16:09 发布

您现在位置:Python中文网/ 问答频道 /正文

词汇丰富度的熵公式是

enter image description here

概率p-ith是用V-ith除以N来计算的,其中N是文本中标记的总数,V-ith是特定类型出现的次数(至少我是这么理解的)。在

所以,如果我有一个字符串the, the, the, a, a, over, love, one, tree 有9tokens,但只有6types。在

V-'theth'(据我所知)将是3,因此{}将被计算为{}。^{{cd8>那么{cd8}就可以了。H在这个实例中是-100*((0.33*log0.33 + 0.22*log0.22 + 0.11*log0.11 + 0.11*log0.11 + 0.11*log0.11+ 0.11*log0.11)/log9)

虽然我可以在Python中获得字符串(标记)的长度:

 string = ['the', 'the', 'the', 'a', 'a', 'over', 'love', 'one', 'tree']
 len(string)
 9

以及类型的数量:

^{pr2}$

我不太清楚如何用Python计算这个公式。 谢谢。在

资料来源:戴尔、莫伊尔和萨默斯(第551页)《自然语言处理手册》(2000年)。https://books.google.at/books?id=VoOLvxyX0BUC&pg=PA551&lpg=PA551&dq=entropy+vocabulary+richness&source=bl&ots=wucWFF1Rn_&sig=Hms1qwhXlcOaPEXI84eDqxsTEdo&hl=en&sa=X&ved=0CC8Q6AEwAmoVChMIjvvQnvPVxwIVhJ5yCh35ZAb_#v=onepage&q&f=false


Tags: the字符串标记tree类型stringbooksone
1条回答
网友
1楼 · 发布于 2024-09-28 03:16:09

要计算西格玛,可以这样做:

def calculateEntropy(freqDict,total):
    entropy=0
    nbElements=0
    for element in freqDict:
        p=float(freqDict[element])/total
        entropy-=p*math.log(p,2)
        nbElements+=1
    if nbElements==total:
        return entropy
    else:
        return calculateEntropy(freqDict,nbElements)

要获得令牌频率,可以使用一个简单的dict,其中token作为键,事件作为值。要得到完整的公式,您仍然需要得到100*entropy/math.log(nbElements,2)

相关问题 更多 >

    热门问题