word-tally
Tallies the number of times each word appears in one or more unicode input sources. Use word-tally as a command-line tool or WordTally via the Rust library interface. I/O is streamed by default. Memory-mapped and fully-buffered-in-memory I/O modes can be selected to optimize for different file sizes and workloads. Optionally take advantage of multiple CPU cores through parallelization of sorting, memory-mapped file I/O and word counting. Memory-mapping is only supported for files with seekable file descriptors, but performs well in parallel mode.
Parallel streaming and parallel buffered I/O modes use SIMD for quick chunk boundary detection. Parallel memory-mapped I/O mode slices chunk boundaries more directly and is often the most efficient choice for large files. Sequential streaming mode uses the least peak memory and is the default.
Usage
Usage: word-tally [OPTIONS] [PATHS]...
Arguments:
[PATHS]... File paths to use as input (use "-" for stdin) [default: -]
Options:
-I, --io <STRATEGY> I/O strategy [default: streamed] [possible values: mmap, streamed, buffered]
-p, --parallel Use threads for parallel processing
-c, --case <FORMAT> Case normalization [default: lower] [possible values: original, upper, lower]
-s, --sort <ORDER> Sort order [default: desc] [possible values: desc, asc, unsorted]
-m, --min-chars <COUNT> Exclude words containing fewer than min chars
-M, --min-count <COUNT> Exclude words appearing fewer than min times
-E, --exclude-words <WORDS> Exclude words from a comma-delimited list
-i, --include <PATTERN> Include only words matching a regex pattern
-x, --exclude <PATTERN> Exclude words matching a regex pattern
-f, --format <FORMAT> Output format [default: text] [possible values: text, json, csv]
-d, --delimiter <VALUE> Delimiter between keys and values [default: " "]
-o, --output <PATH> Write output to file rather than stdout
-v, --verbose Print verbose details
-h, --help Print help (see more with '--help')
-V, --version Print version
Installation
Examples
I/O & parallelization
word-tally uses sequential, streamed processing by default to reduce memory usage. Parallel processing is also available for memory-mapped, streamed and fully-buffered I/O. The different modes have different performance and resource usage characteristics.
# Streamed I/O from stdin (`--io=streamed` is default)
|
# Streamed I/O from a file
# Process multiple files with aggregated word counts
# Mix stdin and files
|
# Memory-mapped I/O with efficient parallel processing (requires a file rather than stdin)
# Buffered I/O with parallel processing
# Streamed I/O with parallel processing
The --io=mmap memory-mapped processing mode only works with files and cannot be used with piped input. Parallel processing with memory mapping can be very efficient but mmap requires a file with a seekable file descriptor.
Output formats
Text (default)
# Write to file instead of stdout
# Custom delimiter between word and count
# Pipe to other tools
|
CSV
# Using a comma delimiter (unescaped without headers)
# Using proper CSV format (escaped with headers)
JSON
Visualization
Convert JSON output for visualization with d3-cloud:
|
Format and pipe the JSON output to the wordcloud_cli to produce an image:
| |
Case normalization
# Default lowercase normalization
# Preserve original case
# Convert all to uppercase
Sorting options
# Sort by frequency (descending, default)
# Sort alphabetically (ascending)
# No sorting (sorted by order seen)
Filtering words
# Only include words that appear at least 10 times
# Exclude words with fewer than 5 characters
# Exclude words by pattern
# Combining include and exclude patterns
# Exclude specific words
Verbose output
|
#>> source -
#>> total-words 6
#>> unique-words 3
#>> delimiter " "
#>> case lower
#>> order desc
#>> processing sequential
#>> io streamed
#>> min-chars none
#>> min-count none
#>> exclude-words none
#>> exclude-patterns none
#>>
#>> fo 3
#>> fi 2
#>> fe 1
Environment variables
The following environment variables configure various aspects of the library:
I/O and processing strategy configuration:
WORD_TALLY_IO- I/O strategy (default: streamed, options: streamed, buffered, memory-mapped)WORD_TALLY_PROCESSING- Processing strategy (default: sequential, options: sequential, parallel)
Memory allocation and performance:
WORD_TALLY_UNIQUENESS_RATIO- Ratio of total words to unique words for capacity estimation. Higher values allocate less initial memory. Books tend to have a 10:1 ratio, but a more conservative 256:1 is used as default to reduce unnecessary memory overhead (default: 256)WORD_TALLY_WORDS_PER_KB- Estimated words per KB of text for capacity calculation (default: 128, max: 512)WORD_TALLY_STDIN_BUFFER_SIZE- Buffer size for stdin when size cannot be determined (default: 262144)WORD_TALLY_DEFAULT_CAPACITY- Default initial capacity when there is no size hint (default: 1024)WORD_TALLY_WORD_DENSITY- Multiplier for estimating unique words per chunk (default: 15)
Parallel processing configuration:
WORD_TALLY_THREADS- Number of threads for parallel processing (default: all available cores)WORD_TALLY_CHUNK_SIZE- Size of chunks for parallel processing in bytes (default: 65536)
Exit codes
word-tally uses standard unix exit codes to indicate success or the types of failure:
0: Success1: General error64: Command line usage error65: Data format error66: Cannot open input69: Service unavailable74: I/O error
Library usage
[]
= "0.25.0"
use File;
use ;
The library supports case normalization, sorting, filtering and I/O and processing strategies.
Stability notice
Pre-release level stability: This is prerelease software. Expect breaking interface changes at MINOR version (0.x.0) bumps until a stable release.
Tests & benchmarks
Tests
Clone the repository.
Run all tests.
Run specific test modules.
Run individual tests
cargo test --test filters_tests -- test_min_chars
cargo test --test io_tests -- test_memory_mapped
Benchmarks
Run all benchmarks.
Run specific benchmark groups
Run specific individual benchmarks