sumy icon indicating copy to clipboard operation
sumy copied to clipboard

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

Open Archkik opened this issue 2 years ago • 2 comments

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

Archkik avatar Jul 21 '22 10:07 Archkik

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)

miso-belica avatar Jul 21 '22 12:07 miso-belica

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

miso-belica avatar Oct 23 '22 16:10 miso-belica