Spaces:
Running
Running
| """ | |
| Extractive summarization via TextRank (sumy). | |
| Ringan, tanpa model berat. | |
| """ | |
| from typing import List, Dict | |
| import re | |
| def summarize(text: str, num_sentences: int = 3) -> Dict: | |
| text = (text or "").strip() | |
| if not text: | |
| return {"summary": "", "sentences": []} | |
| try: | |
| from sumy.parsers.plaintext import PlaintextParser | |
| from sumy.nlp.tokenizers import Tokenizer | |
| from sumy.summarizers.text_rank import TextRankSummarizer | |
| parser = PlaintextParser.from_string(text, Tokenizer("english")) | |
| summarizer = TextRankSummarizer() | |
| sentences = [str(s) for s in summarizer(parser.document, num_sentences)] | |
| if sentences: | |
| return {"summary": " ".join(sentences), "sentences": sentences} | |
| except Exception: | |
| pass | |
| # Fallback: ambil N kalimat terpanjang (proxy informativeness) | |
| raw = re.split(r"(?<=[.!?])\s+", text) | |
| raw = [s.strip() for s in raw if len(s.strip()) > 40] | |
| raw.sort(key=len, reverse=True) | |
| picked = raw[:num_sentences] | |
| return {"summary": " ".join(picked), "sentences": picked} | |