def test_textnet_clustering(corpus): """Test calculating clustering coefficients.""" noun_phrases = corpus.noun_phrases() n_np = tn.Textnet(noun_phrases, connected=True) assert len(n_np.clustering) == n_np.graph.vcount()
def test_save(tmpdir, corpus): """Test Textnet graph saving.""" noun_phrases = corpus.noun_phrases() n_np = tn.Textnet(noun_phrases) out = tmpdir.join("graph.graphml") n_np.save_graph(str(out)) assert len(tmpdir.listdir()) == 1
def test_plot_backbone(tmpdir, corpus): """Test ProjectedTextnet plotting with alpha cut.""" n = tn.Textnet(corpus.tokenized()) papers = n.project(node_type="doc") out = tmpdir.join("plot-3.png") plot = papers.plot(alpha=0.4, label_nodes=True, target=str(out)) assert len(plot._objects) > 0 assert len(tmpdir.listdir()) == 1
def test_plot_projected(tmpdir, corpus): """Test ProjectedTextnet plotting.""" n = tn.Textnet(corpus.tokenized()) papers = n.project(node_type="doc") out = tmpdir.join("plot-2.png") plot = papers.plot(show_clusters=True, label_nodes=True, target=str(out)) assert len(plot._objects) > 0 assert len(tmpdir.listdir()) == 1
def test_plot_layout(tmpdir, corpus): """Test Textnet plotting with bipartite layout and node labels.""" noun_phrases = corpus.noun_phrases() n_np = tn.Textnet(noun_phrases) out = tmpdir.join("plot-1.png") plot = n_np.plot(target=str(out), bipartite_layout=True, label_nodes=True) assert len(plot._objects) > 0 assert len(tmpdir.listdir()) == 1
def test_plot(tmpdir, corpus): """Test Textnet plotting.""" noun_phrases = corpus.noun_phrases() n_np = tn.Textnet(noun_phrases) out = tmpdir.join("plot-0.png") plot = n_np.plot(target=str(out)) assert len(plot._objects) > 0 assert len(tmpdir.listdir()) == 1
def test_plot_scaled(tmpdir, corpus): """Test ProjectedTextnet plotting with scaled nodes.""" n = tn.Textnet(corpus.tokenized()) papers = n.project(node_type="doc") out = tmpdir.join("plot-4.png") plot = papers.plot(scale_nodes_by="betweenness", label_nodes=True, target=str(out)) assert len(plot._objects) > 0 assert len(tmpdir.listdir()) == 1
def test_textnet_birank(corpus): """Test calculating BiRank.""" noun_phrases = corpus.noun_phrases() n_np = tn.Textnet(noun_phrases, connected=True) assert len(n_np.birank) == n_np.graph.vcount() assert len(n_np.cohits) == n_np.graph.vcount() assert len(n_np.hits) == n_np.graph.vcount() bgrm = tn.network.bipartite_rank(n_np, normalizer="BGRM", max_iter=200) assert len(bgrm) == n_np.graph.vcount()
def test_textnet(corpus): """Test Textnet class using sample data.""" noun_phrases = corpus.noun_phrases() n_np = tn.Textnet(noun_phrases) assert n_np.graph.vcount() > 0 assert n_np.graph.ecount() > 0 g_np_groups = n_np.project(node_type="doc") assert g_np_groups.vcount() > 0 assert g_np_groups.ecount() > 0 g_np_words = n_np.project(node_type="term") assert g_np_words.vcount() > 0 assert g_np_words.ecount() > 0
def test_plot_filtered(tmpdir, corpus): """Test ProjectedTextnet plotting filtered labels.""" n = tn.Textnet(corpus.tokenized()) papers = n.project(node_type="doc") out = tmpdir.join("plot-5.png") plot = papers.plot( label_nodes=True, label_edges=True, node_label_filter=lambda v: v.degree() > 2, edge_label_filter=lambda e: e["weight"] > 0.1, target=str(out), ) assert len(plot._objects) > 0 assert len(tmpdir.listdir()) == 1
def test_textnet_save_and_load(corpus, tmpdir): """Test roundtrip of saving and loading a textnet from file.""" out = tmpdir.join("out.textnet") net = tn.Textnet( corpus.tokenized(), connected=True, doc_attrs={"test": { "New York Times": 1, "Los Angeles Times": 3 }}, ) net.save(out) loaded = tn.load_textnet(out) assert net.nodes["id"] == loaded.nodes["id"] assert net.edges["weight"] == loaded.edges["weight"] assert net.summary == loaded.summary
def test_html_repr(corpus): assert tn._repr_html_() != "" assert corpus._repr_html_() != "" assert tn.Textnet(corpus.tokenized())._repr_html_() != ""
def test_context(corpus): """Test formal context creation from textnet.""" n = tn.Textnet(corpus.tokenized(sublinear=False)) ctx = n.context assert len(ctx) == 3