kbbi

    Unofficial KBBI (Kamus Besar Bahasa Indonesia) lookup for MoonBit: a library, a CLI, and an agent skill.

    kbbi
    indonesian
    dictionary
    cli
    skill
    agent
    Download zip
    Author
    Version
    0.4.1
    License
    Apache-2.0
    Last updated
    14 days ago
    Downloads
    222

    #gaato/kbbi

    Look up Indonesian words in KBBI Daring, the online edition of Kamus Besar Bahasa Indonesia, from MoonBit or from a shell. An entry comes back as typed data, or as JSON from the command: headwords with homograph numbers, pronunciations, root words and nonstandard spellings, and each sense's labels, definition, examples and example meanings. An agent skill for the command is included.

    Unofficial, and not affiliated with KBBI or its operator. Results are read from KBBI Daring's pages.

    #Install

    The command needs no install: MoonBit's moonx downloads the latest release and runs it, as below. For the library:

    moon add gaato/kbbi moon add gaato/mttp

    #Try it from a shell

    moonx gaato/kbbi/cmd/kbbi makan moonx gaato/kbbi/cmd/kbbi rumah sakit moonx gaato/kbbi/cmd/kbbi --help

    The arguments form one query, joined with spaces, so the second line looks up the phrase rumah sakit. A lookup prints one pretty-printed JSON document.

    Exit codeMeaning
    0Success, including a word KBBI does not have (found: false)
    1A network, status or decoding failure
    2Invalid arguments

    Errors go to stderr as error: .... Requests time out after 10 seconds. Please avoid rapid bulk lookups.

    An optional moonrun policy limits the command's network access to KBBI. Save this as kbbi-policy.json:

    {"net":{"connect":["kbbi.kemendikdasmen.go.id:443"]}}

    moonx --experimental-policy kbbi-policy.json gaato/kbbi/cmd/kbbi gemas

    #Use the library

    lookup takes a Client from gaato/mttp_core, so the library imports no async runtime and builds for wasm, native and js. An executable adds gaato/mttp, whose @mttp.Client is the same type, and builds one with @mttp.client(), as run_lookup does below. The caller owns and closes the client, and its timeout, retry and body-limit settings apply to lookups. Reuse one client for several queries.

    ///|
    /// Prints the headword and first definition of each entry for `word`.
    async fn print_definitions(client : @mttp.Client, word : String) -> Unit {
    let result = @kbbi.lookup(client, word)
    if !result.found {
    println("\{word}: not in KBBI")
    }
    for entry in result.entries {
    println("\{entry.headword}: \{entry.senses[0].gloss}")
    }
    }

    ///|
    /// Builds a client, looks `word` up and closes the client, on mttp's event
    /// loop; an executable's `fn main` can call it.
    fn run_lookup(word : String) -> Unit {
    @mttp.run(async fn() {
    let client = @mttp.client(timeout_ms=10_000)
    defer client.close()
    print_definitions(client, word)
    })
    }

    ///|
    test "the usage example compiles" {
    ignore(print_definitions)
    ignore(run_lookup)
    }

    A word KBBI does not have is not an error: the result has found: false and no entries. to_json() converts a result to the same JSON the command prints.

    #Parse a saved page

    parse reads an entry page without any I/O, and entry_url gives the page to save. Use it for tests, or to reread an entry without another request:

    ///|
    test "parse a saved page" {
    let html =
    #|<h2>ma.kan<sup>1</sup></h2>
    #|<ol><li><font color="red"><i><span title="Verba: kata kerja">v</span></i></font>
    #|mengisap:<font color="grey"><i>-- candu</i></font></li></ol>
    let url = @kbbi.entry_url("makan")
    let result = @kbbi.parse(html, query="makan", url~)
    inspect(url, content="https://kbbi.kemendikdasmen.go.id/entri/makan")
    inspect(result.entries[0].headword, content="makan")
    inspect(result.entries[0].senses[0].gloss, content="mengisap")
    }

    #Errors

    lookup raises KbbiError:

    • EmptyQuery: the query is blank after trimming. Nothing is sent.
    • Http: sending or reading failed. It carries the TransportError.
    • Status: KBBI answered with a non-2xx status.
    • Decode: the page is not valid UTF-8.

    Errors raised by the client's own policies pass through unchanged.

    #Packages

    PackageWhat it isTargets
    gaato/kbbiThe library: lookup, parse, entry_url and the result typeswasm, native, js
    gaato/kbbi/cmd/kbbiThe kbbi command and its agent skillwasm, native

    The agent skill describes how to run lookups and read the JSON.

    #License

    Apache-2.0

    KbbiError

    pub suberror KbbiError {
    EmptyQuery
    Http(
    TransportError
    )
    Status(Int)
    Decode(String)
    } derive(
    Debug
    )

    Why lookup failed: the query was empty after trimming, the transport failed, the site answered with a non-2xx status, or the body was not valid UTF-8.

    KbbiError::to_repr

    Renders a KbbiError for debugging with the derived Debug.

    Entry

    pub struct Entry {
    headword : String
    homograph : Int?
    pronunciation : String?
    root_words : Array[String]
    nonstandard_forms : Array[String]
    senses : Array[Sense]
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    One headword on an entry page, with its senses in page order.

    headword has KBBI's syllable dots removed; homograph is the superscript number that tells homographs apart, and pronunciation the syllable spelling KBBI gives for some words. root_words lists the words a derived form is built from.

    Entry::equal

    fn Entry::equal(Entry, Entry) -> Bool

    Compares two Entry values field by field with the derived Eq.

    Entry::not_equal

    fn Entry::not_equal(x : Entry, y : Entry) -> Bool

    Compares two Entry values field by field with the derived Eq.

    Entry::to_json

    Converts a Entry to JSON with the derived ToJson.

    Entry::to_repr

    Renders a Entry for debugging with the derived Debug.

    Example

    pub struct Example {
    text : String
    meaning : String?
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    An example phrase for a sense. In text, -- stands for the headword; meaning holds the explanation KBBI gives for an idiomatic example.

    Example::equal

    fn Example::equal(Example, Example) -> Bool

    Compares two Example values field by field with the derived Eq.

    Example::not_equal

    fn Example::not_equal(x : Example, y : Example) -> Bool

    Compares two Example values field by field with the derived Eq.

    Example::to_json

    Converts a Example to JSON with the derived ToJson.

    Example::to_repr

    Renders a Example for debugging with the derived Debug.

    Label

    pub struct Label {
    code : String
    name : String
    description : String
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    A word-class or usage label on a sense, such as v for a verb.

    code is the abbreviation shown on the page, name the label's full name (the code when KBBI gives none), and description the explanation after the name, or empty when there is none.

    Label::equal

    fn Label::equal(Label, Label) -> Bool

    Compares two Label values field by field with the derived Eq.

    Label::not_equal

    fn Label::not_equal(x : Label, y : Label) -> Bool

    Compares two Label values field by field with the derived Eq.

    Label::to_json

    Converts a Label to JSON with the derived ToJson.

    Label::to_repr

    Renders a Label for debugging with the derived Debug.

    LookupResult

    pub struct LookupResult {
    found : Bool
    query : String
    url : String
    entries : Array[Entry]
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    The outcome of one lookup. found is true when entries is not empty; query is the trimmed query and url the entry page that was read.

    LookupResult::equal

    Compares two LookupResult values field by field with the derived Eq.

    LookupResult::not_equal

    fn LookupResult::not_equal(x : LookupResult, y : LookupResult) -> Bool

    Compares two LookupResult values field by field with the derived Eq.

    LookupResult::to_json

    Converts a LookupResult to JSON with the derived ToJson.

    LookupResult::to_repr

    Renders a LookupResult for debugging with the derived Debug.

    Sense

    pub struct Sense {
    labels : Array[Label]
    gloss : String
    examples : Array[Example]
    } derive(Eq,
    Debug
    ,
    ToJson
    )

    One numbered definition of an entry: its word-class and usage labels, the definition text in gloss, and the example phrases that follow it.

    Sense::equal

    fn Sense::equal(Sense, Sense) -> Bool

    Compares two Sense values field by field with the derived Eq.

    Sense::not_equal

    fn Sense::not_equal(x : Sense, y : Sense) -> Bool

    Compares two Sense values field by field with the derived Eq.

    Sense::to_json

    Converts a Sense to JSON with the derived ToJson.

    Sense::to_repr

    Renders a Sense for debugging with the derived Debug.

    DEFAULT_BASE_URL

    let DEFAULT_BASE_URL : String

    The KBBI Daring site that lookup and entry_url use unless a base_url is given.

    entry_url

    fn entry_url(query : String, base_url? : String) -> String

    The entry page URL for query: the query is trimmed, lowercased and percent-encoded as one path segment under <base_url>/entri/. A trailing / on base_url is ignored.

    lookup

    async fn lookup(client :
    Client
    , query : String, base_url? : String) -> LookupResult

    Looks up query on KBBI Daring: trims it, sends a GET request for its entry_url through client, and parses the page with parse. A word KBBI does not have gives found: false, not an error.

    Raises EmptyQuery for a blank query (before any request), Http when the transport fails, Status for a non-2xx response, and Decode when the body is not valid UTF-8. Other client policy errors propagate unchanged. The caller owns the client; its policies and body limits apply to the call.

    parse

    fn parse(html : StringView, query~ : String, url~ : String) -> LookupResult

    Parses an entry page's HTML without any IO. query is trimmed and, with url, copied into the result. Each <h2> headword followed by a list of senses becomes an Entry; a "not found" page or HTML that cannot be parsed gives found: false. Never raises.