miso-belica/sumy

question: how could I extract a specific number of keywords instead of sentence?

Archkik opened this issue · 2 comments

how could I extract a specific number of keywords instead of sentence with python API?

You can pick from the summary anything you want by providing custom function. The function gets collection if SentenceInfo objects.

# -*- coding: utf-8 -*-

from sumy.parsers.html import HtmlParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer as Summarizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words


LANGUAGE = "english"


def pick_sentences(infos: list[SentenceInfo]):
	# your algorithm here
	return [] # any SentenceInfo objects you want to pick


if __name__ == "__main__":
    url = "https://en.wikipedia.org/wiki/Automatic_summarization"
    parser = HtmlParser.from_url(url, Tokenizer(LANGUAGE))
    stemmer = Stemmer(LANGUAGE)

    summarizer = Summarizer(stemmer)
    summarizer.stop_words = get_stop_words(LANGUAGE)

    for sentence in summarizer(parser.document, pick_sentences):
        print(sentence)

@Archkik does this work for your use-case? Is your issue different somehow? Can you describe what you are trying to achieve then?