Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verification when summarize_corpus returns null. Fix #1531. #1570

Merged
merged 5 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion gensim/summarization/summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def summarize_corpus(corpus, ratio=0.2):
_set_graph_edge_weights(graph)
_remove_unreachable_nodes(graph)

# Cannot calculate eigenvectors if number of unique words in text < 3. Warns user to add more text. The function ends.
# Cannot calculate eigenvectors if number of unique documents in corpus < 3.
# Warns user to add more text. The function ends.
if len(graph.nodes()) < 3:
logger.warning("Please add more sentences to the text. The number of reachable nodes is below 3")
return
Expand Down Expand Up @@ -211,6 +212,11 @@ def summarize(text, ratio=0.2, word_count=None, split=False):

most_important_docs = summarize_corpus(corpus, ratio=ratio if word_count is None else 1)

# If couldn't get important docs, the algorithm ends.
if not most_important_docs:
logger.warning("Couldn't get relevant sentences.")
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raising an exception better? I'm not sure whether this is an error state, or just a warning.

Many people don't have logging enabled, and the docstring implies the result of this function is a string (not None).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is an error. Perhaps it makes even more sense to actually return the entire text as the summary wasn't possible, but this will break compatibility with the old behavior.

Regarding the docstring, the method returns a string or a list if the split parameter was set to true, so perhaps the best thing to do is:

if not most_important_docs:
    logger.warning("Couldn't get relevant sentences.")
    return [] if split else ""

Copy link
Owner

@piskvorky piskvorky Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks like a good solution 👍
Except we probably want to work with unicode in general ("" => u"", if the rest of the code uses proper unicode too).

CC @menshikh-iv


# Extracts the most important sentences with the selected criterion.
extracted_sentences = _extract_important_sentences(sentences, corpus, most_important_docs, word_count)

Expand Down
7 changes: 7 additions & 0 deletions gensim/test/test_summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ def test_low_distinct_words_corpus_summarization_is_none(self):

self.assertTrue(summarize_corpus(corpus) is None)

def test_low_distinct_words_summarization_is_none(self):
pre_path = os.path.join(os.path.dirname(__file__), 'test_data')

with utils.smart_open(os.path.join(pre_path, "testlowdistinctwords.txt"), mode="r") as f:
text = f.read()

self.assertTrue(summarize(text) is None)

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