gpt4 book ai didi

java - 如何在 Java 中正确使用 Apache 公共(public)数学库中的 ZipfDistribution?

转载 作者:行者123 更新时间:2023-11-30 09:59:49 25 4
gpt4 key购买 nike

我想基于遵循 Zipf 分布的单词(来自字典)创建一个数据源(在 Java 中)。所以我来ZipfDistributionNormalDistribution Apache 公共(public)库的一部分。不幸的是,关于如何使用这些类的信息很少。我试着做了一些测试,但我不确定我是否以正确的方式使用它。我只关注每个构造函数的文档中所写的内容。但结果似乎并不“均匀”。

import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.ZipfDistribution;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class ZipfDistributionDataSource extends RichSourceFunction<String> {
private static final String DISTINCT_WORDS_URL = "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt";

public static void main(String[] args) throws Exception {
ZipfDistributionDataSource zipfDistributionDataSource = new ZipfDistributionDataSource();
StringBuffer stringBuffer = new StringBuffer(zipfDistributionDataSource.readDataFromResource());
String[] words = stringBuffer.toString().split("\n");
System.out.println("size: " + words.length);

System.out.println("Normal Distribution");
NormalDistribution normalDistribution = new NormalDistribution(words.length / 2, 1);
for (int i = 0; i < 10; i++) {
int sample = (int) normalDistribution.sample();
System.out.print("sample[" + sample + "]: ");
System.out.println(words[sample]);
}

System.out.println();
System.out.println("Zipf Distribution");
ZipfDistribution zipfDistribution = new ZipfDistribution(words.length - 1, 1);
for (int i = 0; i < 10; i++) {
int sample = zipfDistribution.sample();
System.out.print("sample[" + sample + "]: ");
System.out.println(words[sample]);
}
}

private String readDataFromResource() throws Exception {
URL url = new URL(DISTINCT_WORDS_URL);
InputStream in = url.openStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
builder.append(line + "\n");
}
bufferedReader.close();

} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
}

输出

size: 370103
Normal Distribution
sample[185049]: metathesize
sample[185052]: metathetically
sample[185051]: metathetical
sample[185050]: metathetic
sample[185049]: metathesize
sample[185050]: metathetic
sample[185052]: metathetically
sample[185050]: metathetic
sample[185052]: metathetically
sample[185050]: metathetic

Zipf Distribution
sample[11891]: anaphasic
sample[314]: abegge
sample[92]: abandoner
sample[3]: aah
sample[36131]: blepharosynechia
sample[218]: abbozzo
sample[8]: aalii
sample[5382]: affing
sample[6394]: agoraphobia
sample[4360]: adossed

最佳答案

从代码的角度来看,您使用得很好 :) 问题在于假设源 Material 显然是按字母顺序排列的,但它是由 Zipf 订购的。使用 ZipfDistribution 的全部意义在于 words[0] 必须是最常见的词(提示:它是 'the')并且大约是 words[1] 的频率的两倍)等等。

https://en.wikipedia.org/wiki/Word_lists_by_frequency https://en.wikipedia.org/wiki/Most_common_words_in_English

关于java - 如何在 Java 中正确使用 Apache 公共(public)数学库中的 ZipfDistribution?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58590352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com