GraphPartition { public static <T extends IMentionable<? extends Serializable>, X extends Serializable, Y extends Serializable> LabeledMTJMatrix<X> getMentionMatrix(List<T> data, Function<T, X> funcX, Function<T, Y> funcY) { List<Y> dimYOrd = data.stream() .map(funcY) .distinct() .filter(Objects::nonNull) .collect(Collectors.toList()); List<X> dimXOrd = data.stream() .map(funcX) .distinct() .filter(Objects::nonNull) .collect(Collectors.toList()); Map<X, Integer> dimXToIdx = IntStream.range(0, dimXOrd.size()) .boxed() .collect(Collectors.toMap(i -> dimXOrd.get(i), i -> i)); Map<Y, Integer> dimYToIdx = IntStream.range(0, dimYOrd.size()) .boxed() .collect(Collectors.toMap(i -> dimYOrd.get(i), i -> i)); Matrix M = new LinkedSparseMatrix(dimYOrd.size(), dimXOrd.size()); for (T mention : data) { Y yValue = funcY.apply(mention); X xValue = funcX.apply(mention); if (yValue == null || xValue == null) { continue; } int rowIdx = dimYToIdx.get(yValue); int columnIdx = dimXToIdx.get(xValue); double existingValue = M.get(rowIdx, columnIdx); M.set(rowIdx, columnIdx, existingValue + mention.getOccurrences()); } Matrix M_c = M.copy(); Matrix A = M.transAmult(M_c, new DenseMatrix(M.numColumns(), M.numColumns())); @SuppressWarnings("unchecked") X[] labels = (X[]) new Serializable[dimXToIdx.size()]; for (Map.Entry<X, Integer> entry : dimXToIdx.entrySet()) { labels[entry.getValue()] = entry.getKey(); } return LabeledMTJMatrix.of(A, Lists.newArrayList(labels)); }  static LabeledMTJMatrix<X> getMentionMatrix(List<T> data,
                                            Function<T, X> funcX,
                                            Function<T, Y> funcY); static LabeledDenseMatrix<L> getSimilarityMatrix(
            LabeledMTJMatrix<L> labeledMatrix); static Matrix getPermutedMatrix(Matrix A, int[] permutations); static List<L> getPermutedLabels(List<L> originalLabels,
                                                                     int[] permutations);  }
@Test public void testGetMentionMatrix() { List<EmojiEntity> mentions = Lists.newArrayListWithCapacity(16); mentions.add(new EmojiEntity("u1", "r1", DateTime.now(), "a", 1, false)); mentions.add(new EmojiEntity("u1", "r2", DateTime.now(), "a", 1, false)); mentions.add(new EmojiEntity("u1", "r3", DateTime.now(), "a", 1, false)); mentions.add(new EmojiEntity("u1", "r1", DateTime.now(), "b", 1, false)); mentions.add(new EmojiEntity("u1", "r2", DateTime.now(), "b", 1, false)); mentions.add(new EmojiEntity("u1", "r2", DateTime.now(), "c", 1, false)); mentions.add(new EmojiEntity("u1", "r3", DateTime.now(), "c", 1, false)); LabeledMTJMatrix<String> result = GraphPartition.getMentionMatrix(mentions, mention -> mention.getRoomName(), mention -> mention.getValue()); assertEquals(3, result.getMatrix().numRows()); assertEquals(3, result.getMatrix().numColumns()); assertEquals(3, result.getLabels().stream().distinct().count()); }
@Test public void testGetMentionMatrix_withNulls() { List<EmojiEntity> mentions = Lists.newArrayListWithCapacity(16); mentions.add(new EmojiEntity(null, "r2", DateTime.now().plus(1), "a", 1, false)); mentions.add(new EmojiEntity("u1", null, DateTime.now().plus(2), "a", 1, false)); LabeledMTJMatrix<String> result = GraphPartition.getMentionMatrix(mentions, mention -> mention.getRoomName(), mention -> mention.getUsername()); assertEquals(1, result.getMatrix().numRows()); assertEquals(1, result.getMatrix().numColumns()); assertEquals(1, result.getLabels().stream().distinct().count()); }