spellio

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Spellio

A word processing engine built with trie structure than can finish words during typing and correct typing mistakes.

The project will be enhanced with new features in the future (like prediction of the next word based on the previous one).

Engine tries to return the most probable words, and because of that, it stores information about the frequency of each word. Therefore, adding one word multiple times will increase its stored frequency.

Word adding

You can add words in two ways:

  1. Manually (one-by-one)
  2. Read from file
Usage

Currently, there are two main mods available:

1. Word completion

Engine will try to complete word based on the prefix that was inputted. Possible words will be sorted by their frequency in the engine.

Example:

fmt.Printf("hospi...: %v\n", engine.CompleteWord("hospi", 5))
// hospi...: [hospital hospitality hospitably hospitable]
2. Word correction

Engine will correct inputted text based on given keyboard layout. Expected number of mistakes is len(word) / 3. Possible words will be sorted by number of mistaken letters and by their frequency in the engine.

Example:

fmt.Printf("housr?: %v\n", engine.CorrectWord("housr", layouts.English, 5))
// `housr?: [house hoist]`

Documentation

Overview

Package spellio provides a trie structure for storing words and their frequencies and later spell checking and correction.

The trie is optimized for fast lookup and traversal. It is also optimized for space efficiency.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

An Engine is a trie structure that stores information about words and their frequencies.

Zero value is a valid empty Engine.

func (*Engine) CompleteWord

func (e *Engine) CompleteWord(prefix string, limit int) []*Word

CompleteWord returns a list of words that start with the given prefix.

The list is sorted by frequency in descending order and can be limited by the given limit.

func (*Engine) CorrectWord

func (e *Engine) CorrectWord(rawWord string, layout KeyboardLayout, limit int) []*Word

CorrectWord returns a list of words that are similar to the given word.

The list is sorted by the number of changes in ascending order and their frequency in descending order. The list can be limited by the given limit parameter.

The number of allowed changes is calculated as the length of the word divided by the 3.

func (*Engine) CountWords

func (e *Engine) CountWords() int

CountWords returns the number of words in the dictionary.

func (*Engine) FindWord

func (e *Engine) FindWord(word string) (*Word, bool)

FindWord will try to find a word in the Engine.

If the word is found, the Word and true are returned. If the word is not found, an empty Word and false are returned.

func (*Engine) GetNearbyWords

func (e *Engine) GetNearbyWords(rawWord string, maxChanges int, layout KeyboardLayout) []NearbyWord

GetNearbyWords returns all words in the dictionary that are near the given word and can be transformed into it with the given number of changes.

func (*Engine) GetWordsByPrefix

func (e *Engine) GetWordsByPrefix(prefix string) []*Word

GetWordsByPrefix returns all words in the dictionary that start with the given prefix.

func (*Engine) Insert

func (e *Engine) Insert(word string)

Insert adds a word to the Engine. If the word already exists, its frequency is incremented.

The word is converted to lowercase before being inserted.

func (*Engine) InsertFromText

func (e *Engine) InsertFromText(reader io.Reader)

InsertFromText reads words from a reader and inserts them into the Engine.

Words will be separated by a space character, cleaned of non-alphabetic characters and converted to lowercase before being inserted.

func (*Engine) OutputAllWords

func (e *Engine) OutputAllWords(writer io.Writer) error

OutputAllWords will write all words in the Engine to a writer.

Each word will be on its own line, with its frequency in parentheses.

type KeyboardLayout added in v0.2.0

type KeyboardLayout map[rune][]rune

A KeyboardLayout is a map of characters to characters that are nearby on the keyboard and are considered as possible replacements.

type NearbyWord added in v0.2.0

type NearbyWord struct {
	*Word
	Changes int
}

A NearbyWord contains the word and the number of changes required to transform it into the original word.

type Word

type Word struct {
	Freq uint // Frequency of the word in the dictionary.
	// contains filtered or unexported fields
}

A Word is a word in the dictionary.

func (*Word) String

func (w *Word) String() string

String returns the word as a string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL