# ratanakchhay/khmerns-go architecture

> Architecture diagram of the GitHub repository ratanakchhay/khmerns-go, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ratanakchhay/khmerns-go
- Repository: https://github.com/ratanakchhay/khmerns-go

## Overview

This is a small Go binding for the Khmer Neural Segmenter, not a standalone segmentation engine. The principal workflow is a Go caller creating a context and submitting text; the binding crosses a C-compatible wrapper into the native segmenter and returns tokens to Go. The Go layer also frees native token memory after copying results. The native engine is an external dependency, so its internal stages are not shown. The repository excerpts establish the wrapper calls, but do not expose the engine implementation.

Main entry points: [`clib/wrapper.cpp`](https://github.com/ratanakchhay/khmerns-go/blob/main/clib/wrapper.cpp) (C wrapper), [`clib/clib.go`](https://github.com/ratanakchhay/khmerns-go/blob/main/clib/clib.go) (Context and segmentation), [`example.use.go`](https://github.com/ratanakchhay/khmerns-go/blob/main/example.use.go) (Usage example)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_go["Go API"]
  node_example["Usage example<br/>[example.use.go]"]
  node_binding["Context and segmentation<br/>[clib.go]"]
end

subgraph group_native["Native bridge"]
  node_bridge["C wrapper<br/>[wrapper.cpp]"]
  node_abi["C declarations<br/>[wrapper.h]"]
end

subgraph group_engine["External engine"]
  node_segmenter["Khmer segmenter"]
end

node_caller(("Go caller"))

node_caller -->|"runs"| node_example
node_example -->|"calls"| node_binding
node_binding -->|"calls"| node_bridge
node_bridge -->|"initializes and segments"| node_segmenter
node_binding -->|"frees tokens"| node_bridge
node_bridge -->|"uses native context"| node_segmenter

click node_example "https://github.com/ratanakchhay/khmerns-go/blob/main/example.use.go"
click node_binding "https://github.com/ratanakchhay/khmerns-go/blob/main/clib/clib.go"
click node_bridge "https://github.com/ratanakchhay/khmerns-go/blob/main/clib/wrapper.cpp"
click node_abi "https://github.com/ratanakchhay/khmerns-go/blob/main/clib/wrapper.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_example,node_binding toneBlue
class node_bridge,node_abi toneAmber
class node_segmenter toneMint
class node_caller toneIndigo
```

## Components

### Go API

- [Usage example](https://github.com/ratanakchhay/khmerns-go/blob/main/example.use.go): `example.use.go`
- [Context and segmentation](https://github.com/ratanakchhay/khmerns-go/blob/main/clib/clib.go): `clib/clib.go`

### Native bridge

- [C wrapper](https://github.com/ratanakchhay/khmerns-go/blob/main/clib/wrapper.cpp): `clib/wrapper.cpp`
- [C declarations](https://github.com/ratanakchhay/khmerns-go/blob/main/clib/wrapper.h): `clib/wrapper.h`

### External engine

- **Khmer segmenter**

### Other components

- **Go caller**

## Connections

- Go caller → Usage example: runs (evidence: `example.use.go`)
- Usage example → Context and segmentation: calls (evidence: `example.use.go`)
- Context and segmentation → C wrapper: calls (evidence: `clib/clib.go`)
- C wrapper → Khmer segmenter: initializes and segments (evidence: `clib/wrapper.cpp`)
- Context and segmentation → C wrapper: frees tokens (evidence: `clib/clib.go`)
- C wrapper → Khmer segmenter: uses native context (evidence: `clib/wrapper.cpp`)
