Repository navigation
Expand file tree
/
Copy pathcompressor.py
More file actions
56 lines (43 loc) · 2 KB
/
Copy pathcompressor.py
File metadata and controls
56 lines (43 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
compressor.py — LexRank extractive transcript compression.
Reduces long transcripts to the top 25% most semantically central sentences
before sending to Gemini. Safe fallback: returns original text unchanged if
sumy is unavailable or fails.
Author: Adith K B
"""
from __future__ import annotations
COMPRESS_THRESHOLD_CHARS = 16_000 # ~4k tokens; config overrides this
COMPRESS_TARGET_RATIO = 0.25 # keep top 25% of sentences
def compress_transcript(
text: str,
threshold_chars: int = COMPRESS_THRESHOLD_CHARS,
target_ratio: float = COMPRESS_TARGET_RATIO,
) -> tuple[str, bool, int, int]:
"""
Compress a transcript using LexRank extractive summarization.
Returns:
(compressed_text, was_compressed, original_chars, compressed_chars)
Falls back to (original_text, False, len(text), len(text)) on any failure.
"""
if not text or len(text) <= threshold_chars:
return text, False, len(text or ""), len(text or "")
try:
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words
parser = PlaintextParser.from_string(text, Tokenizer("english"))
stemmer = Stemmer("english")
summary = LexRankSummarizer(stemmer)
summary.stop_words = get_stop_words("english")
# Count total sentences, target top ratio
total_sentences = len(list(parser.document.sentences))
keep = max(10, int(total_sentences * target_ratio))
compressed_sentences = summary(parser.document, keep)
compressed = " ".join(str(sentence) for sentence in compressed_sentences)
return compressed, True, len(text), len(compressed)
except Exception as e:
# sumy not installed, NLTK data missing, or any other failure
print(f"[COMPRESS] Failed: {e}")
return text, False, len(text), len(text)