/** * {@inheritDoc} */ public double get(int index) { return vector.get(index); }
@Override public double activity(int value) { return Av.get(value - os); }
/** * {@inheritDoc} */ public double get(int index) { return vector.get(index); }
public double getPageDistance(int pageId) { return pageDistances.get(pageId); } public boolean hasCategoryDistanceForIndex(int categoryId) {
public double getCategoryDistanceForIndex(int catIndex) { return catDistances.get(catIndex); }
@Override public double get(int key) { if (key < 0 || key >= length) throw new IndexOutOfBoundsException(Integer.toString(key)); return data.get(key); }
public double getCategoryDistance(int categoryId) { return catDistances.get(graph.catIdToIndex(categoryId)); } public double getCategoryDistanceForIndex(int catIndex) {
/** * {@inheritDoc} */ public double[] toArray() { double[] array = new double[length()]; for (int i : vector.keys()) array[i] = vector.get(i); return array; }
@Override public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) { double matchFactor = minFactorMap.get(edge.getEdge()); double weight = weighting.calcWeight(edge, reverse, prevOrNextEdgeId); if (matchFactor < 0) { return maxWeight.value * weight; } return matchFactor * weight; }
/** * {@inheritDoc} */ public void set(int index, double value) { double old = vector.get(index); if (value == 0) vector.remove(index); else vector.put(index, value); magnitude = -1; }
/** * {@inheritDoc} */ public double[] toArray() { double[] array = new double[length()]; for (int i : vector.keys()) array[i] = vector.get(i); return array; }
/** * {@inheritDoc} */ public double add(int index, double delta) { double val = vector.get(index) + delta; if (val == 0) vector.remove(index); else set(index, val); nonZeroIndices = null; magnitude = -1; return val; }
@Override public SRResultList predictMostSimilar(List<SRResultList> scores, int maxResults, TIntSet validIds) { int numMetrics = scores.size(); TIntDoubleHashMap scoreMap = new TIntDoubleHashMap(); for (SRResultList resultList : scores){ for (SRResult result : resultList){ double value = result.getScore()/numMetrics; scoreMap.adjustOrPutValue(result.getId(),value,value); } } List<SRResult> resultList = new ArrayList<SRResult>(); for (int id : scoreMap.keys()){ resultList.add(new SRResult(id,scoreMap.get(id))); } Collections.sort(resultList); Collections.reverse(resultList); SRResultList result = new SRResultList(maxResults); for (int i=0; i<maxResults&&i<resultList.size();i++){ result.set(i,resultList.get(i)); } return result; }
private SRResultList indexedMostSimilar(PhraseVector v1, int maxResults, TIntSet candidateIds) { final TIntDoubleHashMap dots = new TIntDoubleHashMap(maxResults * 5); for (int i = 0; i < v1.ids.length; i++) { long featureId = v1.ids[i]; final float featureVal = v1.vals[i]; TIntFloatMap index = invertedIndex.get(featureId); if (index == null) continue; synchronized (index) { index.forEachEntry(new TIntFloatProcedure() { @Override public boolean execute(int id, float val) { dots.adjustOrPutValue(id, val * featureVal, val * featureVal); return true; } }); } } final Leaderboard leaderboard = new Leaderboard(maxResults); double l1 = v1.norm2(); int keys[] = dots.keys(); for (int i = 0; i < keys.length; i++) { int id = keys[i]; double l2 = byId.get(id).getVector().norm2(); double dot = dots.get(id); double sim = dot / (l1 * l2); leaderboard.tallyScore(id, sim); } return leaderboard.getTop(); }
@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); }
/** * Fills the minFactorMap with weights for the virtual edges. */ private void fillVirtualEdges(TIntDoubleHashMap minFactorMap, TIntObjectHashMap<EdgeIteratorState> virtualEdgesMap, EdgeExplorer explorer, QueryResult qr) { EdgeIterator iter = explorer.setBaseNode(qr.getClosestNode()); while (iter.next()) { if (isVirtualNode(qr.getClosestNode())) { if (traverseToClosestRealAdj(explorer, iter) == qr.getClosestEdge().getAdjNode()) { virtualEdgesMap.put(iter.getEdge(), qr.getClosestEdge()); } else { virtualEdgesMap.put(iter.getEdge(), qr.getClosestEdge().detach(true)); } } double dist = minFactorMap.get(iter.getEdge()); if (dist < 0 || dist > qr.getQueryDistance()) { minFactorMap.put(iter.getEdge(), qr.getQueryDistance() + 0.5); } } }