Skip to content

Commit

Permalink
Optimize remove_unreachable_nodes in gensim.summarization (#2263)
Browse files Browse the repository at this point in the history
* slightly optimize remove_unreachable_nodes

* add test
  • Loading branch information
horpto authored and menshikh-iv committed Jan 8, 2019
1 parent e0bfb3f commit 02e8bf5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gensim/summarization/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ def remove_unreachable_nodes(graph):
"""

for node in graph.nodes():
if sum(graph.edge_weight((node, other)) for other in graph.neighbors(node)) == 0:
if all(graph.edge_weight((node, other)) == 0 for other in graph.neighbors(node)):
graph.del_node(node)
24 changes: 24 additions & 0 deletions gensim/test/test_summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@
from gensim import utils
from gensim.corpora import Dictionary
from gensim.summarization import summarize, summarize_corpus, keywords, mz_keywords
from gensim.summarization.commons import remove_unreachable_nodes, build_graph


class TestCommons(unittest.TestCase):

def _build_graph(self):
graph = build_graph(['a', 'b', 'c', 'd'])
graph.add_edge(('a', 'b'))
graph.add_edge(('b', 'c'))
graph.add_edge(('c', 'a'))
return graph

def test_remove_unreachable_nodes(self):
graph = self._build_graph()
self.assertTrue(graph.has_node('d'))
remove_unreachable_nodes(graph)
self.assertFalse(graph.has_node('d'))

graph = self._build_graph()
graph.add_edge(('d', 'a'), wt=0.0)
graph.add_edge(('b', 'd'), wt=0)
self.assertTrue(graph.has_node('d'))
remove_unreachable_nodes(graph)
self.assertFalse(graph.has_node('d'))


class TestSummarizationTest(unittest.TestCase):
Expand Down

0 comments on commit 02e8bf5

Please sign in to comment.