Documentation
¶
Overview ¶
Package ichkil provides Arabic Tashkeel (diacritization): it assigns harakat, shadda and tanwin to bare Arabic text.
The engine is a tiny, self-contained ONNX model (≈5 MB, CPU-only) that is downloaded from Hugging Face on first use and cached locally; afterwards everything works offline.
Quick start:
// One-liner — downloads + caches the model on first use. got, err := ichkil.Diacritize(ctx, "محمد قرأ الكتاب") // got == "مُحَمَّدٌ قَرَأَ الْكِتَابِ"
Or, for explicit control:
d, err := ichkil.New(ctx) // default repository
defer d.Close()
got, err := d.Diacritize("الكتاب على الطاولة")
d, err := ichkil.NewFromFiles("model.onnx") // fully offline
A Diacritizer is safe for concurrent use. Close releases the underlying ONNX Runtime session; it must not be called concurrently with other methods.
Index ¶
- Constants
- Variables
- func Argmax(values []float32) int
- func Attach(base string, labels []int) string
- func ComboToLabel(combo string) int
- func Decode(base string, logits []float32, numLabels int) string
- func Diacritize(ctx context.Context, text string) (string, error)
- func Encode(text string, sym2ID map[string]int, unkID int) []int
- func IsArabicLetter(r rune) bool
- func LabelToCombo(label int) string
- func PredictLabels(ctx context.Context, text string) ([]int, error)
- func ResetDefault()
- func RuntimeVersion() string
- func SHA256OfFile(path string) (string, error)
- func SetDefault(d *Diacritizer)
- func StripDiacritics(text string) string
- func VerifyChecksum(path, expected string) error
- type Config
- type Diacritizer
- func (d *Diacritizer) Close() error
- func (d *Diacritizer) Diacritize(text string) (string, error)
- func (d *Diacritizer) MaxSeqLen() int
- func (d *Diacritizer) NumLabels() int
- func (d *Diacritizer) PredictLabels(text string) ([]int, error)
- func (d *Diacritizer) RepoID() string
- func (d *Diacritizer) Revision() string
- func (d *Diacritizer) String() string
- type ModelBundle
- type Option
- func WithBaseURL(baseURL string) Option
- func WithCacheDir(dir string) Option
- func WithConfigPath(path string) Option
- func WithNumThreads(n int) Option
- func WithRepoID(repoID string) Option
- func WithRevision(revision string) Option
- func WithToken(token string) Option
- func WithoutChecksumVerification() Option
Constants ¶
const ( // DefaultRepoID is the Hugging Face model repository. DefaultRepoID = "ichkil/ichkil" // DefaultRevision is the branch fetched by default. DefaultRevision = "main" )
Defaults for model resolution.
const ( // LabelNonLetter is the label for non-letter tokens (spaces, punctuation) // or padding. LabelNonLetter = 0 // LabelNone is the label for a letter that carries no diacritic. LabelNone = 1 // NumLabels is the number of distinct labels the model can emit. NumLabels = 13 )
Label constants.
const Version = "1.0.0"
Version is the semantic version of the ichkil Go library.
Variables ¶
var ( // ErrChecksumMismatch is returned when the SHA-256 of model.onnx does not // match the expected digest (from SHA256SUMS or config.sha256). ErrChecksumMismatch = errors.New("ichkil: model checksum mismatch") // ErrModelLoad is returned when the model artifact or its config is // missing, invalid or unreadable (including ONNX Runtime environment // initialization failures). ErrModelLoad = errors.New("ichkil: model load error") // ErrInference is returned when an ONNX Runtime inference fails. ErrInference = errors.New("ichkil: inference error") )
Sentinel errors returned by the ichkil package. Use errors.Is to test for them, e.g. errors.Is(err, ichkil.ErrChecksumMismatch).
Functions ¶
func Argmax ¶
Argmax returns the index of the maximum value (first one wins ties), matching ONNX Runtime.
func Attach ¶
Attach appends predicted diacritics onto the bare letters of base. labels is one entry per character of base (a shorter slice leaves the remaining characters undecorated). Non-letter characters pass through unchanged.
func ComboToLabel ¶
ComboToLabel maps a diacritic combination (or "") to its label id (0 if unknown).
func Decode ¶
Decode attaches predicted diacritics onto base from flat logits. logits is the model output row for base in row-major order (len(logits) == len(base) * numLabels). Non-letter characters pass through unchanged.
func Diacritize ¶
Diacritize vocalizes bare Arabic text using a shared default Diacritizer (created lazily on first use — it downloads the model once, then works offline).
func IsArabicLetter ¶
IsArabicLetter reports whether r is a single Arabic letter (U+0621..U+064A).
func LabelToCombo ¶
LabelToCombo maps a label id to its diacritic combination ("" for unknown ids).
func PredictLabels ¶
PredictLabels returns per-character labels for text using a shared default Diacritizer.
func ResetDefault ¶
func ResetDefault()
ResetDefault discards the shared default Diacritizer (if any), forcing a fresh model resolution + session on the next convenience call.
func RuntimeVersion ¶
func RuntimeVersion() string
RuntimeVersion returns the version of the loaded ONNX Runtime shared library (empty string when it is not available).
func SHA256OfFile ¶
SHA256OfFile returns the hex SHA-256 digest of the file at path.
func SetDefault ¶
func SetDefault(d *Diacritizer)
SetDefault installs d as the shared default Diacritizer used by Diacritize and PredictLabels.
func StripDiacritics ¶
StripDiacritics returns text with all diacritics and tatweel removed. Letters, spaces and punctuation are preserved so the result stays aligned with the original character positions.
func VerifyChecksum ¶
VerifyChecksum returns an error wrapping ErrChecksumMismatch if the file's SHA-256 differs from expected (compared case-insensitively).
Types ¶
type Config ¶
type Config struct {
// Sym2ID maps every vocabulary symbol to its token id.
Sym2ID map[string]int `json:"sym2id"`
// NumTags is the number of output labels (13).
NumTags int `json:"num_tags"`
// MaxSeqLen is the maximum sequence length of the model (1024).
MaxSeqLen int `json:"max_seq_len"`
// SHA256 is the optional expected digest of model.onnx (fallback when the
// repository has no SHA256SUMS file).
SHA256 string `json:"sha256"`
}
Config is the runtime configuration loaded from the model's config.json.
func LoadConfig ¶
LoadConfig loads and minimally validates a config.json.
type Diacritizer ¶
type Diacritizer struct {
// contains filtered or unexported fields
}
Diacritizer is a ready-to-use Arabic diacritizer backed by ONNX Runtime.
func New ¶
func New(ctx context.Context, opts ...Option) (*Diacritizer, error)
New creates a Diacritizer by resolving the model from Hugging Face (cached after the first download) and opening an ONNX Runtime session.
func NewFromFiles ¶
func NewFromFiles(modelPath string, opts ...Option) (*Diacritizer, error)
NewFromFiles creates a Diacritizer from local model.onnx + config.json files (fully offline). The config defaults to config.json next to the model; use WithConfigPath to point elsewhere.
func (*Diacritizer) Close ¶
func (d *Diacritizer) Close() error
Close releases the ONNX Runtime session. The diacritizer must not be used after Close; Close is idempotent. The shared ONNX Runtime environment (process-wide) is left intact.
func (*Diacritizer) Diacritize ¶
func (d *Diacritizer) Diacritize(text string) (string, error)
Diacritize turns bare (or already vocalized) Arabic into fully-vocalized text. Diacritics are stripped first, so the result is canonical and idempotent; non-Arabic characters pass through unchanged.
Diacritize("محمد قرأ الكتاب") -> "مُحَمَّدٌ قَرَأَ الْكِتَابِ"
func (*Diacritizer) MaxSeqLen ¶
func (d *Diacritizer) MaxSeqLen() int
MaxSeqLen returns the model's maximum sequence length (1024).
func (*Diacritizer) NumLabels ¶
func (d *Diacritizer) NumLabels() int
NumLabels returns the number of distinct labels (13).
func (*Diacritizer) PredictLabels ¶
func (d *Diacritizer) PredictLabels(text string) ([]int, error)
PredictLabels returns the predicted label id (0..NumLabels-1) for every character of text (diacritics stripped first). Empty input yields a nil slice.
func (*Diacritizer) RepoID ¶
func (d *Diacritizer) RepoID() string
RepoID returns the Hugging Face repository the model came from ("<local>" for NewFromFiles).
func (*Diacritizer) Revision ¶
func (d *Diacritizer) Revision() string
Revision returns the branch / tag / commit (or model file name) the model came from.
func (*Diacritizer) String ¶
func (d *Diacritizer) String() string
type ModelBundle ¶
type ModelBundle struct {
// OnnxPath is the absolute path to the local model.onnx.
OnnxPath string
// Config is the parsed runtime configuration.
Config Config
// RepoID is the Hugging Face repository the bundle came from.
RepoID string
// Revision is the branch / tag / commit the bundle was fetched from.
Revision string
}
ModelBundle is a resolved, verified local model artifact plus its config.
func ResolveModel ¶
func ResolveModel(ctx context.Context, cfg *config) (ModelBundle, error)
ResolveModel downloads (or reuses the cached) model files from Hugging Face: model.onnx, config.json and — when checksum verification is enabled — SHA256SUMS. Everything is cached under $ICHKIL_CACHE_DIR (or ~/.cache/ichkil by default), so only the first call per revision touches the network.
type Option ¶
type Option func(*config)
Option customizes model resolution and the ONNX Runtime session.
func WithBaseURL ¶
WithBaseURL sets the Hugging Face base URL (default: $HF_ENDPOINT or https://huggingface.co).
func WithCacheDir ¶
WithCacheDir sets the model cache directory (default $ICHKIL_CACHE_DIR or ~/.cache/ichkil).
func WithConfigPath ¶
WithConfigPath sets the config.json used by NewFromFiles (default: config.json next to the model).
func WithNumThreads ¶
WithNumThreads sets the ONNX Runtime intra-op thread count (0 = ORT heuristic, the default).
func WithRepoID ¶
WithRepoID sets the Hugging Face model repository (default "ichkil/ichkil").
func WithRevision ¶
WithRevision sets the branch, tag or commit to fetch (default "main").
func WithToken ¶
WithToken sets the Hugging Face token used for gated / private repositories (default: $HF_TOKEN).
func WithoutChecksumVerification ¶
func WithoutChecksumVerification() Option
WithoutChecksumVerification disables the SHA-256 verification of model.onnx against SHA256SUMS (enabled by default).
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
ichkil
command
Command ichkil is a small command-line interface to the ichkil Arabic Tashkeel (diacritization) engine.
|
Command ichkil is a small command-line interface to the ichkil Arabic Tashkeel (diacritization) engine. |
|
examples
|
|
|
basic
command
Command basic demonstrates the minimal ichkil usage.
|
Command basic demonstrates the minimal ichkil usage. |