Golang : Spell checking with ispell example
Chinese New Year for 2017 celebration just over and now it is time to get back to work. Here is a simple example on how to wrap ispell
program in Golang. ispell
is a command line utility that can be used to check if a word is spelled correctly and if not, it will give a list of suggestions.
Pretty straightforward explanation can be found in the code below. This version should work correctly for International Ispell Version 3.4.00 8 Feb 2015
.
Here you go!
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
func handleError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func ispellWrapper(inputWord string) string {
cmd := exec.Command("ispell")
stdin, err := cmd.StdinPipe()
handleError(err)
stdin.Write([]byte(inputWord + "\n"))
stdin.Close()
data, err := cmd.Output()
if err != nil {
panic(err)
}
// skip the credit line and get the remainder text
interestData := data[52:]
// check for "word: ok"
test := string(interestData[:8])
if (strings.Contains(test, "word: ok")) || (strings.Contains(test, "word: how")) {
//fmt.Println("Word ok")
return "Word ok"
} else {
// remove the "how about:" and the new line prompt "word: "
// and return the suggestions from ispell
test1 := interestData[17 : len(interestData)-7]
trimmed := strings.TrimSpace(string(test1))
return trimmed
}
}
func main() {
// spell check some words, if nothing is wrong with the given word
// you will get "Word ok" else you will get suggestions from ispell
fmt.Println(ispellWrapper("hello"))
fmt.Println(ispellWrapper("stinge"))
fmt.Println(ispellWrapper("wattee"))
}
Output:
Word ok
singe, sting, stinger, stings, stingy, tinge
wattle
Happy coding!
References:
https://www.socketloop.com/tutorials/trim-white-spaces-string-golang
https://www.socketloop.com/tutorials/golang-how-to-pipe-input-data-to-executing-child-process
See also : Golang : Convert word to its plural form example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+23.7k Golang : Upload to S3 with official aws-sdk-go package
+12k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+6.4k Golang : How to validate ISBN?
+34.7k Golang : Upload and download file to/from AWS S3
+37.7k Golang : Read a text file and replace certain words
+21.7k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+20.8k Golang : Sort and reverse sort a slice of strings
+7.9k Golang : Tell color name with OpenCV example
+9.5k Golang : ffmpeg with os/exec.Command() returns non-zero status
+4.6k Which content-type(MIME type) to use for JSON data
+17.5k Golang : How to make a file read only and set it to writable again?