miso-belica / sumy

Module for automatic summarization of text documents and HTML pages.
https://miso-belica.github.io/sumy/
Apache License 2.0
3.46k stars 525 forks source link

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

Open Archkik opened 1 year ago

Archkik commented 1 year ago

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

miso-belica commented 1 year ago

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 commented 1 year ago

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