From c0a17d1b4f595f5117790ce13716e3b11f98238c Mon Sep 17 00:00:00 2001 From: Aryaz Eghbali Date: Mon, 5 Jun 2023 15:48:03 +0200 Subject: [PATCH 1/3] Fixed bug with power iteration --- sumy/summarizers/lex_rank.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sumy/summarizers/lex_rank.py b/sumy/summarizers/lex_rank.py index c6d230a5..85f0bf2f 100644 --- a/sumy/summarizers/lex_rank.py +++ b/sumy/summarizers/lex_rank.py @@ -164,6 +164,7 @@ def power_method(matrix, epsilon): while lambda_val > epsilon: next_p = numpy.dot(transposed_matrix, p_vector) + next_p /= numpy.linalg.norm(next_p) lambda_val = numpy.linalg.norm(numpy.subtract(next_p, p_vector)) p_vector = next_p From 7802d3b7fe9c06b58c0821238f2485a5b1d97662 Mon Sep 17 00:00:00 2001 From: Aryaz Eghbali Date: Mon, 5 Jun 2023 15:48:03 +0200 Subject: [PATCH 2/3] Fixed bug with power iteration --- sumy/summarizers/lex_rank.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sumy/summarizers/lex_rank.py b/sumy/summarizers/lex_rank.py index c6d230a5..85f0bf2f 100644 --- a/sumy/summarizers/lex_rank.py +++ b/sumy/summarizers/lex_rank.py @@ -164,6 +164,7 @@ def power_method(matrix, epsilon): while lambda_val > epsilon: next_p = numpy.dot(transposed_matrix, p_vector) + next_p /= numpy.linalg.norm(next_p) lambda_val = numpy.linalg.norm(numpy.subtract(next_p, p_vector)) p_vector = next_p From 8b7cc9a82f563828d3e1ba3b6b9bf4540697b47f Mon Sep 17 00:00:00 2001 From: Aryaz Eghbali Date: Thu, 10 Aug 2023 10:35:54 +0200 Subject: [PATCH 3/3] Added test for infinite issue with power_method --- tests/test_summarizers/test_lex_rank.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_summarizers/test_lex_rank.py b/tests/test_summarizers/test_lex_rank.py index 85f99999..1e9c0572 100644 --- a/tests/test_summarizers/test_lex_rank.py +++ b/tests/test_summarizers/test_lex_rank.py @@ -169,3 +169,16 @@ def test_power_method_should_return_different_scores_for_sentences(): scores = LexRankSummarizer.power_method(matrix, LexRankSummarizer.epsilon) assert len(frozenset(scores.tolist())) > 1 + +def test_power_method_should_return_finite(): + """See https://github.com/miso-belica/sumy/issues/187""" + matrix = numpy.array([ + [0.1, 0.2, 0.3, 0.6, 0.9], + [0.45, 0, 0.3, 0.6, 0], + [0.5, 0.6, 0.3, 1, 0.9], + [0.7, 0, 0, 0.6, 0], + [0.5, 0.123, 0, 0.111, 0.9], + ]) + scores = LexRankSummarizer.power_method(matrix, LexRankSummarizer.epsilon) + + assert all(numpy.isfinite(scores)) \ No newline at end of file