私は Neo4j を扱い始めているので、このトピックについてはあまり詳しくありません。下の図では、緑のノードが「ドキュメント」を表し、赤のノードが特定のドキュメントで発生する「用語」を表す 2 モード (二部) グラフがあります。(実際のグラフは実際には巨大です: 約 20.000.000 ドキュメントと 25.000 用語)。
neo4j (Cypher または Java) で用語の共起ペアをどのように数えているのだろうか。クエリからの望ましい出力は次のようになります。
# Example: Pair (term-1, term-2) occurs in doc-1 and in doc-3
# Frequency for pair (term-1, term-2) should be 2
# termA | term B | frequency
term-1 | term-2 | 2
term-1 | term-3 | 1
term-2 | term-3 | 2
グラフはhttp://console.neo4j.org/r/7fmo7cで入手できます
Neo4jでテストグラフを再現するコード
set name root
mkrel -t ROOT -c -v
cd 1
set name doc-1
set type document
mkrel -t HAVE -cv
cd 2
set name term-1
set type term
cd ..
mkrel -t HAVE -cv
cd 3
set name term-2
set type term
cd ..
mkrel -t HAVE -cv
cd 4
set name term-3
set type term
mkrel -t HAVE -d INCOMING -c
cd 5
set name doc-2
set type document
mkrel -t HAVE -d OUTGOING -n 3
cd 3
mkrel -t HAVE -d INCOMING -c
cd 6
set name doc-3
set type document
mkrel -t HAVE -d OUTGOING -n 2
テストグラフをJavaで再現するコード
import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
public class CountPairs {
private static final String DB_PATH = "test.db";
private static GraphDatabaseService graphDb;
public static void main(String[] args) {
graphDb = new GraphDatabaseFactory().
newEmbeddedDatabaseBuilder(DB_PATH).
setConfig(GraphDatabaseSettings.node_keys_indexable, "name, type").
setConfig(GraphDatabaseSettings.node_auto_indexing, "true").
newGraphDatabase();
Transaction tx = graphDb.beginTx();
Node doc1, doc2, doc3 = null;
Node term1, term2, term3 = null;
Relationship rel1, rel2, rel3, rel4, rel5, rel6, rel7 = null;
try
{
// Create nodes
doc1 = graphDb.createNode();
doc2 = graphDb.createNode();
doc3 = graphDb.createNode();
term1 = graphDb.createNode();
term2 = graphDb.createNode();
term3 = graphDb.createNode();
// Set properties
doc1.setProperty("name", "doc1");
doc1.setProperty("type", "document");
doc2.setProperty("name", "doc2");
doc2.setProperty("type", "document");
doc3.setProperty("name", "doc3");
doc3.setProperty("type", "document");
// Set properties
term1.setProperty("name", "term1");
term1.setProperty("type", "term");
term2.setProperty("name", "term2");
term2.setProperty("type", "term");
term3.setProperty("name", "term3");
term3.setProperty("type", "term");
// Create relations
rel1 = doc1.createRelationshipTo(term1, DynamicRelationshipType.withName("HAVE"));
rel2 = doc1.createRelationshipTo(term2, DynamicRelationshipType.withName("HAVE"));
rel3 = doc1.createRelationshipTo(term3, DynamicRelationshipType.withName("HAVE"));
rel4 = doc2.createRelationshipTo(term2, DynamicRelationshipType.withName("HAVE"));
rel5 = doc2.createRelationshipTo(term3, DynamicRelationshipType.withName("HAVE"));
rel6 = doc3.createRelationshipTo(term1, DynamicRelationshipType.withName("HAVE"));
rel7 = doc3.createRelationshipTo(term2, DynamicRelationshipType.withName("HAVE"));
tx.success();
}
catch(Exception e)
{
tx.failure();
}
finally
{
tx.finish();
}
graphDb.shutdown();
}
}