如何计算文本中词频的最佳zipf分布

How to calculate the optimal zipf distribution of word frequencies in a text

对于家庭作业,我必须绘制文本的词频并将其与最佳 zipf 分布进行比较。

根据对数日志图中的排名绘制文本的统计词频似乎工作正常。

但我对计算最佳 zipf 分布感到困惑。结果应如下所示:

我不明白计算直线 zipf 的方程是什么样的。

zipf 法则的德语维基百科页面上,我发现了一个似乎有效的方程式

但没有引用任何来源,所以我不明白 1.78 的常数从何而来。

#tokenizes the file 
tokens = word_tokenize(raw)
tokensNLTK = Text(tokens)

#calculates the FreqDist of all words - all words in lower case
freq_list = FreqDist([w.lower() for w in tokensNLTK]).most_common()

#Data for X- and Y-Axis plot
values=[]
for item in (freq_list):
    value = (list(item)[1]) / len([w.lower() for w in tokensNLTK])
    values.append(value)

#graph of counted frequencies gets plotted
plt.yscale('log')
plt.xscale('log')
plt.plot(np.array(list(range(1, (len(values)+1)))), np.array(values))

#graph of optimal zipf distribution is plotted
optimal_zipf = 1/(np.array(list(range(1, (len(values)+1))))* np.log(1.78*len(values)))###1.78
plt.plot(np.array(list(range(1, (len(values)+1)))), optimal_zipf)
plt.show()

我使用此脚本的结果如下所示:

但我不确定最优 zipf 分布是否计算正确。如果是这样,最优 zipf 分布不应该在某一点穿过 X-axis 吗?

编辑:如果有帮助,我的文本有 2440400 个标记和 27491 种类型

看看这个 research paper by Andrew William Chisholm. 特别是第 22 页。

H(N) ≈ ln(N) + γ

Where γ is the Euler-Mascheroni constant with approximate value 0.57721. Noting that exp(γ) ≈ 1.78, equation <...> can be re-written to become for large N (N must be greater than 1,000 for this to be accurate to one part in a thousand).

pr ≈ 1 / [r*ln(1.78*N)]