Skip to content

Commit

Permalink
Simplified merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixRilling committed Dec 31, 2023
1 parent 94ee939 commit 80942df
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/main/java/dev/rilling/musicbrainzenricher/util/MergeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,35 @@ public static <T> Set<T> getMostCommon(Collection<Set<T>> sets, double minUsageP
return Set.copyOf(sets.iterator().next());
}

List<T> all = mergeIntoList(sets);
return getMostCommon(all, minUsagePercentage);
List<T> allValues = sets.stream().flatMap(Collection::stream).toList();
return getMostCommon(allValues, minUsagePercentage);
}


private static <T> Set<T> getMostCommon(List<T> all, double minUsagePercentage) {
if (all.isEmpty()) {
private static <T> Set<T> getMostCommon(List<T> allValues, double minUsageRatio) {
if (allValues.isEmpty()) {
return Set.of();
}

// Roughly based on Musicbrainz Picard's picard.track.Track._convert_folksonomy_tags_to_genre
Map<T, Integer> counted = count(all);
Map<T, Integer> counted = count(allValues);
// Max count must be present as we know the map is not empty.
int maxCount = counted.values().stream().max(Integer::compareTo).orElseThrow();

return counted.entrySet().stream().filter(entry -> {
double usagePercentage = ((double) entry.getValue()) / maxCount;
return usagePercentage >= minUsagePercentage;
return usagePercentage >= minUsageRatio;
}).map(Map.Entry::getKey).collect(Collectors.toUnmodifiableSet());
}


private static <T> Map<T, Integer> count(Collection<T> all) {
Map<T, Integer> counted = new HashMap<>(all.size());
for (T t : all) {
counted.compute(t, (ignored, count) -> count == null ? 1 : count + 1);
for (T value : all) {
counted.compute(value, (ignored, count) -> count == null ? 1 : count + 1);
}
return counted;
}


private static <T> List<T> mergeIntoList(Collection<? extends Collection<T>> sets) {
List<T> all = new ArrayList<>(sets.size() * 5);
sets.forEach(all::addAll);
return all;
}

}

0 comments on commit 80942df

Please sign in to comment.