word-tally
Tallies the number of times each word appears in one or more Unicode input sources using ICU4X for word boundary detection. Use word-tally as a command-line tool or WordTally via the Rust library interface.
Unless an I/O mode is specified, a reasonable strategy is automatically selected based on input type:
- Files: Parallel memory-mapped I/O for seekable files
- Pipes, sockets, stdin, other: Parallel streaming I/O for data streams
- Character/block devices: Sequential streaming I/O for device compatibility
All parallel modes use SIMD-accelerated chunk boundary detection. Memory mapping requires seekable file descriptors and won't work with stdin, pipes or devices.
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: auto] [possible values: auto, parallel-mmap, parallel-in-memory, parallel-stream, stream]
-c, --case <FORMAT> Case normalization [default: original] [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
-n, --min-count <COUNT> Exclude words appearing fewer than min times
-w, --exclude-words <WORDS> Exclude words from a comma-delimited list
-i, --include <PATTERNS> Include only words matching a regex pattern
-x, --exclude <PATTERNS> Exclude words matching a regex pattern
-f, --format <FORMAT> Output format [default: text] [possible values: text, json, csv]
-d, --field-delimiter <VALUE> Delimiter between field and value [default: " "] (text format only)
-D, --entry-delimiter <VALUE> Delimiter between entries [default: "\n"] (text format only)
-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 strategies
Choose an I/O strategy based on your performance and memory requirements:
# Default: Auto-selection of a reasonable I/O strategy depending on input type
|
# Sequential streamed I/O with minimal memory usage
# Parallel streamed I/O
# Parallel memory-mapped I/O
# Parallel fully loaded into memory
Additional features:
# Process multiple files
# Mix stdin and files
|
Note: Memory mapping (parallel-mmap) requires seekable files and cannot be used with stdin or pipes.
Output formats
Text (default)
# Write to file instead of stdout
# Custom delimiter between word and count
# Custom delimiter between entries (e.g., comma-separated)
# Pipe to other tools
|
Custom delimiters
# Tab-separated values without escaping
# Custom delimiters
CSV
# CSV with proper escaping and 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
# Convert to lowercase
# 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 " "
#>> entry-delimiter "\n"
#>> case original
#>> order desc
#>> io parallel-stream
#>> min-chars none
#>> min-count none
#>> exclude-words none
#>> exclude-patterns none
#>> include-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: parallel-stream, options: stream, parallel-stream, parallel-in-memory, parallel-mmap)
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, and a balanced 32:1 is used as default for better performance (default: 32)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)
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 failure64: Command line usage error65: Data format error66: Input not found70: Internal software error73: Output creation failed74: I/O error77: Permission denied
Library usage
[]
= "0.28.0"
use HashMap;
use ;
use Result;
The library provides full control over case normalization, sorting, filtering, I/O strategies, and output formats.
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