compress

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package compress implements the prompt-compression algorithm GrayCode native prompt-compression implementation.

Algorithm overview:

  1. Split input into segments (sentences, preserving code/URLs).
  2. Per segment, run the auto-clarity check: segments containing security/destructive keywords are passed through verbatim.
  3. For safe segments, apply the dictionary (eng/v1) first (longest-match-first phrase substitutions).
  4. Then apply the drop-list for the chosen intensity level (Lite/Full/Ultra).
  5. Re-join segments, preserving original whitespace.

Package compress implements the prompt-compression algorithm GrayCode native prompt-compression implementation.

The algorithm is a pure text-rewrite engine. Three intensity levels (Lite, Full, Ultra) progressively drop articles, filler words, pleasantries, hedging, and redundant conjunctions. A built-in dictionary (eng/v1) substitutes verbose phrases with terse equivalents.

Auto-clarity rules (see safety.go) prevent compression of security warnings, destructive operations, and code-bearing segments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSafeSegment

func IsSafeSegment(s string) bool

IsSafeSegment returns true if s contains no safety keywords. A segment with safety keywords must be passed through unchanged.

func SplitSafeSegments

func SplitSafeSegments(s string) []segment

SplitSafeSegments splits s into segments that are safe to compress and segments that must be preserved verbatim. Returns a slice of (text, safe) pairs. Adjacent safe/unsafe segments are merged by their flag.

func SubstituteDict

func SubstituteDict(s string) string

SubstituteDict applies the eng/v1 dictionary to s, replacing verbose phrases with terse equivalents. Case-insensitive on keys; preserves the original casing of the first character of the match.

Multi-word keys are matched as whole phrases. The longest match is preferred to avoid partial replacements.

Types

type CompressResult

type CompressResult struct {
	// Original is the input verbatim.
	Original string
	// Compressed is the rewritten text.
	Compressed string
	// Stats for the operation.
	Stats Stats
}

CompressResult reports what the compression did to the input.

type Intensity

type Intensity int

Intensity controls the aggressiveness of prompt compression.

The intensity ordering is Lite < Full < Ultra. Higher intensity drops more word classes and applies more aggressive dictionary substitutions.

const (
	// Lite drops only pleasantries and obvious filler; preserves articles.
	Lite Intensity = iota
	// Full drops articles, filler, pleasantries, and hedging.
	// This is the default.
	Full
	// Ultra additionally drops conjunctions and forces sentence fragments.
	Ultra
)

func (Intensity) String

func (i Intensity) String() string

String returns the canonical name of the intensity level.

type Stats

type Stats struct {
	Intensity             Intensity
	OriginalBytes         int
	CompressedBytes       int
	OriginalWords         int
	CompressedWords       int
	BytesSaved            int
	PercentOff            float64
	DroppedArticles       int
	DroppedFiller         int
	DroppedHedge          int
	DroppedConjunctions   int
	DictionaryHits        int
	PassThroughSegments   int
	CompressedSegments    int
	BytesSavedByDict      int
	BytesSavedByDrops     int
	RefusedDueToSensitive bool
	// Per-segment counts (informational; first N at most).
	SensitiveKeywordsHit []string
}

Stats reports per-stage counts.

func Compress

func Compress(s string, intensity Intensity) (string, Stats)

Compress applies the algorithm to s at the given intensity.

Behavior:

  • Empty input returns (input, empty result) untouched.
  • CJK-heavy text is passed through (compression rules assume Latin grammar).
  • Sensitive segments (security/destructive keywords) are preserved verbatim regardless of intensity.
  • Multi-word drop-list entries ("of course", "feel free") are matched as whole phrases.
  • Dictionary substitutions are case-insensitive and preserve the case of the first character of each match.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL