Class Counter<T>
java.lang.Object
de.uni_mannheim.informatik.dws.melt.matching_jena_matchers.util.Counter<T>
- Type Parameters:
T
- the datatype of the objects to count.
A Counter is for counting arbitrary objects.
The number per object is integer, thus the maximum frequency of an entity should not be higher than 2,147,483,647.
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprivate Map<T,
Counter<T>.MutableInt> private static final com.fasterxml.jackson.databind.ObjectMapper
private static final org.slf4j.Logger
private Comparator<Map.Entry<T,
? extends Comparable>> private long
-
Constructor Summary
ConstructorDescriptionCounter()
Create a new counter object with a default initial capacity.Create a new counter object from the given iterable.Counter
(Iterable<T> iterable, Comparator<T> elementComparator) Create a new counter object from the given iterable.Counter
(Comparator<T> elementComparator) Create a new counter object with a default initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add one element to the countervoid
Add this element to the counter multiple times.void
Adds another counter to this counter.void
Add a collection of elements to the counter.void
Add a collection of elements to the counter.betweenFrequency
(double min, double max) Return a list of elements with their frequency.betweenFrequencyRelativeToTotal
(double min, double max, long total) Return a list of elements with their frequency relative to the given total number.betweenFrequencyRelativeToTotalReturningElements
(double min, double max, long total) Return a set of elements where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max).betweenFrequencyReturningElements
(double min, double max) Return a set of elements where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max).boolean
int
Returns the amount of distinct elements.long
getCount()
Get the number of how many elements which were added to this counter (this includes duplicates).int
Get the count for a specific element.Returns the set of distinct elements.Return a sorted map of the most common elements and their counts.getSortedMap
(int n) Return a sorted map of the most common elements (limit by n) and their counts.int
hashCode()
boolean
isEmpty()
loadFromJsonFile
(File jsonFile) Load a counter instance from a json file.loadFromJsonParser
(com.fasterxml.jackson.core.JsonParser parser) loadFromJsonString
(String jsonRepresentation) Given a json representation of a counter, create a new counter.Return a list of the most common elements and their counts.mostCommon
(int n) Return a list of the n most common elements and their counts.mostCommonByPercentage
(double percentage) Return a list of the elements with a higher or equal frequency/percentage than the given one together with their frequency.Return the most common element.Return a list ordered by their number of occurences.mostCommonElements
(int n) Return a list of the n most common elements.mostCommonElementsByPercentage
(double percentage) Return a list of the elements with a higher or equal frequency/percentage than the given one.mostCommonElementsByPercentageOrTopN
(double percentage, int topN) Return a list of elements which have a higher (or equal) frequency than the specified one but not more than the specified amount(parameter topN).Return a list of the most common elements with the highest count.boolean
Removes one count of the given element from this counter (the element is still in the counter if it was added multiple times).boolean
Removes the specified number of occurances of the given element from this counter.boolean
removeAll
(Collection<T> elements) Removes the given element from this counter (even if it appears multiple times), if it is present (optional operation).boolean
Removes the given element from this counter (even if it appears multiple times), if it is present (optional operation).boolean
It will substract the other counter from this counter.toJson()
Returns a json representation of this counter.void
Write the counter to a file (content is json).toString()
ToString method which returns the counter well formatted in multiple lines to have a better overview.toStringMultiline
(int n) ToString method which returns return values from functions likemostCommon(int)
well formatted in multiple lines to have a better overview.toStringMultiline
(Collection<Map.Entry<T, Double>> entries) ToString method which returns the given entries well formatted in multiple lines.
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
counts
-
overallCount
private long overallCount -
mapComparator
-
JSON_MAPPER
private static final com.fasterxml.jackson.databind.ObjectMapper JSON_MAPPER
-
-
Constructor Details
-
Counter
public Counter()Create a new counter object with a default initial capacity. -
Counter
Create a new counter object with a default initial capacity.- Parameters:
elementComparator
- the comparator to use if the count is the same
-
Counter
Create a new counter object from the given iterable.- Parameters:
iterable
- any iterable of type T
-
Counter
Create a new counter object from the given iterable.- Parameters:
iterable
- any iterable of type TelementComparator
- the comparator to use if the count is the same
-
-
Method Details
-
addAll
Add a collection of elements to the counter.- Parameters:
iterable
- the iterable to be added.
-
addAll
Add a collection of elements to the counter.- Parameters:
iterator
- the iterator to be added.
-
addAll
Adds another counter to this counter. Only this counter is modified.- Parameters:
counter
- the other counter to add to this object.
-
add
Add one element to the counter- Parameters:
t
- the element to add
-
add
Add this element to the counter multiple times.- Parameters:
t
- the element to addhowOften
- how often this element should be added.
-
remove
Removes one count of the given element from this counter (the element is still in the counter if it was added multiple times). It will remove the element only if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call). In case you want to remove all counts of an element, then useremoveAll(java.lang.Object)
.- Parameters:
t
- the element to remove- Returns:
- true if an element was removed
-
remove
Removes the specified number of occurances of the given element from this counter. It will remove the element only if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call). If the element should be removed more times, than it is added, the corresponding element is removed from this counter. In case you want to remove all counts of an element, then useremoveAll(java.lang.Object)
.- Parameters:
t
- the element to removehowOften
- how often this element should be removed.- Returns:
- true if an element was removed
-
removeAll
Removes the given element from this counter (even if it appears multiple times), if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call).- Parameters:
t
- the element to remove- Returns:
- true if an element was removed
-
removeAll
Removes the given element from this counter (even if it appears multiple times), if it is present (optional operation). Returns true if this counter contained the specified element t (or equivalently, if this collection changed as a result of the call).- Parameters:
elements
- the element to remove- Returns:
- true if the counter was changed/modified
-
substract
It will substract the other counter from this counter. This counter will be modified and the counts are reduced.- Parameters:
other
- the other counter with values to be removed from this counter.- Returns:
- true if this counter was changed/modified
-
getCount
Get the count for a specific element. If the element does not appear in this counter, 0 is returned.- Parameters:
t
- the element for which the count should be returned- Returns:
- how often this element occured.
-
getCount
public long getCount()Get the number of how many elements which were added to this counter (this includes duplicates).- Returns:
- how many elements are added to this list
-
getDistinctElements
Returns the set of distinct elements.- Returns:
- the set of distinct elements
-
getAmountOfDistinctElements
public int getAmountOfDistinctElements()Returns the amount of distinct elements.- Returns:
- amount of distinct elements
-
isEmpty
public boolean isEmpty() -
mostCommon
Return a list of the n most common elements and their counts. Ordered from the most common to the least.- Parameters:
n
- number of common elements to return- Returns:
- list of entries
-
mostCommon
Return a list of the most common elements and their counts. Ordered from the most common to the least.- Returns:
- list of entries
-
getSortedMap
Return a sorted map of the most common elements and their counts. Ordered from the most common to the least.- Returns:
- sorted map
-
getSortedMap
Return a sorted map of the most common elements (limit by n) and their counts. Ordered from the most common to the least.- Parameters:
n
- the number of elements to put in the sorted map- Returns:
- sorted map
-
mostCommonWithHighestCount
Return a list of the most common elements with the highest count. Usually this is only one element but if there are multiple ones with the same count, then this function will return them all. E.g. (a, a, a, b, b, b, c) results in a list of ((a,3), (b, 3)).- Returns:
- list of elements with their counts
-
mostCommonByPercentage
Return a list of the elements with a higher or equal frequency/percentage than the given one together with their frequency.- Parameters:
percentage
- between zero and one. 0.95 means 95 percent.- Returns:
- list of entries with frequency
-
mostCommonElementsByPercentage
Return a list of the elements with a higher or equal frequency/percentage than the given one.- Parameters:
percentage
- between zero and one. 0.95 menas 95 percent.- Returns:
- list of entries
-
mostCommonElementsByPercentageOrTopN
Return a list of elements which have a higher (or equal) frequency than the specified one but not more than the specified amount(parameter topN). If one of those two parameters is zero, then it will not be taken into account. Especially if both are zero, then all elements will be returned.- Parameters:
percentage
- between zero and one. 0.95 means 95 percent.topN
- the maximum number of common elements to return- Returns:
- list of elements (element with highest count first)
-
mostCommonElements
Return a list of the n most common elements. Ordered from the most common to the least.- Parameters:
n
- number of common elements to return- Returns:
- list of elements
-
mostCommonElements
Return a list ordered by their number of occurences. Ordered from the most common to the least.- Returns:
- list of elements
-
mostCommonElement
Return the most common element.- Returns:
- most common element or null if counter is empty.
-
betweenFrequency
Return a list of elements with their frequency. Only elements are returned where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). Ordered from the most common to the least.- Parameters:
min
- the min frequency (inclusive)max
- the max frequency (inclusive)- Returns:
- list of elements with their frequency
-
betweenFrequencyReturningElements
Return a set of elements where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). Ordered from the most common to the least.- Parameters:
min
- the min frequency (inclusive)max
- the max frequency (inclusive)- Returns:
- set of elements
-
betweenFrequencyRelativeToTotal
public List<Map.Entry<T,Double>> betweenFrequencyRelativeToTotal(double min, double max, long total) Return a list of elements with their frequency relative to the given total number. Only elements are returned where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). Ordered from the most common to the least.- Parameters:
min
- the min frequency (inclusive)max
- the max frequency (inclusive)total
- the total number to compare with- Returns:
- list of elements with their frequency
-
betweenFrequencyRelativeToTotalReturningElements
Return a set of elements where the frequency is between the given min and max arguments (greater or equal to min and less or equal to max). It calculates the frequency compared to the parameter total. Ordered from the most common to the least.- Parameters:
min
- the min frequency (inclusive)max
- the max frequency (inclusive)total
- the total number to compare with- Returns:
- set of elements
-
toString
-
toStringMultiline
ToString method which returns the given entries well formatted in multiple lines.- Parameters:
entries
- the values which should be nicely formatted- Returns:
- a string which contains the entries information in multiple lines.
-
toStringMultiline
ToString method which returns the counter well formatted in multiple lines to have a better overview.- Returns:
- a string which contains the counter information in multiple lines.
-
toStringMultiline
ToString method which returns return values from functions likemostCommon(int)
well formatted in multiple lines to have a better overview.- Parameters:
n
- the number of elements to print as multiline- Returns:
- a string which contains the counter information in multiple lines.
-
toJson
Returns a json representation of this counter.- Returns:
- a json representation
-
toJson
Write the counter to a file (content is json).- Parameters:
file
- the file to use for writing the JSON content.
-
loadFromJsonString
Given a json representation of a counter, create a new counter. The comparator is not loaded/serialized.- Parameters:
jsonRepresentation
- the json representation of a counter.- Returns:
- the new counter
-
loadFromJsonFile
Load a counter instance from a json file. The comparator is not loaded/serialized.- Parameters:
jsonFile
- the json file.- Returns:
- the new instance
-
loadFromJsonParser
private static Counter<String> loadFromJsonParser(com.fasterxml.jackson.core.JsonParser parser) throws IOException - Throws:
IOException
-
hashCode
public int hashCode() -
equals
-