Package org.nlpub.watset.graph
Class ChineseWhispers<V,E>
- java.lang.Object
-
- org.nlpub.watset.graph.ChineseWhispers<V,E>
-
- Type Parameters:
V- the type of nodes in the graphE- the type of edges in the graph
- All Implemented Interfaces:
Clustering<V>
public class ChineseWhispers<V,E> extends Object implements Clustering<V>
Implementation of the Chinese Whispers algorithm.- See Also:
- Biemann (TextGraphs-1)
-
-
Field Summary
Fields Modifier and Type Field Description protected org.jgrapht.Graph<V,E>graphThe graph.protected intiterationsThe number of iterations.static intITERATIONSThe default number of Chinese Whispers iterations.protected Map<V,Integer>labelsThe mapping of nodes to labels.protected RandomrandomThe random number generator.protected intstepsThe number of actual algorithm iterations.protected NodeWeighting<V,E>weightingThe node weighting approach.
-
Constructor Summary
Constructors Constructor Description ChineseWhispers(org.jgrapht.Graph<V,E> graph, NodeWeighting<V,E> weighting)Create an instance of the Chinese Whispers algorithm.ChineseWhispers(org.jgrapht.Graph<V,E> graph, NodeWeighting<V,E> weighting, int iterations, Random random)Create an instance of the Chinese Whispers algorithm.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidfit()Run the algorithm to induce the parameters of the clusters.Collection<Collection<V>>getClusters()Return a collection of clusters, each cluster is a collection of objects.intgetIterations()Return the number of iterations specified in the constructorintgetSteps()Return the number of iterations actually performed duringfit().static <V,E>
Function<org.jgrapht.Graph<V,E>,Clustering<V>>provider(NodeWeighting<V,E> weighting)A factory function that sets up the algorithm for the given graph.static <V,E>
Function<org.jgrapht.Graph<V,E>,Clustering<V>>provider(NodeWeighting<V,E> weighting, int iterations, Random random)A factory function that sets up the algorithm for the given graph.protected Map<Integer,Double>score(org.jgrapht.Graph<V,E> graph, Map<V,Integer> labels, NodeWeighting<V,E> weighting, V node)Score the label weights in the given neighborhood graph, which is a subgraph ofgraph.protected intstep(List<V> nodes)Perform one iteration of the algorithm.
-
-
-
Field Detail
-
ITERATIONS
public static final int ITERATIONS
The default number of Chinese Whispers iterations.- See Also:
- Constant Field Values
-
weighting
protected final NodeWeighting<V,E> weighting
The node weighting approach.
-
iterations
protected final int iterations
The number of iterations.
-
random
protected final Random random
The random number generator.
-
steps
protected int steps
The number of actual algorithm iterations.
-
-
Constructor Detail
-
ChineseWhispers
public ChineseWhispers(org.jgrapht.Graph<V,E> graph, NodeWeighting<V,E> weighting, int iterations, Random random)
Create an instance of the Chinese Whispers algorithm.- Parameters:
graph- the graphweighting- the node weighting approachiterations- the number of iterationsrandom- the random number generator
-
ChineseWhispers
public ChineseWhispers(org.jgrapht.Graph<V,E> graph, NodeWeighting<V,E> weighting)
Create an instance of the Chinese Whispers algorithm.- Parameters:
graph- the graphweighting- the node weighting approach
-
-
Method Detail
-
provider
public static <V,E> Function<org.jgrapht.Graph<V,E>,Clustering<V>> provider(NodeWeighting<V,E> weighting)
A factory function that sets up the algorithm for the given graph.- Type Parameters:
V- the type of nodes in the graphE- the type of edges in the graph- Parameters:
weighting- the node weighting approach- Returns:
- a factory function that sets up the algorithm for the given graph
-
provider
public static <V,E> Function<org.jgrapht.Graph<V,E>,Clustering<V>> provider(NodeWeighting<V,E> weighting, int iterations, Random random)
A factory function that sets up the algorithm for the given graph.- Type Parameters:
V- the type of nodes in the graphE- the type of edges in the graph- Parameters:
weighting- the node weighting approachiterations- the number of iterationsrandom- the random number generator- Returns:
- a factory function that sets up the algorithm for the given graph
-
fit
public void fit()
Description copied from interface:ClusteringRun the algorithm to induce the parameters of the clusters.- Specified by:
fitin interfaceClustering<V>
-
step
protected int step(List<V> nodes)
Perform one iteration of the algorithm.- Parameters:
nodes- the list of nodes- Returns:
- whether any label changed or not
-
getClusters
public Collection<Collection<V>> getClusters()
Description copied from interface:ClusteringReturn a collection of clusters, each cluster is a collection of objects. Usually this method is called after theClustering.fit()method.- Specified by:
getClustersin interfaceClustering<V>- Returns:
- clusters
-
score
protected Map<Integer,Double> score(org.jgrapht.Graph<V,E> graph, Map<V,Integer> labels, NodeWeighting<V,E> weighting, V node)
Score the label weights in the given neighborhood graph, which is a subgraph ofgraph. This method sums the node weights corresponding to each label.- Parameters:
graph- the neighborhood graphlabels- the map of graph nodes to their labelsweighting- the node weighting approachnode- the target node- Returns:
- a mapping of labels to sums of their weights
-
getIterations
public int getIterations()
Return the number of iterations specified in the constructor- Returns:
- the number of iterations
- See Also:
ChineseWhispers(Graph, NodeWeighting, int, Random)
-
getSteps
public int getSteps()
Return the number of iterations actually performed duringfit(). Should be no larger than the value ofgetIterations().- Returns:
- the number of iterations
-
-