Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.

Commit 486a016

Browse files
committed
Use Java 11 instead of Groovy for examples
1 parent 532fe74 commit 486a016

File tree

8 files changed

+202
-164
lines changed

8 files changed

+202
-164
lines changed

examples/FuzzyClustering.groovy

Lines changed: 0 additions & 44 deletions
This file was deleted.

examples/FuzzyClustering.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// java -cp watset.jar FuzzyClustering.java
2+
3+
/*
4+
* Copyright 2019 Dmitry Ustalov
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
20+
import org.jgrapht.Graph;
21+
import org.jgrapht.graph.DefaultWeightedEdge;
22+
import org.jgrapht.graph.SimpleWeightedGraph;
23+
import org.nlpub.watset.graph.*;
24+
import org.nlpub.watset.util.Sense;
25+
26+
import java.util.function.Function;
27+
28+
public class FuzzyClustering {
29+
public static void main(String[] args) {
30+
Graph<String, DefaultWeightedEdge> graph = SimpleWeightedGraph.<String, DefaultWeightedEdge>createBuilder(DefaultWeightedEdge.class).
31+
addVertices("a", "b", "c", "d", "e").
32+
addEdge("a", "b").
33+
addEdge("a", "c").
34+
addEdge("a", "c").
35+
addEdge("d", "e").
36+
build();
37+
38+
System.out.print("Graph: ");
39+
System.out.println(graph);
40+
41+
// MaxMax Example
42+
MaxMax<String, DefaultWeightedEdge> maxmax = new MaxMax<>(graph);
43+
maxmax.fit();
44+
45+
System.out.print("MaxMax Digraph: ");
46+
System.out.println(maxmax.getDigraph());
47+
48+
System.out.print("MaxMax Clusters: ");
49+
System.out.println(maxmax.getClusters());
50+
51+
// Watset Example
52+
Function<Graph<String, DefaultWeightedEdge>, Clustering<String>> local = ChineseWhispers.provider(NodeWeighting.top());
53+
Function<Graph<Sense<String>, DefaultWeightedEdge>, Clustering<Sense<String>>> global = ChineseWhispers.provider(NodeWeighting.top());
54+
55+
SimplifiedWatset<String, DefaultWeightedEdge> watset = new SimplifiedWatset<>(graph, local, global);
56+
watset.fit();
57+
58+
System.out.print("Watset Sense Graph: ");
59+
System.out.println(watset.getSenseGraph());
60+
61+
System.out.print("Watset Clusters: ");
62+
System.out.println(watset.getClusters());
63+
}
64+
}

examples/HardClustering.groovy

Lines changed: 0 additions & 60 deletions
This file was deleted.

examples/HardClustering.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// java -cp watset.jar HardClustering.java
2+
3+
/*
4+
* Copyright 2019 Dmitry Ustalov
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
20+
import org.jgrapht.Graph;
21+
import org.jgrapht.graph.DefaultWeightedEdge;
22+
import org.jgrapht.graph.SimpleWeightedGraph;
23+
import org.nlpub.watset.graph.*;
24+
import org.nlpub.watset.util.Sense;
25+
26+
import java.util.function.Function;
27+
28+
public class HardClustering {
29+
public static void main(String[] args) {
30+
Graph<String, DefaultWeightedEdge> graph = SimpleWeightedGraph.<String, DefaultWeightedEdge>createBuilder(DefaultWeightedEdge.class).
31+
addVertices("a", "b", "c", "d", "e").
32+
addEdge("a", "b").
33+
addEdge("a", "c").
34+
addEdge("a", "c").
35+
addEdge("d", "e").
36+
build();
37+
38+
System.out.print("Graph: ");
39+
System.out.println(graph);
40+
41+
// Empty Example
42+
EmptyClustering<String> empty = new EmptyClustering<>();
43+
empty.fit();
44+
45+
System.out.print("Empty Clusters: ");
46+
System.out.println(empty.getClusters());
47+
48+
// Singleton Example
49+
SingletonClustering<String, DefaultWeightedEdge> singleton = new SingletonClustering<>(graph);
50+
singleton.fit();
51+
52+
System.out.print("Singleton Clusters: ");
53+
System.out.println(singleton.getClusters());
54+
55+
// Together Example
56+
TogetherClustering<String, DefaultWeightedEdge> together = new TogetherClustering<>(graph);
57+
together.fit();
58+
59+
System.out.print("Together Clusters: ");
60+
System.out.println(together.getClusters());
61+
62+
// Components Clustering Example
63+
ComponentsClustering<String, DefaultWeightedEdge> components = new ComponentsClustering<>(graph);
64+
components.fit();
65+
66+
System.out.print("Components Clusters: ");
67+
System.out.println(components.getClusters());
68+
69+
// Chinese Whispers Example
70+
ChineseWhispers<String, DefaultWeightedEdge> cw = new ChineseWhispers<>(graph, NodeWeighting.top());
71+
cw.fit();
72+
73+
System.out.print("Chinese Whispers Clusters: ");
74+
System.out.println(cw.getClusters());
75+
76+
// Markov Clustering Example
77+
MarkovClustering<String, DefaultWeightedEdge> mcl = new MarkovClustering<>(graph, 2, 2);
78+
mcl.fit();
79+
80+
System.out.print("Markov Clustering Clusters: ");
81+
System.out.println(mcl.getClusters());
82+
}
83+
}

examples/Makefile

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
export LANG := en_US.UTF-8
22
export CLASSPATH := $(CURDIR)/watset.jar
33

4-
# https://issues.apache.org/jira/browse/GROOVY-8339 is really annoying
5-
export JAVA_OPTS := --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang.annotation=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED
6-
7-
ifeq ($(shell which groovy),)
8-
$(error Please install Groovy and add it to PATH)
9-
endif
10-
11-
RELEASE := 2.0.0
4+
RELEASE := 2.1.3
125

136
all: HardClustering FuzzyClustering PickleGraph PickleClustering CommandLine
147

158
clean:
169
rm -fv *.pkl *.jar
1710

18-
%: %.groovy | watset.jar
19-
groovy $<
11+
%: %.java | watset.jar
12+
java "$<"
2013

2114
%: %.py
22-
python3 $<
15+
python3 "$<"
2316

2417
%: %.sh | watset.jar
25-
$(SHELL) $<
18+
$(SHELL) "$<"
2619

2720
watset.jar:
2821
curl -sLo "$@" "https://github.com/nlpub/watset-java/releases/download/$(RELEASE)/watset.jar"

examples/PickleClustering.groovy

Lines changed: 0 additions & 44 deletions
This file was deleted.

examples/PickleClustering.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// python3 PickleGraph.py
2+
// java -cp watset.jar PickleClustering.java
3+
4+
/*
5+
* Copyright 2020 Dmitry Ustalov
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
import net.razorvine.pickle.Unpickler;
22+
import net.razorvine.pickle.objects.ClassDict;
23+
import org.jgrapht.Graph;
24+
import org.jgrapht.graph.DefaultWeightedEdge;
25+
import org.nlpub.watset.graph.*;
26+
import org.nlpub.watset.util.NetworkXFormat;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.nio.file.Files;
31+
import java.nio.file.Paths;
32+
33+
public class PickleClustering {
34+
public static void main(String[] args) throws IOException {
35+
try (InputStream stream = Files.newInputStream(Paths.get("karate_club_graph.pkl"))) {
36+
ClassDict nx = NetworkXFormat.parse(stream);
37+
Graph<Object, DefaultWeightedEdge> graph = NetworkXFormat.load(nx);
38+
39+
ChineseWhispers<Object, DefaultWeightedEdge> cw = new ChineseWhispers<>(graph, NodeWeighting.top());
40+
cw.fit();
41+
42+
System.out.print("Chinese Whispers Clusters: ");
43+
System.out.println(cw.getClusters());
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)