Skip to content

Commit

Permalink
Fix ZeroDivisionError keywords issue with short input (#2154)
Browse files Browse the repository at this point in the history
* Return keywords before matrix processing if no graph edges.

* Add testcase for empty graph.
  • Loading branch information
lshostenko authored and menshikh-iv committed Aug 10, 2018
1 parent 0107800 commit 17fa0dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gensim/summarization/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ def keywords(text, ratio=0.2, words=None, split=False, scores=False, pos_filter=

_remove_unreachable_nodes(graph)

if not graph.edges():
return _format_results([], [], split, scores)

# Ranks the tokens using the PageRank algorithm. Returns dict of lemma -> score
pagerank_scores = _pagerank(graph)

Expand Down
6 changes: 6 additions & 0 deletions gensim/test/test_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def test_text_keywords_with_small_graph(self):
kwds = keywords(text, words=1, split=True)
self.assertTrue(len(kwds))

def test_text_keywords_without_graph_edges(self):
# regression test, we get graph with no edges on this text
text = 'Sitio construcción. Estaremos línea.'
kwds = keywords(text, deacc=False, scores=True)
self.assertFalse(len(kwds))


if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
Expand Down

0 comments on commit 17fa0dc

Please sign in to comment.