-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageRankCalculator.java
29 lines (25 loc) · 1.25 KB
/
PageRankCalculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.github.ivangomes.pagerank;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
@Accessors(fluent = true, chain = true)
@Getter
@Setter
public class PageRankCalculator implements Function<PageRankIndex, Map<String, Double>> {
private double dampingFactor = 0.85;
@Override
public Map<String, Double> apply(PageRankIndex pageRankIndex) {
Map<String, Double> deltas = new HashMap<>(pageRankIndex.size());
pageRankIndex.getIncomingLinksMap().forEach((targetId, sourceIds) -> {
double initialPageRank = pageRankIndex.getPageRanks().computeIfAbsent(targetId, id -> 1d / pageRankIndex.getOutgoingLinksMap().size());
double finalPageRank = (1 - dampingFactor) / pageRankIndex.size() + dampingFactor
* sourceIds.stream().mapToDouble(sourceId -> pageRankIndex.getPageRanks().computeIfAbsent(sourceId, id -> 1d / pageRankIndex.size()) / (double) pageRankIndex.getOutgoingLinksMap().get(sourceId).size()).sum();
pageRankIndex.getPageRanks().put(targetId, finalPageRank);
deltas.put(targetId, finalPageRank - initialPageRank);
});
return deltas;
}
}