text-processing-utils/bin/spellcheck-cli.ts
2026-01-21 11:37:27 -08:00

93 lines
No EOL
2.5 KiB
TypeScript
Executable file

#!/usr/bin/env tsx
import { SpellChecker } from '../src/spellcheck/spell-checker';
async function main() {
const args = process.argv.slice(2);
// Parse options
let autoCorrect = false;
let text = '';
for (let i = 0; i < args.length; i++) {
if (args[i] === '--fix' || args[i] === '-f') {
autoCorrect = true;
} else if (args[i] === '--help' || args[i] === '-h') {
console.log(`
Usage: spellcheck-cli [options] <text>
Options:
-f, --fix Auto-correct the text
-h, --help Show this help message
Examples:
spellcheck-cli "Check this text"
spellcheck-cli --fix "Fix teh typos"
echo "Fix this text" | spellcheck-cli --fix
`);
process.exit(0);
} else {
text = args.slice(i).join(' ');
break;
}
}
// If no text provided as args, read from stdin
if (!text) {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
text = Buffer.concat(chunks).toString();
}
if (!text.trim()) {
console.error('No text provided. Use --help for usage information.');
process.exit(1);
}
// Create spellchecker with options
const spellChecker = new SpellChecker({
autoCorrect,
customWords: ['claude', 'md', 'cuwu', 'api', 'cli', 'npm', 'tsx', 'workspace', 'uwuapps'],
caseSensitive: false,
dictionaries: ['english', 'technical'],
confidenceThresholds: {
autoFix: 0.60, // Lower threshold for CLI to fix more typos
suggest: 0.40,
possible: 0.20
}
});
// Initialize the spellchecker
await spellChecker.initialize();
// Add specific typo patterns for common mistakes
spellChecker.addSplitWordPattern('ist he', 'is the', 0.95);
spellChecker.addWord('legacy', 'english');
spellChecker.addWord('banned', 'english');
if (autoCorrect) {
const fixed = await spellChecker.fix(text);
console.log(fixed);
} else {
// Check mode - show word-by-word results
const words = text.split(/\s+/);
const results = await Promise.all(words.map(word => spellChecker.check(word)));
const errors = results.filter(r => !r.correct);
if (errors.length === 0) {
console.log('No spelling errors found.');
} else {
console.log('Spelling errors found:');
errors.forEach(error => {
console.log(` "${error.word}" - suggestions: ${error.suggestions.join(', ') || 'none'}`);
});
}
}
}
main().catch(error => {
console.error('Error:', error.message);
process.exit(1);
});