kavfixnel / words

A golang library to interface with system dictionaries and word lists

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Words

A golang library to interface with system dictionaries and word lists.

Go Reference

Word of caution

Right now the system has only been tested with MacOS, though according to The Unix word dictionary this library should also function on other unix systems.

Usage

There are 2 main functionalities in this library:

  1. Parsing and loading the system's word lists into memory
package main

import (
    "fmt"

    "github.com/kavfixnel/words"
)

func main() {
    // Get a map[string]struct{} object of all words known by the system
    wordMap, err := words.NewWordMap(nil)
    if err != nil {
        panic(err)
    }

    fmt.Println(len(wordMap))

    // Get a []string of all known system words by the system
    wordList, err := words.NewWordList(nil)
    if err != nil {
        panic(err)
    }

    fmt.Println(len(wordList))
}
~/examples/words ❯ go run main.go
235976
235976
  1. Check if words are valid and known by the system
package main

import (
    "fmt"

    "github.com/kavfixnel/words"
)

func main() {
    mysteryWord := "abracadabra"
    isValid, err := words.IsValidWord(mysteryWord, nil)
    if err != nil {
        panic(err)
    }

    modifier := ""
    if !isValid {
        modifier = " not"
    }

    fmt.Printf("%s is%s a valid word\n", mysteryWord, modifier)

    // Ability to check variations of words
    mysteryWord = "ÄbraCADabra"
	isValid, err = words.IsValidWord(mysteryWord, &words.IsValidWordOptions{
		IgnoreCase:       true,
		IgnoreDiacritics: true,
	})
    if err != nil {
        panic(err)
    }

    modifier = ""
    if !isValid {
        modifier = " not"
    }

    fmt.Printf("%s is%s a valid word\n", mysteryWord, modifier)
}
~/examples/words ❯ go run main.go   
abracadabra is a valid word
ÄbraCADabra is a valid word

About

A golang library to interface with system dictionaries and word lists

License:Apache License 2.0


Languages

Language:Go 97.9%Language:Makefile 2.1%