Go SDK
Installing
The best part of ODict being written in Go is that calling it from Go is super easy.
To use ODict in your project, simply add it via go get
:
go get github.com/TheOpenDictionary/odict@latest
Most of the options you see referenced in method calls below are the same options available in the CLI.
Reading & writing dictionaries
import "github.com/TheOpenDictionary/odict/lib/core"
// Write a dictionary from an XML string
core.WriteDictionaryFromXML("<dictionary></dictionary>", "dictionary.odict")
// Read a dictionary into memory and write it to a new path
dict := core.ReadDictionary("dictionary.odict")
core.WriteDictionaryFromExisting("new.odict", dict)
// Compile a dictionary from an XML file
core.CompileDictionary("dictionary.xml", "dictionary.odict")
Performing lookups
import (
"github.com/TheOpenDictionary/odict/lib/core"
"github.com/TheOpenDictionary/odict/lib/types"
)
// Read a dictionary into memory
dict := core.ReadDictionary("dictionary.odict")
// Look up entries
var entries []types.Entry = core.Lookup(
core.LookupRequest{
Dictionary: dict,
Queries: []string{"dog", "cat"},
Split: 2,
Follow: false,
},
)
Indexing & searching
import (
"github.com/TheOpenDictionary/odict/lib/search"
"github.com/TheOpenDictionary/odict/lib/types"
)
dict := core.ReadDictionary("dictionary.odict")
# Perform a full-text index of the dictionary
search.Index(
search.IndexRequest{
Dictionary: dict,
Overwrite: true,
Quiet: true,
},
)
# Perform a full-text search
var entries []types.Entry = search.SearchDictionary(
// Here we need to use string() seeing Id() is a buffer object from Flatbuffers
string(dict.Id()),
// Our search term
"run",
// This sets exact=false
false
)
Splitting entries
import (
"github.com/TheOpenDictionary/odict/lib/core"
"github.com/TheOpenDictionary/odict/lib/types"
)
var entries []types.Entry = core.Split(
SplitRequest{
Dictionary: dict1,
Query: "hotdog",
Threshold: 3,
},
)
Merging dictionaries
import (
"github.com/TheOpenDictionary/odict/lib/core"
"github.com/TheOpenDictionary/odict/lib/types"
)
dict1 := core.ReadDictionary("dictionary1.odict")
dict2 := core.ReadDictionary("dictionary2.odict")
var merged types.DictionaryRepresentable = MergeDictionaries(dict1, dict2)
Listing all headwords
import "github.com/TheOpenDictionary/odict/lib/core"
var lexicon []string = core.Lexicon("dictionary1.odict")