@Override public double[][] cosimilarity(int rowIds[], int columnIds[]) throws DaoException { int [][] rowLinks = new int[rowIds.length][]; int [][] colLinks = new int[columnIds.length][]; for (int i = 0; i < rowIds.length; i++) rowLinks[i] = getLinks(rowIds[i], true); for (int i = 0; i < columnIds.length; i++) colLinks[i] = getLinks(columnIds[i], true); double result[][] = new double[rowIds.length][columnIds.length]; for (int i = 0; i < rowIds.length; i++) { for (int j = 0; j < columnIds.length; j++) { int has1 = hasLink(rowLinks[i], columnIds[j]); int has2 = hasLink(colLinks[j], rowIds[i]); result[i][j] = normalize(0.5 * has1 + 0.5 * has2); } } return result; }
@Override public SRResult similarity(int pageId1, int pageId2, boolean explanations) throws DaoException { int count = 0; int links1[] = getLinks(pageId1, true); int links2[] = getLinks(pageId2, true); count += hasLink(links1, pageId2); count += hasLink(links2, pageId1); return new SRResult(normalize(1.0 * count / 2.0)); }
@Override public SRResultList mostSimilar(int pageId, int maxResults, TIntSet validIds) throws DaoException { TIntIntMap scores = new TIntIntHashMap(); for (int id : getLinks(pageId, true)) { if (validIds == null || validIds.contains(id)) scores.adjustOrPutValue(id, 1, 1); } for (int id : getLinks(pageId, false)) { if (validIds == null || validIds.contains(id)) scores.adjustOrPutValue(id, 1, 1); } Leaderboard leaderboard = new Leaderboard(maxResults); for (int id : scores.keys()) { leaderboard.tallyScore(id, scores.get(id) / 2.0); } return normalize(leaderboard.getTop()); }