T
- the type of the samplespublic class FrequencyDistribution<T> extends Object implements Serializable
Suppose we want to record the number of occurrences of each word in a sentence, then this class can be used as follows:
FrequencyDistribution<String> fd = new FrequencyDistribution<String>(); for (String word : "foo bar baz foo".split(" ")) { fd.inc(word); } System.out.println(fd.getCount("foo"));
The last call to getCount(T)
will yield 2, because the
word "foo" has appeared twice in the given sequence of words.
This class was inspired by NLTK's FreqDist.
ConditionalFrequencyDistribution
,
Serialized FormConstructor and Description |
---|
FrequencyDistribution()
Creates a new empty
FrequencyDistribution . |
FrequencyDistribution(Iterable<T> iterable)
Creates a new
FrequencyDistribution prefilled with samples from an Iterable . |
Modifier and Type | Method and Description |
---|---|
void |
addSample(T sample,
long number)
Increases the count for a given
sample . |
void |
clear() |
boolean |
contains(T sample)
Indicates whether this distribution contains outcomes for a given
sample . |
long |
getB()
Returns the total number of sample values (or bins) that have counts greater than zero.
|
long |
getCount(T sample)
Returns the count for a given
sample . |
Set<T> |
getKeys()
Returns the
Set of sample values (or bins) for which counts have been recorded. |
long |
getMaxFreq()
Returns the highest frequency that is currently stored.
|
List<T> |
getMostFrequentSamples(int n)
Returns the n most frequent samples in the distribution.
|
long |
getN()
Returns the total number of sample outcomes that have been recorded by this frequency
distribution.
|
T |
getSampleWithMaxFreq()
Returns the sample which has currently the highest frequency.
|
void |
inc(T sample)
Increments the count for a given
sample . |
void |
incAll(Iterable<T> iterable)
Increments the count for each sample in a given
Iterable . |
void |
load(File file) |
void |
save(File file) |
public FrequencyDistribution()
FrequencyDistribution
.public FrequencyDistribution(Iterable<T> iterable)
FrequencyDistribution
prefilled with samples from an Iterable
.
The count for each sample in the iterable is cumulatively increased by 1.iterable
- the Iterable
used to fill the FrequencyDistribution
public boolean contains(T sample)
sample
.sample
- the sample to look uppublic void inc(T sample)
sample
.sample
- the sample to increment the count forpublic void incAll(Iterable<T> iterable)
Iterable
.iterable
- the samples used to increment the countspublic long getN()
public long getB()
public long getCount(T sample)
sample
. If no such samples have been recorded yet,
0
will be returned.sample
- the sample to get the count forpublic Set<T> getKeys()
Set
of sample values (or bins) for which counts have been recorded.public void addSample(T sample, long number)
sample
.sample
- the sample to increase the count fornumber
- the number to increase bypublic long getMaxFreq()
public T getSampleWithMaxFreq()
public void save(File file) throws IOException
IOException
public void load(File file) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
public void clear()
public List<T> getMostFrequentSamples(int n)
n
- the numer of most frequent samples to return.Copyright © 2007–2018 Ubiquitous Knowledge Processing (UKP) Lab, Technische Universität Darmstadt. All rights reserved.