If you wish to test a textual content file for typos, Linux will help.
It has a few instruments and numerous instructions that may level out the errors together with aspell and enchant, and I’ll share a script that I put collectively lately that appears for typos utilizing the system’s phrases file.
Utilizing aspell
aspell could be very intelligent device that may level out typos and make it surprisingly straightforward to repair them. When used to make adjustments to a single file, it reverses the textual content and background colours to focus on misspelled phrases. You’d begin it with a command like this:
$ aspell test myfile
If aspell detects no typos, it merely exits. In any other case, it can open with a show that accommodates the file textual content (or simply the highest traces relying on the size of the file) adopted by a listing of prompt substitute phrases and, beneath that, a listing of the instructions you could run. The primary typo (or suspected typo) will probably be displayed with the textual content and background colours reversed as proven beneath.
I want that I might sort with my eyes closed and by no means make a mistake. I do not
like typoze and I feel I run into them way more typically than I need.
1) depose 6) typo <===== prompt phrases
2) typos 7) topaz
3) typo's 8) topees
4) varieties 9) sort's
5) sort 0) typed
i) Ignore I) Ignore all <===== instructions
r) Substitute R) Substitute all
a) Add l) Add Decrease
b) Abort x) Exit
?
If you wish to exchange the typo with one of many phrases listed, simply use your keyboard to sort the digit to the left of the phrase you need to choose. If it’s the one typo within the file, aspell will make the change and exit. In any other case, it can transfer on to the subsequent misspelled phrase.
You may also exchange a typo by typing “r” after which typing the phrase you need to use to interchange it. If it’s a phrase that’s more likely to be repeated, you may press “R” as an alternative and exchange all cases of the phrase within the file. You may also resolve to disregard what aspell deems a typo. In spite of everything, it may be a time period that aspell merely doesn’t acknowledge or an acronym. You are able to do this one occasion at a time by typing “i” or as a bunch by typing “I”.
As a precaution, aspell creates a backup file (e.g., myfile.bak) of the file you’re checking to be able to recuperate your typos should you discover it vital and restore phrases you might need modified in error.
You may also use aspell to test the spelling of a bunch of phrases. Sort “aspell -a” as proven beneath and you’ll sort a phrase and see the checklist of prompt replacements. If aspell responds with an asterisk (*), the phrase was spelled appropriately.
$ aspell -a @(#) Worldwide Ispell Model 3.1.20 (however actually Aspell 0.60.8) typoze & typoze 17 0: depose, typos, typo's, varieties, sort, typo, topaz, topees, sort's, typed, tapes, topee, Topsy, doze, pose, tape, tape's
typos
*
^C
Sort ^C to exit as proven above.
Utilizing enchant
A device named “enchant” will checklist the phrases it considers typos with a command like this:
$ enchant -l myfile typoze typoze
Should you anticipate lots of typos, you should utilize a command just like the one beneath to let you know what number of occasions every typo seems within the file:
$ enchant -l myfile | uniq -c 2 typoze
To view the prompt replacements, run a command like this:
$ enchant -a myfile | grep : & typoze 1 5: typo & typoze 1 6: typo
Constructing a spell-checking script
I put a bash script collectively to see how properly I might test the phrases in a file towards the Linux phrases file (/usr/share/dict/phrases on my system). The duty turned out to be slightly trickier than I anticipated.
I run the script like this:
$ findTypos myfile typoze typoze
The script accommodates a sequence of instructions to search out and show a listing of the misspelled phrases. The primary group of instructions test to see {that a} filename has been offered as an argument. If not, it prompts for one.
#!/bin/bash if [ $# == 0 ]; then echo -n "file: " learn file else file=$1 fi whereas learn -ra line; do for phrase in "${line[@]}"; do phrase=$(echo $phrase | tr '[:upper:]' '[:lower:]') phrase=`echo $phrase | tr -d '[.,?:!"]'` # punct would not work for this phrase=`echo $phrase | sed s/'s//| sed s/'s//` grep ^$phrase$ phrases >/dev/null || echo $phrase finished; finished < $file
The script then runs by way of every phrase within the file and runs a tr command to vary it to all lowercase to keep away from points with capitalized phrases. It then makes use of a second tr command to take away most punctuation marks in order that intervals, query marks, and so on. don’t cling to the phrases that have to be checked. I didn’t use [:punct:] as a result of it could have eliminated the apostrophe in phrases like “isn’t”, however I individually eliminated the possessive “’s” on the ends of phrases. The final step was searching for the phrase within the phrases file. The ^ and $ characters inform grep to search out solely the phrase specified, not phrases that may embody that phrase.
The script, which I name “findTypos”, finds typos, however makes no try to repair them or recommend substitute phrases.
Wrap-Up
Detecting misspelled phrases in your textual content recordsdata might be useful, particularly should you’re making ready your weekly report and your boss is a stickler for grammar. Thankfully, Linux gives numerous methods to assist with this.
Copyright © 2022 IDG Communications, Inc.