Coverage for agentos/marketplace/skills/summarize/summarize.py: 12%
41 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 23:17 +0800
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 23:17 +0800
1"""
2summarize — Text summarization using extractive and heuristic methods.
4Actions: summarize, extract_keywords, bullet_points, word_count
5No external API required — pure Python.
6"""
8import re
9from collections import Counter
10from typing import Any
13def run(
14 action: str = "summarize",
15 text: str = "",
16 file_path: str = "",
17 ratio: float = 0.3,
18 **kwargs: Any,
19) -> str:
20 content = text
21 if file_path:
22 try:
23 with open(file_path, encoding="utf-8") as f:
24 content = f.read()
25 except FileNotFoundError:
26 return f"[summarize] File not found: {file_path}"
27 except Exception as e:
28 return f"[summarize] Error reading file: {e}"
30 if not content:
31 return "[summarize] No text provided."
33 if action == "word_count":
34 words = re.findall(r"\b\w+\b", content.lower())
35 wc = len(words)
36 sc = len(re.split(r"[.!?]+", content))
37 return f"Words: {wc}, Sentences: ~{sc}, Characters: {len(content)}"
39 if action == "extract_keywords":
40 words = re.findall(r"\b[a-zA-Z]{3,}\b", content.lower())
41 stop = {
42 "the",
43 "and",
44 "for",
45 "that",
46 "with",
47 "this",
48 "from",
49 "have",
50 "are",
51 "not",
52 "but",
53 "was",
54 "you",
55 "all",
56 "can",
57 "has",
58 "had",
59 "been",
60 "will",
61 "they",
62 "its",
63 "their",
64 "them",
65 "our",
66 "than",
67 "then",
68 "also",
69 "into",
70 "just",
71 "about",
72 "more",
73 "some",
74 "when",
75 "your",
76 "which",
77 "make",
78 "like",
79 "what",
80 "over",
81 "such",
82 "here",
83 "were",
84 "how",
85 "one",
86 "two",
87 }
88 filtered = [w for w in words if w not in stop]
89 top = Counter(filtered).most_common(15)
90 return "Top keywords: " + ", ".join(f"{w}({c})" for w, c in top)
92 if action == "bullet_points":
93 sentences = re.split(r"(?<=[.!?])\s+", content)
94 sentences = [s.strip() for s in sentences if len(s.strip()) > 20]
95 n = max(3, int(len(sentences) * ratio))
96 return "[summarize] Bullet Points:\n" + "\n".join(f"- {s}" for s in sentences[:n])
98 # Default: summarize (extractive)
99 sentences = re.split(r"(?<=[.!?])\s+", content)
100 sentences = [s.strip() for s in sentences if len(s.strip()) > 10]
101 if not sentences:
102 return content[:500]
104 n = max(2, int(len(sentences) * ratio))
105 summary = " ".join(sentences[:n])
106 if len(summary) > 1500:
107 summary = summary[:1500] + "..."
108 return summary
111__all__ = ["run"]