如何在Java中正确使用Apache Commons数学库中的ZipfDistribution?

java   apache-commons-math   zipf  

温馨提示:将鼠标放在语句上可以显示对应的英文。   或者   切换至中英文显示

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

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

java apache-commons-math zipf

1 个回复

按投票数排序按时间排序

猜你喜欢

转载自blog.csdn.net/fly_time2012/article/details/108757939
今日推荐