Cluster.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package de.jplag.clustering;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.function.BiFunction;
  6. import java.util.stream.Collectors;
  7. /**
  8. * Cluster part of a {@link ClusteringResult}.
  9. * @param <T> type of the clusters members
  10. */
  11. public class Cluster<T> {
  12. private float communityStrength;
  13. private Collection<T> members;
  14. private ClusteringResult<T> clusteringResult = null;
  15. public Cluster(Collection<T> members, float communityStrength) {
  16. this.members = new ArrayList<>(members);
  17. this.communityStrength = communityStrength;
  18. }
  19. public Collection<T> getMembers() {
  20. return members;
  21. }
  22. /**
  23. * See {@link ClusteringResult#getCommunityStrength}
  24. * @return community strength of the cluster
  25. */
  26. public float getCommunityStrength() {
  27. return communityStrength;
  28. }
  29. /**
  30. * Sets this clusters clustering result. Should only be called by classes extending {@link ClusteringResult} on their
  31. * own clusters.
  32. * @param clusteringResult the clustering result
  33. */
  34. public void setClusteringResult(ClusteringResult<T> clusteringResult) {
  35. this.clusteringResult = clusteringResult;
  36. }
  37. /**
  38. * @return How much each member of this cluster contributes to the {@link ClusteringResult#getCommunityStrength}.
  39. */
  40. public float getCommunityStrengthPerConnection() {
  41. int size = getMembers().size();
  42. if (size < 2)
  43. return 0;
  44. return getCommunityStrength() / connections();
  45. }
  46. /**
  47. * Computes a normalized community strength per connection. Can be used as measure for strength of evidence in
  48. * comparison to other clusters in the same clustering. Guaranteed to be smaller than 1. Negative values indicate
  49. * non-clusters. This method may only be called on clusters that are part of a ClusteringResult.
  50. * @return normalized community strength per connection
  51. */
  52. public float getNormalizedCommunityStrengthPerConnection() {
  53. List<Cluster<T>> goodClusters = clusteringResult.getClusters().stream().filter(cluster -> cluster.getCommunityStrength() > 0)
  54. .collect(Collectors.toList());
  55. float posCommunityStrengthSum = (float) goodClusters.stream().mapToDouble(Cluster::getCommunityStrengthPerConnection).sum();
  56. int size = clusteringResult.getClusters().size();
  57. if (size < 2)
  58. return getCommunityStrengthPerConnection();
  59. return getCommunityStrengthPerConnection() / posCommunityStrengthSum;
  60. }
  61. /**
  62. * How much this cluster is worth during optimization.
  63. */
  64. public double getWorth(BiFunction<T, T, Float> similarity) {
  65. double communityStrength = getCommunityStrength();
  66. if (getMembers().size() > 1) {
  67. communityStrength /= connections();
  68. }
  69. double averageSimilarity = averageSimilarity(similarity);
  70. return communityStrength * averageSimilarity;
  71. }
  72. /**
  73. * Computes the average similarity inside the cluster.
  74. * @param similarity function that supplies the similarity of two cluster members.
  75. * @return average similarity
  76. */
  77. public float averageSimilarity(BiFunction<T, T, Float> similarity) {
  78. List<T> members = new ArrayList<>(getMembers());
  79. if (members.size() < 2) {
  80. return 1;
  81. }
  82. float similaritySum = 0;
  83. for (int i = 0; i < members.size(); i++) {
  84. for (int j = i + 1; j < members.size(); j++) {
  85. similaritySum += similarity.apply(members.get(i), members.get(j));
  86. }
  87. }
  88. return similaritySum / connections();
  89. }
  90. private int connections() {
  91. int size = getMembers().size();
  92. return ((size - 1) * size) / 2;
  93. }
  94. /**
  95. * Whether this cluster is very uninformative or wrong and should be pruned as last step of the clustering process.
  96. * @return is bad
  97. */
  98. public boolean isBadCluster() {
  99. return getMembers().size() < 2 || getCommunityStrength() < 0;
  100. }
  101. }