Skip to main content

Surgical, occurrence-based word manipulation for strings and files

Project description

wordscalpel 🔪

Surgical, occurrence-based word manipulation for strings and files.

Log sanitization, document redaction, and content pipelines often need precise word-level control that Python's stdlib doesn't provide. wordscalpel solves that.


Installation

pip install wordscalpel
# or from source:
git clone https://github.com/yourname/wordscalpel
cd wordscalpel && pip install -e .

Quick Start

import wordscalpel as ws

text = "the cat sat on the mat near the hat"

# Count occurrences
ws.count_occurrences(text, "the")           # → 3

# Get character positions
ws.get_positions(text, "the")               # → [(0,3), (15,18), (28,31)]

# Remove the 2nd occurrence
ws.remove_occurrence(text, "the", 2)        # → "the cat sat on  mat near the hat"

# Remove occurrences 1 through 2
ws.remove_occurrence_range(text, "the", 1, 2)

# Remove all occurrences
ws.remove_all_occurrences(text, "the")

# Replace the 2nd occurrence
ws.replace_occurrence(text, "the", "a", 2) # → "the cat sat on a mat near the hat"

# Replace all occurrences
ws.replace_all_occurrences(text, "the", "a")

# Case-insensitive mode
ws.count_occurrences("The the THE", "the", case_sensitive=False)  # → 3

File Operations

from wordscalpel.file_ops import process_file, file_count_occurrences

# Count in file
file_count_occurrences("input.txt", "error")

# Remove 2nd occurrence and save
process_file("input.txt", "output.txt", "error", n=2, operation="remove")

# Replace all
process_file("input.txt", "output.txt", "error", operation="replace", replacement="warning")

# Remove range
process_file("input.txt", "output.txt", "error", operation="remove_range", start_n=2, end_n=4)

CLI

# Remove 2nd occurrence of "error" from a file
wordscalpel remove --word "error" --n 2 --file input.txt --output out.txt

# Remove all occurrences
wordscalpel remove --word "error" --all --file input.txt --output out.txt

# Replace all occurrences
wordscalpel replace --word "foo" --with "bar" --all --file input.txt --output out.txt

# Replace 1st occurrence
wordscalpel replace --word "foo" --with "bar" --n 1 --file input.txt --output out.txt

# Count occurrences
wordscalpel count --word "error" --file input.txt

# Show character positions
wordscalpel positions --word "error" --file input.txt

# Case-insensitive
wordscalpel count --word "error" --file input.txt --ignore-case

# Pipe from stdin
echo "hello world hello" | wordscalpel remove --word "hello" --n 1

Error Handling

from wordscalpel.exceptions import OccurrenceNotFoundError, InvalidRangeError, EmptyInputError

try:
    ws.remove_occurrence("hello world", "hello", 5)
except OccurrenceNotFoundError as e:
    print(e)  # Occurrence 5 of 'hello' not found. Total occurrences found: 1

try:
    ws.remove_occurrence_range("a a a", "a", 2, 10)
except InvalidRangeError as e:
    print(e)  # Range [2, 10] is invalid. Total occurrences found: 3

try:
    ws.count_occurrences("", "word")
except EmptyInputError as e:
    print(e)  # The text is empty.

API Reference

Function Description
count_occurrences(text, word, case_sensitive) Count word occurrences
get_positions(text, word, case_sensitive) Get (start, end) spans
remove_occurrence(text, word, n, case_sensitive) Remove nth occurrence
remove_occurrence_range(text, word, start, end, case_sensitive) Remove range
remove_all_occurrences(text, word, case_sensitive) Remove all
replace_occurrence(text, word, replacement, n, case_sensitive) Replace nth
replace_all_occurrences(text, word, replacement, case_sensitive) Replace all
process_file(in, out, word, n, operation, ...) File-level operations
file_count_occurrences(path, word, case_sensitive) Count in file
file_get_positions(path, word, case_sensitive) Positions in file

Running Tests

pip install pytest
pytest tests/ -v

Running Benchmarks

python benchmarks/benchmark.py

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wordscalpel-1.0.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wordscalpel-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file wordscalpel-1.0.0.tar.gz.

File metadata

  • Download URL: wordscalpel-1.0.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for wordscalpel-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e8284f4b987d06fa34edad0555457f12e98065873d1c332bd335ba1b8407e110
MD5 df6f3f1f9ce4e4f2cbcf547273701542
BLAKE2b-256 c69ff8f442810c28a69a4e2197a1ae95aebf75ca998eeaf391f5ad303df91541

See more details on using hashes here.

File details

Details for the file wordscalpel-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: wordscalpel-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for wordscalpel-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16673b87be4d369c4ad76f51ad5ce2290110d423032431e4689eabe641b67553
MD5 31623805187305d36211eb743989139a
BLAKE2b-256 3fd745d92e20cf19627f1f002e786799767f65c911a01bb73afd4e5fb0ca270d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page