文本摘要器
通过提取最重要的句子来总结长文本。
110
关于此工具
文本摘要工具通过识别和保留关键句子,帮助您快速从长文档、文章或报告中提取最重要的信息。这个工具对于任何处理大量文本的人都是必不可少的——无论您是审阅研究论文的学生、管理多份文档的专业人士,还是想在不花费数小时阅读的情况下获取信息的人。
只需将您的文本粘贴或输入到输入字段中,根据需要调整摘要长度,该工具就会智能地识别最重要的句子并以简洁格式呈现。它在您的浏览器中即时工作,无需任何账户或将文件上传到外部服务器,是一个快速且私密的日常文本压缩解决方案。
该摘要工具最适合用于新闻文章、学术论文和商业文件等形式良好、结构清晰的内容,其中重要信息表述明确。对于创意写作或包含高度专业术语的内容,结果可能会有所不同,因此总是值得将摘要与原文进行比较,以确保没有遗漏任何关键细节。
常见问题
代码实现
# Extractive text summarization (TF-based)
import re
from collections import Counter
STOP_WORDS = {
"the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for",
"of", "with", "by", "from", "is", "was", "are", "were", "be", "been",
"has", "have", "had", "do", "does", "did", "will", "would", "could",
"should", "that", "this", "it", "its", "he", "she", "they", "we", "you",
"i", "not", "no", "as", "if", "so", "than", "then", "more", "most",
}
def tokenize_sentences(text: str) -> list[str]:
sentences = re.split(r'(?<=[.!?])\s+', text.strip())
return [s.strip() for s in sentences if len(s.strip()) > 10]
def word_frequency(sentences: list[str]) -> dict[str, int]:
words = re.findall(r"[a-z']+", " ".join(sentences).lower())
return Counter(w for w in words if w not in STOP_WORDS and len(w) > 2)
def score_sentences(sentences: list[str], freq: dict[str, int]) -> list[float]:
scores = []
n = len(sentences)
for i, sentence in enumerate(sentences):
words = re.findall(r"[a-z']+", sentence.lower())
score = sum(freq.get(w, 0) for w in words)
if words:
score /= len(words) # normalize by length
# Position weight
rel_pos = i / max(n - 1, 1)
if rel_pos <= 0.2:
score *= 1.4
elif rel_pos >= 0.8:
score *= 1.2
scores.append(score)
return scores
def summarize(text: str, num_sentences: int = 3) -> str:
sentences = tokenize_sentences(text)
if len(sentences) <= num_sentences:
return text
freq = word_frequency(sentences)
scores = score_sentences(sentences, freq)
# Get top-N sentence indices, sort by original position
ranked = sorted(range(len(scores)), key=lambda i: -scores[i])[:num_sentences]
selected = sorted(ranked)
return " ".join(sentences[i] for i in selected)
# Example
text = """...""" # Your long text here
print(summarize(text, num_sentences=3))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.