kotoshu

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: BSD-2-Clause Imports: 10 Imported by: 0

README

kotoshu-go

Go client for the Kotoshu HTTP spell-check API.

Install

go get github.com/kotoshu/kotoshu-go

Quick start

import kotoshu "github.com/kotoshu/kotoshu-go"

ctx := context.Background()
client, _ := kotoshu.New("http://localhost:9292")

// Check a document
result, _ := client.Check(ctx, "helo wrold", &kotoshu.CheckOptions{Language: "en"})
for _, e := range result.Errors {
    fmt.Println(e.Word, "->", topThree(e.Suggestions))
}

// Suggestions for one word
sugs, _ := client.Suggest(ctx, "helo", &kotoshu.SuggestOptions{Language: "en", Max: 3})

// Language detection
det, _ := client.Detect(ctx, "bonjour le monde")
fmt.Println(det.Language, det.Confidence)

// Quick predicate
ok, _ := client.Correct(ctx, "hello", &kotoshu.CheckOptions{Language: "en"})

Reference

New(baseURL string, opts ...) (*Client, error)

Options:

  • WithLanguage("en") — default language for all calls
  • WithTimeout(30 * time.Second) — HTTP timeout
  • WithHTTPClient(client) — inject a custom *http.Client
Methods (all take context.Context first)
Method Returns
Health(ctx) *Health, error
Languages(ctx) []string, error
Check(ctx, text, *CheckOptions) *DocumentResult, error
Suggest(ctx, word, *SuggestOptions) []Suggestion, error
Detect(ctx, text) *Detection, error
Correct(ctx, word, *CheckOptions) bool, error
Errors

Server errors are returned as *kotoshu.APIError with .Code and .Message. kotoshu.IsResourceNotSetup(err) checks for the 422 case.

License

BSD-2-Clause, same as Kotoshu.

Documentation

Overview

Package kotoshu is a Go client for the Kotoshu HTTP spell-check API.

Index

Constants

View Source
const Version = "0.1.0"

Variables

This section is empty.

Functions

func IsResourceNotSetup

func IsResourceNotSetup(err error) bool

IsResourceNotSetup reports whether err is a 422 resource_not_setup.

Types

type APIError

type APIError struct {
	Code    string
	Message string
}

APIError wraps a server error response.

func (*APIError) Error

func (e *APIError) Error() string

type CheckOptions

type CheckOptions struct {
	Language string
	Format   string // "full" or "errors"
}

CheckOptions controls a single /v1/check call.

type Client

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

Client is the Kotoshu HTTP API client.

func New

func New(baseURL string, opts ...Option) (*Client, error)

New constructs a client.

func (*Client) Check

func (c *Client) Check(ctx context.Context, text string, opts *CheckOptions) (*DocumentResult, error)

Check posts text to /v1/check.

func (*Client) Correct

func (c *Client) Correct(ctx context.Context, word string, opts *CheckOptions) (bool, error)

Correct returns true if the word is in the dictionary.

func (*Client) Detect

func (c *Client) Detect(ctx context.Context, text string) (*Detection, error)

Detect posts text to /v1/detect.

func (*Client) Health

func (c *Client) Health(ctx context.Context) (*Health, error)

Health probes /v1/health.

func (*Client) Languages

func (c *Client) Languages(ctx context.Context) ([]string, error)

Languages returns cached languages.

func (*Client) Suggest

func (c *Client) Suggest(ctx context.Context, word string, opts *SuggestOptions) ([]Suggestion, error)

Suggest posts word to /v1/suggest.

type Detection

type Detection struct {
	Language   string  `json:"language"`
	Confidence float64 `json:"confidence"`
}

Detection is the response from /v1/detect.

type DocumentResult

type DocumentResult struct {
	File      string      `json:"file"`
	WordCount int         `json:"word_count"`
	Errors    []WordError `json:"errors"`
}

DocumentResult is the response from /v1/check.

type Health

type Health struct {
	Status    string          `json:"status"`
	Ready     map[string]bool `json:"ready"`
	Timestamp string          `json:"timestamp,omitempty"`
}

Health is the response from /v1/health.

type Languages

type Languages struct {
	Cached []string `json:"cached"`
}

Languages is the response from /v1/languages.

type Option

type Option func(*Client)

Option configures a Client.

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient supplies a custom *http.Client.

func WithLanguage

func WithLanguage(lang string) Option

WithLanguage sets the default language.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the HTTP timeout.

type SuggestOptions

type SuggestOptions struct {
	Language string
	Max      int
}

SuggestOptions controls a single /v1/suggest call.

type Suggestion

type Suggestion struct {
	Word       string  `json:"word"`
	Distance   int     `json:"distance"`
	Confidence float64 `json:"confidence"`
	Source     string  `json:"source"`
}

Suggestion is a single correction candidate.

type WordError

type WordError struct {
	Word        string       `json:"word"`
	Position    *int         `json:"position"`
	Suggestions []Suggestion `json:"suggestions"`
}

WordError is one misspelled word with its candidates.

Jump to

Keyboard shortcuts

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