@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 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()); }
@Override public SRMetric get(String name, Config config, Map<String, String> runtimeParams) throws ConfigurationException { if (!config.getString("type").equals("directlink")) { return null; } if (runtimeParams == null || !runtimeParams.containsKey("language")) { throw new IllegalArgumentException("Monolingual SR Metric requires 'language' runtime parameter"); } Language language = Language.getByLangCode(runtimeParams.get("language")); LocalLinkDao linkDao = getConfigurator().get(LocalLinkDao.class); Disambiguator dab = getConfigurator().get(Disambiguator.class, config.getString("disambiguator"), "language", language.getLangCode()); DirectLinkMetric mw = new DirectLinkMetric( name, language, getConfigurator().get(LocalPageDao.class), linkDao, dab ); configureBase(getConfigurator(), mw, config); return mw; } }
private int[] getLinks(int pageId1, boolean outLinks) throws DaoException { TIntList result = new TIntArrayList(); for (LocalLink ll : linkDao.getLinks(getLanguage(), pageId1, outLinks)) { result.add(ll.getLocalId()); } result.sort(); return result.toArray(); }
@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)); }