@Override public LocalPage getClosestCategory(LocalPage page, Set<LocalPage> candidates, boolean weightedDistance) throws DaoException { CategoryGraph graph = getGraph(page.getLanguage()); CategoryBfs bfs = new CategoryBfs(graph, page.getLocalId(), page.getLanguage(), Integer.MAX_VALUE, null, this); bfs.setAddPages(false); bfs.setExploreChildren(false); Map<Integer, LocalPage> indexToCandidates = new HashMap<Integer, LocalPage>(); for (LocalPage c : candidates) { indexToCandidates.put(graph.catIdToIndex(c.getLocalId()), c); } List<LocalPage> matches = new ArrayList<LocalPage>(); while (bfs.hasMoreResults() && matches.isEmpty()) { CategoryBfs.BfsVisited visited = bfs.step(); for (int catId : visited.cats.keys()) { if (indexToCandidates.containsKey(catId)) { matches.add(indexToCandidates.get(catId)); } } } if (matches.isEmpty()) { return null; } else { return matches.get(new Random().nextInt(matches.size())); } }
while ((bfs1.hasMoreResults() || bfs2.hasMoreResults()) && (maxDist1 + maxDist2 < shortestDistance)) { while (bfs1.hasMoreResults() && (maxDist1 <= maxDist2 || !bfs2.hasMoreResults())) { CategoryBfs.BfsVisited visited = bfs1.step(); for (int catId : visited.cats.keys()) { while (bfs2.hasMoreResults() && (maxDist2 <= maxDist1 || !bfs1.hasMoreResults())) { CategoryBfs.BfsVisited visited = bfs2.step(); for (int catId : visited.cats.keys()) {
if (!hasMoreResults()) { return visited; do { cs = openCats.poll(); } while (hasMoreResults() && catDistances.contains(cs.getCatIndex()));
@Override public SRResultList mostSimilar(int pageId, int maxResults, TIntSet validIds) throws DaoException { if (!mostSimilarIsTrained()) { return new SRResultList(0); } SRResultList results = getCachedMostSimilar(pageId, maxResults, validIds); if (results != null) { return results; } CategoryBfs bfs = new CategoryBfs(graph,pageId,getLanguage(), maxResults, validIds, catHelper); while (bfs.hasMoreResults()) { bfs.step(); } results = new SRResultList(bfs.getPageDistances().size()); int i = 0; for (int pageId2: bfs.getPageDistances().keys()) { results.set(i++, pageId2, distanceToScore(bfs.getPageDistances().get(pageId2))); } results.sortDescending(); return normalize(results); }