2

I want to create a source of data (in Java) based on words (from a dictionary) that follow a Zipf distribution. So I come to ZipfDistribution and NormalDistribution of the Apache commons library. Unfortunately, information about how to use these classes are rarely. I tried to do some tests but I am not sure if I am using it in the right manner. I am following only what is written in the documentation of each constructor. But the results don't seem to be "well-distributed".

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();
    }
}

output

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

1 Answer 1

1

You are using it just fine from a code perspective :) The problem is in assuming the source material is ordered by Zipf when it is clearly alphabetical. The whole point of using ZipfDistribution is that words[0] must be the most common word (hint: it's 'the') and roughly twice the freq of words[1]) etc.

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

3
  • I am afraid I didn't get your point. I see that the problem is that my list of words is alphabetically sorted. Then, I didn't understand afterwards. I mean, if I have a number which represents my zipf distribution I can just select the words that correspond to that number, can't I?
    – Felipe
    Oct 28, 2019 at 13:39
  • I am not sure if I am right. But if I use the Moby Dick book it already follows very close the Zipf distribution.
    – Felipe
    Oct 28, 2019 at 13:48
  • 1
    Note your example ZipfDistribution sample values all lie close to 0 and NormalDistribution sample values all lie close to N/2. That is because those are the highest probability areas of their distribution; A random sample is most likely to be from there. The classes have no knowledge of the data except its size N and the prerequisite that it is sorted in the expected distribution. Their purpose is merely to encapsulate the probability computations (say "what is the probability a random word is in the top 10", "How probable is the word 'teapot'" ).
    – drekbour
    Oct 28, 2019 at 13:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.