stardict

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

go-stardict

Go Reference codecov Tests Go Report Card

A stardict dictionary library for Go.

Status

The API is currently unstable and will change. This package will use module version numbering to manage versions and compatibility.

Features

  • [x] Reading dictionary metadata (.ifo files).
  • [x] Reading & searching the dictionary index (.idx file).
  • [x] Reading full dictionary articles.
  • [x] Efficient access for large files.
  • [x] Dictzip support.
  • [x] Capitalization, diacritic, punctuation, and whitespace folding (#19, #25).
  • [x] Synonym support (.syn file) (#2).
  • [x] Glob/Wildcard search support (#21).
  • [ ] Support for tree dictionaries (.tdx file) (#3).
  • [ ] Support for Resource Storage (res/ directory) (#4).
  • [ ] Support for collation files (.idx.clt, .syn.clt) (#7)
  • [ ] Support for offset cache files (.idx.oft, .syn.oft) (#8)

Installation

To install this package run

go get github.com/ianlewis/go-stardict

Examples

Searching a Dictionary

You can search a stardict dictionary directly and list the entries.

// Open dictonaries in a directory
dictionaries, _ := stardict.OpenAll(".")

// Search the dictionaries.
for _, d := range dictionaries {
  entries, _ := d.Search("banana")
  for _, e := range entries {
    // Print out matching index entries.
    fmt.Println(e)
  }
}
  • ilius/go-stardict - Basic stardict library. Support for .ifo,.dict,.idx, and .syn. Some dictzip support.
  • dyatlov/gostardict - Basic stardict library. Only .ifo,.dict,.ifo support. No dictzip support.
  • rongyi/stardict - A nice terminal UI tool with some basic dictionary reading support. Only .ifo,.dict,.ifo support. No dictzip random-access support.
  • wenerme/tools/pkg/stardict - Stardict tools. Only .ifo,.dict,.ifo support. No dictzip support.
  • 1-st/stardict - Very bare-bones. Only .ifo,.dict,.ifo support. No dictzip support.

References

Documentation

Overview

Package stardict implements a library for reading stardict dictionaries in pure Go.

Stardict dictionaries contain several files:

  1. An .ifo file that contains metadata about the dictionary.
  2. An .idx file that contains the dictionary index. It contains search entries and associated offsets into the .dict file. The index file can be compressed using gzip.
  3. A .dict file that contains the dictionary's main article data. The dict file can be compressed using the dictzip format.
  4. An optional .syn file that contains synonyms which link index entries.
  5. A .tdx file is present for "tree dictionaries" and is used instead of the .idx file.

More info on on the dictionary format can be found at this URL: https://github.com/huzheng001/stardict-3/blob/master/dict/doc/StarDictFileFormat

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataList

type DataList []*dict.Data

DataList is entry's data.

func (DataList) String

func (l DataList) String() string

String returns a string representation of the data list.

type Entry

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

Entry is a dictionary entry.

func (*Entry) Data

func (e *Entry) Data() DataList

Data returns the entry's data entries.

func (*Entry) String

func (e *Entry) String() string

String returns a string representation of the Entry.

func (*Entry) Title

func (e *Entry) Title() string

Title return the entry's title.

type Options added in v0.2.0

type Options struct {
	// Folder returns a [transform.Transformer] that performs folding (e.g.
	// case folding, whitespace folding, etc.) on dictionary entries.
	Folder func() transform.Transformer
}

Options are options for the Stardict dictionary.

type Stardict

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

Stardict is a stardict dictionary.

func Open

func Open(path string, options *Options) (*Stardict, error)

Open opens a Stardict dictionary from the given .ifo file path.

func OpenAll

func OpenAll(path string, options *Options) ([]*Stardict, []error)

OpenAll opens all dictionaries under a directory. This function will return all successfully opened dictionaries along with any errors that occurred.

func (*Stardict) Author

func (s *Stardict) Author() string

Author returns the dictionary author. This field is optional for dictionaries.

func (*Stardict) Bookname

func (s *Stardict) Bookname() string

Bookname returns the dictionary name.

func (*Stardict) Close

func (s *Stardict) Close() error

Close closes the dict and any underlying readers.

func (*Stardict) Description

func (s *Stardict) Description() string

Description returns the dictionary description. This field is optional for dictionaries. <br> has been replaced with '\n'.

func (*Stardict) Dict

func (s *Stardict) Dict() (*dict.Dict, error)

Dict returns the dictionary's dict.

func (*Stardict) Email

func (s *Stardict) Email() string

Email returns the dictionary contact email.

func (*Stardict) Index

func (s *Stardict) Index() (*idx.Idx, error)

Index returns a simple in-memory version of the dictionary's index.

func (*Stardict) IndexScanner

func (s *Stardict) IndexScanner() (*idx.Scanner, error)

IndexScanner returns a new index scanner. The caller assumes ownership of the underlying reader so Close should be called on the scanner when finished.

func (*Stardict) Search

func (s *Stardict) Search(query string) ([]*Entry, error)

Search performs a simple full text search of the dictionary for the given query and returns dictionary entries. The query supports glob patterns whose pattern syntax is:

pattern:
    { term }

term:
    `*`         matches any sequence of non-separator characters
    `**`        matches any sequence of characters
    `?`         matches any single non-separator character
    `[` [ `!` ] { character-range } `]`
                character class (must be non-empty)
    `{` pattern-list `}`
                pattern alternatives
    c           matches character c (c != `*`, `**`, `?`, `\`, `[`, `{`, `}`)
    `\` c       matches character c

character-range:
    c           matches character c (c != `\\`, `-`, `]`)
    `\` c       matches character c
    lo `-` hi   matches character c for lo <= c <= hi

pattern-list:
    pattern { `,` pattern }
                comma-separated (without spaces) patterns

The pattern is folded using the given folding transformer and matches the folded word in the index.

func (*Stardict) Syn added in v0.2.0

func (s *Stardict) Syn() (*syn.Syn, error)

Syn returns a simple in-memory version of the dictionary's synonym index.

func (*Stardict) SynWordCount added in v0.2.0

func (s *Stardict) SynWordCount() int64

SynWordCount returns the wordcount in the synonym index.

func (*Stardict) Version

func (s *Stardict) Version() string

Version returns the dictionary format version.

func (*Stardict) Website

func (s *Stardict) Website() string

Website returns the dictionary website url.

func (*Stardict) WordCount

func (s *Stardict) WordCount() int64

WordCount returns the dictionary word count.

Directories

Path Synopsis
cmd
Package dict implements reading .dict files.
Package dict implements reading .dict files.
Package idx implements reading .idx files.
Package idx implements reading .idx files.
Package ifo implements reading .ifo files.
Package ifo implements reading .ifo files.
internal

Jump to

Keyboard shortcuts

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