### Install whatlanggo with go get Source: https://github.com/abadojack/whatlanggo/blob/master/README.md Install the whatlanggo package using the go get command. Requires Go 1.8 or higher. ```sh go get -u github.com/abadojack/whatlanggo ``` -------------------------------- ### Install whatlanggo with go get Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Install the whatlanggo module using the Go package manager. This command fetches the module from the repository. ```sh go get -u github.com/abadojack/whatlanggo ``` -------------------------------- ### Default configuration example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/configuration.md Calls DetectWithOptions with an empty Options struct, using default behavior (no filtering). ```go info := whatlanggo.DetectWithOptions("Foje funkcias", whatlanggo.Options{}) ``` -------------------------------- ### DetectWithOptions whitelist example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Example of using DetectWithOptions with a whitelist to allow only Esperanto and Ukrainian, then detecting a phrase. ```go options := whatlanggo.Options{ Whitelist: map[whatlanggo.Lang]bool{ whatlanggo.Epo: true, whatlanggo.Ukr: true, }, } info := whatlanggo.DetectWithOptions("Mi ne scias", options) fmt.Println(info.Lang.String()) // "Esperanto" ``` -------------------------------- ### DetectLang basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Example of calling DetectLang with a French phrase and printing the language name. ```go lang := whatlanggo.DetectLang("Vouloir, c'est pouvoir") fmt.Println(lang.String()) // "French" ``` -------------------------------- ### Simple language detection example Source: https://github.com/abadojack/whatlanggo/blob/master/README.md Detects the language of a given text using whatlanggo.Detect and prints the language, script, and confidence. The example uses the text 'Foje funkcias kaj foje ne funkcias'. ```go package main import ( "fmt" "github.com/abadojack/whatlanggo" ) func main() { info := whatlanggo.Detect("Foje funkcias kaj foje ne funkcias") fmt.Println("Language:", info.Lang.String(), " Script:", whatlanggo.Scripts[info.Script], " Confidence: ", info.Confidence) } ``` -------------------------------- ### DetectWithOptions blacklist example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Example of using DetectWithOptions with a blacklist to exclude Hebrew and Yiddish, resulting in Lang -1 but script still detected. ```go options := whatlanggo.Options{ Blacklist: map[whatlanggo.Lang]bool{ whatlanggo.Heb: true, whatlanggo.Ydd: true, }, } info := whatlanggo.DetectWithOptions("האקדמיה ללשון העברית", options) fmt.Println(info.Lang, info.Script == nil) // -1 false (Lang -1, script still Hebrew) ``` -------------------------------- ### Blacklisting and whitelisting languages Source: https://github.com/abadojack/whatlanggo/blob/master/README.md Shows how to use whatlanggo.Options to blacklist or whitelist languages when detecting. The example demonstrates blacklisting Ydd and whitelisting Epo and Ukr. ```go package main import ( "fmt" "github.com/abadojack/whatlanggo" ) func main() { //Blacklist options := whatlanggo.Options{ Blacklist: map[whatlanggo.Lang]bool{ whatlanggo.Ydd: true, }, } info := whatlanggo.DetectWithOptions("האקדמיה ללשון העברית", options) fmt.Println("Language:", info.Lang.String(), "Script:", whatlanggo.Scripts[info.Script]) //Whitelist options1 := whatlanggo.Options{ Whitelist: map[whatlanggo.Lang]bool{ whatlanggo.Epo: true, whatlanggo.Ukr: true, }, } info = whatlanggo.DetectWithOptions("Mi ne scias", options1) fmt.Println("Language:", info.Lang.String(), " Script:", whatlanggo.Scripts[info.Script]) } ``` -------------------------------- ### Basic call to DetectScript Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Shows how to call DetectScript and compare the result against a Unicode script constant. The example checks for Cyrillic and prints a message. ```go import "unicode" script := whatlanggo.DetectScript("Привет всем!") if script == unicode.Cyrillic { fmt.Println("Cyrillic script") } ``` -------------------------------- ### Guard against nil script Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Demonstrates handling a nil script returned by DetectScript when no supported script is found. The example checks for nil and prints a fallback message. ```go script := whatlanggo.DetectScript("123456789-=?") if script == nil { fmt.Println("no script found") } else { fmt.Println(whatlanggo.Scripts[script]) } ``` -------------------------------- ### isStopChar basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/utils.md Shows isStopChar returning true for space and digit, false for letter 'a'. ```go fmt.Println(whatlanggo.isStopChar(' '), whatlanggo.isStopChar('0')) // true true fmt.Println(whatlanggo.isStopChar('a')) // false ``` -------------------------------- ### toTrigramChar basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/trigrams.md Shows basic usage: toTrigramChar('.') returns ' ', toTrigramChar('z') returns 'z'. ```go fmt.Println(whatlanggo.toTrigramChar('.')) // ' ' fmt.Println(whatlanggo.toTrigramChar('z')) // 'z' ``` -------------------------------- ### count normalization behavior example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/trigrams.md Shows normalization: '-' and '...' become spaces, and 'e ' and ' i' windows are skipped. ```go fmt.Println(whatlanggo.count("Give - IT...")) // " gi","giv","ive","ve "," it","it " // '-' and '...' became spaces; "e " and " i" windows were skipped ``` -------------------------------- ### Whitelisting languages with precedence Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/options.md Creates Options with a Whitelist containing Esperanto and Ukrainian. Whitelist takes precedence over Blacklist. The example detects Esperanto from the input. ```go options := whatlanggo.Options{ Whitelist: map[whatlanggo.Lang]bool{ whatlanggo.Epo: true, whatlanggo.Ukr: true, }, } lang := whatlanggo.DetectLangWithOptions("Mi ne scias", options) fmt.Println(lang.String()) // "Esperanto" ``` -------------------------------- ### abs basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/utils.md Shows abs returning 65535 for input -65535. ```go fmt.Println(whatlanggo.abs(-65535)) // 65535 ``` -------------------------------- ### Basic detection with default Options Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/options.md Uses an empty Options{} literal to perform default detection. The example detects Esperanto from the input string and prints the language name. ```go info := whatlanggo.DetectWithOptions("La viro amas hundojn", whatlanggo.Options{}) fmt.Println(info.Lang.String()) // "Esperanto" ``` -------------------------------- ### Basic call to Scripts map Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Shows how to retrieve the display name of a detected script using the Scripts map. The example detects the script of a Russian phrase and prints "Cyrillic". ```go info := whatlanggo.Detect("Привет, мир!") fmt.Println(whatlanggo.Scripts[info.Script]) // "Cyrillic" ``` -------------------------------- ### DetectLang unknown language check Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Example of checking if DetectLang returns -1 for an unknown language. ```go lang := whatlanggo.DetectLang("123456789-=?") if lang == -1 { fmt.Println("no language could be identified") } ``` -------------------------------- ### DetectLangWithOptions basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Calls DetectLangWithOptions with an Options{} value on a Ukrainian text. Prints the ISO 639-3 code 'ukr' (test-verified). ```go lang := whatlanggo.DetectLangWithOptions("Та нічого, все нормально. А в тебе як?", whatlanggo.Options{}) fmt.Println(lang.Iso6393()) // "ukr" (test-verified, detect_test.go) ``` -------------------------------- ### Unrecognizable input behavior example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Shows that DetectWithOptions on text matching no profile returns Lang -1 and Confidence 0, triggering the score1 == 0 path. ```go // Text matching no profile at all => score1 == 0 path => Lang -1, Confidence 0 info := whatlanggo.DetectWithOptions("ᬅᬓ᭄ᬱᬭᬯ᭄ᬬᬜ᭄ᬚᬦ", whatlanggo.Options{}) if info.Lang == -1 && info.Confidence == 0 { fmt.Println("unrecognizable input") } ``` -------------------------------- ### Detect language and script in Go Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Import the whatlanggo module and use the Detect function to identify the language and script of a given text. The example prints the language name and script using the Lang.String() method and the Scripts map. ```go package main import ( "fmt" "github.com/abadojack/whatlanggo" ) func main() { info := whatlanggo.Detect("Foje funkcias kaj foje ne funkcias") fmt.Println("Language:", info.Lang.String(), " Script:", whatlanggo.Scripts[info.Script]) } ``` -------------------------------- ### count basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/trigrams.md Basic call from trigrams_test.go: for 'yes', returns counts for ' ye', 'yes', and 'es ' each as 1. ```go got := whatlanggo.count("yes") fmt.Println(got[" ye"], got["yes"], got["es "]) // 1 1 1 ``` -------------------------------- ### DetectLangWithOptions with blacklist example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Uses a blacklist of confusable languages (Jav, Tgl, Swe, Ceb) to avoid misdetection. Prints 'eng' for the given English text. ```go options := whatlanggo.Options{ Blacklist: map[whatlanggo.Lang]bool{ whatlanggo.Jav: true, whatlanggo.Tgl: true, whatlanggo.Swe: true, whatlanggo.Ceb: true, }, } lang := whatlanggo.DetectLangWithOptions("All evil come from a single cause ...", options) fmt.Println(lang.Iso6393()) // "eng" ``` -------------------------------- ### isCyrillic predicate example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Representative of all 23 script predicates. Each takes a rune and returns true if it belongs to the script. This pattern is used for all predicates listed in the table. ```go var isCyrillic = func(r rune) bool { return unicode.Is(unicode.Cyrillic, r) } ``` -------------------------------- ### Detect sentinel for undetected language Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Shows how to recognize the sentinel Info value when no supporting script or language is detected. The example uses Balinese script text, which has no language mapping. ```go info := whatlanggo.Detect("ᬅᬓ᭄ᬱᬭᬯ᭄ᬬᬜ᭄ᬚᬦ") // Balinese script text if info.Lang == -1 && info.Script == nil { fmt.Println("no supporting script/language detected") } ``` -------------------------------- ### Lang.Iso6393 basic call Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Calls Iso6393 on the Spa constant to get its ISO 639-3 code. Prints "spa". ```go fmt.Println(whatlanggo.Spa.Iso6393()) // "spa" ``` -------------------------------- ### Lang.Iso6393 Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Returns the ISO 639-3 code of a Lang as a string. This method is the recommended way to get the ISO 639-3 code. ```APIDOC ## Lang.Iso6393 ### Description Returns the ISO 639-3 code of a `Lang` as a string. ### Method `func (lang Lang) Iso6393() string` ### Parameters - **lang** (Lang) - Required - Receiver, via method syntax `lang.Iso6393()`. ### Returns - **string** - The three-letter ISO 639-3 code, or `""` for unknown values such as `-1`. ### Example ```go fmt.Println(whatlanggo.Spa.Iso6393()) // "spa" ``` ``` -------------------------------- ### getTrigramsWithPositions basic call example Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/trigrams.md Basic call from trigrams_test.go: returns positions where lower means higher frequency. For 'xaaaaabbbbd', pos['aaa'] is 0 and pos['bbb'] is 1. ```go pos := whatlanggo.getTrigramsWithPositions("xaaaaabbbbd") fmt.Println(pos["aaa"]) // 0 — most frequent trigram fmt.Println(pos["bbb"]) // 1 ``` -------------------------------- ### Mixed Han + Hiragana detection Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Demonstrates that DetectScript returns the merged _HiraganaKatakana table for Japanese text containing Han and kana runes, not unicode.Han. The example prints true when comparing to _HiraganaKatakana. ```go script := whatlanggo.DetectScript("中国の文化") // Han + Hiragana mix // DetectScript returns _HiraganaKatakana (unicode.go:5), not unicode.Han, // because kana runes were present (script.go:105-114) fmt.Println(script == _HiraganaKatakana) // true ``` -------------------------------- ### Lang.String basic call Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Calls String on the Yor constant to get the English language name. Prints "Yoruba". ```go fmt.Println(whatlanggo.Yor.String()) // "Yoruba" ``` -------------------------------- ### Define Lang constants with iota Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Declares all 84 language constants using iota, starting with Afr. The constants follow ISO 639-3 codes and are used for language detection. ```go const ( Afr Lang = iota Aka ... Zul ) ``` -------------------------------- ### Restricted configuration with whitelist Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/configuration.md Creates an Options struct with a Whitelist containing Epo and Ukr, then detects language with it. The output is "Esperanto". ```go opts := whatlanggo.Options{ Whitelist: map[whatlanggo.Lang]bool{ whatlanggo.Epo: true, whatlanggo.Ukr: true, }, } info := whatlanggo.DetectWithOptions("Mi ne scias", opts) fmt.Println(info.Lang.String()) // "Esperanto" ``` -------------------------------- ### Reading Info fields after detection Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/info.md Shows how to access the fields of an Info instance returned by whatlanggo.Detect. Prints the language name, script, and confidence. ```go info := whatlanggo.Detect("Además de todo lo anteriormente dicho, también encontramos...") fmt.Println(info.Lang.String()) // "Spanish" fmt.Println(whatlanggo.Scripts[info.Script]) // "Latin" fmt.Println(info.Confidence) // 1 ``` -------------------------------- ### Building a custom classifier from predicates Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Iterates over runes and uses isArabic to detect Arabic characters, printing each match. ```go samples := []rune{'ا', 'ب', 'ج'} for _, r := range samples { if whatlanggo.isArabic(r) { fmt.Println("arabic rune:", r) } } ``` -------------------------------- ### CodeToLang Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Gets the Lang enum constant by its ISO 639-3 code as a lowercase string. Unknown codes are allowed and return -1. ```APIDOC ## CodeToLang ### Description Gets the `Lang` enum constant by its ISO 639-3 code as a lowercase string. Unknown codes are allowed and return -1. ### Method Function call ### Endpoint `func CodeToLang(code string) Lang` ### Parameters - **code** (string) - Required - ISO 639-3 code, e.g. `"eng"`. Unknown codes are allowed. ### Returns - **Lang** - The matching constant, or `-1` when `code` is not in the map. ### Throws/Rejects None. ### Example ```go lang := whatlanggo.CodeToLang("deu") fmt.Println(lang.String()) // "German" ``` ### Example (unknown code) ```go lang := whatlanggo.CodeToLang("xxx") if lang == -1 { fmt.Println("unknown ISO 639-3 code") } ``` ``` -------------------------------- ### DetectWithOptions(text string, options Options) Info Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Detects the language of the given text using the provided options and returns an Info struct with detection results. ```APIDOC ## DetectWithOptions(text string, options Options) Info ### Description Detects the language of the given text using the provided options and returns an Info struct with detection results. ### Method Function ### Signature `DetectWithOptions(text string, options Options) Info` ### Parameters - **text** (string) - Required - The text to detect language for. - **options** (Options) - Required - Configuration options for detection (e.g., whitelist/blacklist). ### Returns - **Info** - The detection result containing language, script, and confidence. ### Example ```go info := whatlanggo.DetectWithOptions("Bonjour le monde", whatlanggo.Options{Whitelist: map[whatlanggo.Lang]bool{whatlanggo.Fra: true}}) ``` ``` -------------------------------- ### Guard every result with explicit checks Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/errors.md Because whatlanggo has no exceptions, guard every result explicitly. Check for Lang == -1 to handle cases 1-4 (unsupported/no script, language unknown for script). Also check IsReliable() for low-confidence results (confidence <= 0.8). ```go info := whatlanggo.DetectWithOptions(text, opts) if info.Lang == -1 { // cases 1, 2, 3 or 4 — no language could be identified if info.Script == nil { fmt.Println("unsupported/no script") // case 1 } else { fmt.Println("language unknown for script") // case 2 or 3 } return } if !info.IsReliable() { // confidence <= 0.8 — ambiguous text, treat as low-confidence result } ``` -------------------------------- ### Options Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/types.md Filtering options for language detection, passed to DetectWithOptions and DetectLangWithOptions. Allows whitelisting or blacklisting languages. ```APIDOC ## Options ### Description Filtering options for language detection, passed to DetectWithOptions and DetectLangWithOptions. Allows whitelisting or blacklisting languages. ### Fields - **Whitelist** (map[Lang]bool) - Optional - Only whitelisted languages considered when non-empty - **Blacklist** (map[Lang]bool) - Optional - Languages to skip; ignored while Whitelist is non-empty ### Used By - `DetectWithOptions` (parameter) - `DetectLangWithOptions` (parameter) ``` -------------------------------- ### DetectWithOptions Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Detects the language and script of the given text with the provided options (whitelist/blacklist filtering). Returns an Info struct containing language, script, and confidence. ```APIDOC ## DetectWithOptions ### Description Detects the language and script of the given text with the provided options (whitelist/blacklist filtering). Returns an Info struct containing language, script, and confidence. ### Method func DetectWithOptions(text string, options Options) Info ### Parameters - **text** (string) - Required - The text to analyze. - **options** (Options) - Required - Struct with `Whitelist map[Lang]bool` and `Blacklist map[Lang]bool` fields; pass `Options{}` for unfiltered detection. ### Returns `Info` — see info.md. When `DetectScript` returns `nil`, the function returns `Info{Lang: -1, Script: nil, Confidence: 0}` immediately; otherwise `Info{Lang, Script, Confidence}` is assembled from `detectLangBaseOnScript`. ### Example (whitelisting) ```go options := whatlanggo.Options{ Whitelist: map[whatlanggo.Lang]bool{ whatlanggo.Epo: true, whatlanggo.Ukr: true, }, } info := whatlanggo.DetectWithOptions("Mi ne scias", options) fmt.Println(info.Lang.String()) // "Esperanto" ``` ### Example (blacklisting) ```go options := whatlanggo.Options{ Blacklist: map[whatlanggo.Lang]bool{ whatlanggo.Heb: true, whatlanggo.Ydd: true, }, } info := whatlanggo.DetectWithOptions("האקדמיה ללשון העברית", options) fmt.Println(info.Lang, info.Script == nil) // -1 false (Lang -1, script still Hebrew) ``` ``` -------------------------------- ### Lang.String Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Returns the English name of the language. Returns a string representing the language name. ```APIDOC ## Lang.String ### Description Returns the English name of the language. Returns a string representing the language name. ### Method Method ### Signature `func (lang Lang) String() string` ### Parameters None ### Returns - **string** - The English name of the language. ### Example ```go name := lang.String() ``` ``` -------------------------------- ### Options struct definition Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/options.md Defines the Options struct with Whitelist and Blacklist maps. Both fields are optional and default to empty maps. Whitelist takes precedence over Blacklist when non-empty. ```go type Options struct { Whitelist map[Lang]bool Blacklist map[Lang]bool } ``` -------------------------------- ### Basic call to isLatin predicate Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Shows how to call a script predicate directly. Returns true for 'ž' (Latin) and false for 'ж' (Cyrillic). ```go fmt.Println(whatlanggo.isLatin('ž')) // true fmt.Println(whatlanggo.isLatin('ж')) // false ``` -------------------------------- ### Detect Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Detects the language and script of the given text. Returns an Info struct containing the detected language, script, and confidence score. ```APIDOC ## Detect ### Description Detects the language and script of the given text. Returns an Info struct containing the detected language, script, and confidence score. ### Method Function call ### Endpoint `func Detect(text string) Info` ### Parameters - **text** (string) - Required - The text to analyze. Punctuation, digits and whitespace are normalized away by the trigram pipeline; there is no minimum length requirement. ### Returns - **Info** - A struct with fields: `Lang Lang`, `Script *unicode.RangeTable`, `Confidence float64`. ### Throws/Rejects None. If no script is detected, or the script has no language mapping, the result is `Info{Lang: -1, Script: nil, Confidence: 0}`. ### Example ```go import "github.com/abadojack/whatlanggo" info := whatlanggo.Detect("Where there is a will there is a way") fmt.Println(info.Lang.Iso6393()) // "eng" fmt.Println(whatlanggo.Scripts[info.Script]) // "Latin" fmt.Println(info.IsReliable()) // true ``` ### Example - Recognizing the "could not detect" sentinel ```go info := whatlanggo.Detect("ᬅᬓ᭄ᬱᬭᬯ᭄ᬬᬜ᭄ᬚᬦ") // Balinese script text if info.Lang == -1 && info.Script == nil { fmt.Println("no supporting script/language detected") } ``` ``` -------------------------------- ### Info struct definition Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/info.md Defines the Info struct returned by Detect and DetectWithOptions. Contains Lang, Script, and Confidence fields. ```go type Info struct { Lang Lang Script *unicode.RangeTable Confidence float64 } ``` -------------------------------- ### getTrigramsWithPositions function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/trigrams.md Builds the trigram → rank map for a text. Trigrams are counted, sorted by ascending count then alphabetically, and assigned descending positions: most frequent gets 0, next 1, etc. ```go func getTrigramsWithPositions(text string) map[string]int ``` -------------------------------- ### Lang.Iso6391 Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Returns the ISO 639-1 code for the language. Returns a string representing the two-letter language code. ```APIDOC ## Lang.Iso6391 ### Description Returns the ISO 639-1 code for the language. Returns a string representing the two-letter language code. ### Method Method ### Signature `func (lang Lang) Iso6391() string` ### Parameters None ### Returns - **string** - The ISO 639-1 code for the language. ### Example ```go code := lang.Iso6391() ``` ``` -------------------------------- ### Lang.String() string Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Returns the English name of the language for the Lang value. ```APIDOC ## Lang.String() string ### Description Returns the English name of the language for the Lang value. ### Method Method ### Signature `func (self Lang) String() string` ### Parameters None ### Returns - **string** - The English language name, or empty string if unknown. ### Example ```go name := whatlanggo.LangEng.String() // "English" ``` ``` -------------------------------- ### Lang.String round trip name back to code Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Converts a language code to a Lang, gets its English name, then prints the name and ISO 639-3 code. Prints "Hindi hin". ```go lang := whatlanggo.CodeToLang("hin") name := lang.String() // "Hindi" fmt.Println(name, lang.Iso6393()) // "Hindi hin" ``` -------------------------------- ### Detect basic call Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Detects language and script of a string. Returns Info with Lang, Script, and Confidence. No minimum length; punctuation, digits, and whitespace are normalized away. If no script or language mapping is found, returns Info{Lang: -1, Script: nil, Confidence: 0}. ```go import "github.com/abadojack/whatlanggo" info := whatlanggo.Detect("Where there is a will there is a way") fmt.Println(info.Lang.Iso6393()) // "eng" fmt.Println(whatlanggo.Scripts[info.Script]) // "Latin" fmt.Println(info.IsReliable()) // true ``` -------------------------------- ### DetectScript Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Detects the script (writing system) of a given text string. Returns a Script constant representing the detected script. ```APIDOC ## DetectScript ### Description Detects the script (writing system) of a given text string. Returns a Script constant representing the detected script. ### Method Function ### Signature `func DetectScript(text string) Script` ### Parameters - **text** (string) - Required - The input text to detect script for. ### Returns - **Script** - A Script constant representing the detected script. ### Example ```go script := whatlanggo.DetectScript("Foje funkcias kaj foje ne funkcias") ``` ``` -------------------------------- ### Lang.Iso6391() string Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Returns the ISO 639-1 two-letter language code for the Lang value. ```APIDOC ## Lang.Iso6391() string ### Description Returns the ISO 639-1 two-letter language code for the Lang value. ### Method Method ### Signature `func (self Lang) Iso6391() string` ### Parameters None ### Returns - **string** - The ISO 639-1 code, or empty string if unknown. ### Example ```go code := whatlanggo.LangEng.Iso6391() // "en" ``` ``` -------------------------------- ### Check for empty string from lookup functions Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/errors.md Lookup functions fail softly; check for the empty string. CodeToLang returns -1 for unknown codes (case 5). Iso6391 returns empty string for languages without an ISO 639-1 code (e.g., Ceb). Accessing Langs with an unknown key yields empty string (case 6). ```go lang := whatlanggo.CodeToLang("xxx") fmt.Println(lang == -1) // true (case 5) code := whatlanggo.Ceb.Iso6391() fmt.Println(len(code) == 0) // true — Ceb has no ISO 639-1 code; "" is a valid result, not an error name := whatlanggo.Langs[-1] fmt.Println(len(name) == 0) // true — unknown Lang key yields "" (case 6) ``` -------------------------------- ### Iterate all supported language names Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Iterates over `whatlanggo.Langs` to print each language constant and its English name. Useful for listing all 84 supported languages. ```go for lang, name := range whatlanggo.Langs { fmt.Println(lang, name) } ``` -------------------------------- ### Lang.Iso6393 Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Returns the ISO 639-3 code for the language. Returns a string representing the three-letter language code. ```APIDOC ## Lang.Iso6393 ### Description Returns the ISO 639-3 code for the language. Returns a string representing the three-letter language code. ### Method Method ### Signature `func (lang Lang) Iso6393() string` ### Parameters None ### Returns - **string** - The ISO 639-3 code for the language. ### Example ```go code := lang.Iso6393() ``` ``` -------------------------------- ### Lang.Iso6393() string Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Returns the ISO 639-3 three-letter language code for the Lang value. ```APIDOC ## Lang.Iso6393() string ### Description Returns the ISO 639-3 three-letter language code for the Lang value. ### Method Method ### Signature `func (self Lang) Iso6393() string` ### Parameters None ### Returns - **string** - The ISO 639-3 code, or empty string if unknown. ### Example ```go code := whatlanggo.LangEng.Iso6393() // "eng" ``` ``` -------------------------------- ### Scripts Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md A map of Unicode script tables to their display names, used for human-readable output. It contains 24 entries covering major scripts. ```APIDOC ## Scripts ### Description A map of Unicode script tables to their display names, used for human-readable output. It contains 24 entries covering major scripts. ### Variable ```go var Scripts = map[*unicode.RangeTable]string{ unicode.Arabic: "Arabic", // ... unicode.Thai: "Thai", } ``` ### Usage Access the display name for a script using the script pointer returned by `DetectScript` or `Detect`. ### Example ```go info := whatlanggo.Detect("Привет, мир!") fmt.Println(whatlanggo.Scripts[info.Script]) // "Cyrillic" ``` ### Notes - The merged `_HiraganaKatakana` table has no entry of its own; `Scripts` holds separate `unicode.Hiragana` and `unicode.Katakana` rows. - Lookup with an unknown key yields the map default (`""`). ``` -------------------------------- ### Options struct Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/options.md Options represents configuration for language detection, allowing whitelisting or blacklisting languages. It is a plain data struct with no behavior. ```APIDOC ## Options struct ### Description Options represents configuration for language detection, allowing whitelisting or blacklisting languages. It is a plain data struct with no behavior. ### Fields - **Whitelist** (map[Lang]bool) - Optional - Set of `Lang` constants to allow. When non-empty, only whitelisted languages are considered. - **Blacklist** (map[Lang]bool) - Optional - Set of `Lang` constants to skip. Ignored while `Whitelist` is non-empty. ### Usage Both fields are plain maps; membership is tested with `options.Whitelist[lang]` / `options.Blacklist[lang]` lookups, where a missing key reads as `false`. The `Options{}` literal initializes both fields empty. ### Filtering Precedence 1. If `Whitelist` is non-empty → every language not present in `Whitelist` is skipped; `Blacklist` is never consulted. 2. Else if `Blacklist` is non-empty → every language present in `Blacklist` is skipped. 3. Otherwise (both empty) → all languages of the profile map are candidates. ### Example ```go info := whatlanggo.DetectWithOptions("La viro amas hundojn", whatlanggo.Options{}) fmt.Println(info.Lang.String()) // "Esperanto" ``` ``` -------------------------------- ### Blacklisting languages to prevent detection Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/options.md Creates Options with a Blacklist containing Hebrew and Yiddish. When both candidates are removed, detection returns Lang -1 but Script is still reported (Hebrew). ```go options := whatlanggo.Options{ Blacklist: map[whatlanggo.Lang]bool{ whatlanggo.Heb: true, whatlanggo.Ydd: true, }, } info := whatlanggo.DetectWithOptions("האקדמיה ללשון העברית", options) fmt.Println(info.Lang == -1) // true — both candidates removed fmt.Println(info.Script == nil) // false — script still reported (Hebrew) ``` -------------------------------- ### Basic call using IsReliable() Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/constants.md Detects language of a Swedish sentence and prints language and confidence if reliable. Uses the exported Detect function and IsReliable() method. ```go info := whatlanggo.Detect("Det här är en mening") if info.IsReliable() { fmt.Println(info.Lang.String(), info.Confidence) } ``` -------------------------------- ### DetectWithOptions function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Function signature for DetectWithOptions, which detects language and script with whitelist/blacklist filtering. ```go func DetectWithOptions(text string, options Options) Info ``` -------------------------------- ### Compare DetectScript result with _HiraganaKatakana Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/unicode.md Shows that DetectScript returns the merged table for text containing kana, not the raw unicode.Hiragana table. Requires importing the standard unicode package. ```go import "unicode" script := whatlanggo.DetectScript("今日は") fmt.Println(script == whatlanggo._HiraganaKatakana) // true fmt.Println(script == unicode.Hiragana) // false — merged table, not the raw one ``` -------------------------------- ### DetectScript Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Detects the script of a given text and returns a pointer to the corresponding Unicode RangeTable, or nil if no supported script is found. It ignores stop characters and uses a majority-based algorithm with early exit and locality optimization. ```APIDOC ## DetectScript ### Description Detects the script of a given text and returns a pointer to the corresponding Unicode RangeTable, or nil if no supported script is found. It ignores stop characters and uses a majority-based algorithm with early exit and locality optimization. ### Function Signature ```go func DetectScript(text string) *unicode.RangeTable ``` ### Parameters - **text** (string) - Required - Arbitrary text; may be empty or contain only punctuation/digits. ### Returns - **script** (*unicode.RangeTable) - Pointer to a script table (one of the `unicode` constants, or the module-level `_HiraganaKatakana` for Japanese), or `nil` when no supported script is found. ### Example ```go import "unicode" script := whatlanggo.DetectScript("Привет всем!") if script == unicode.Cyrillic { fmt.Println("Cyrillic script") } ``` ### Notes - Stop characters are ignored. - If the max script is `unicode.Han` but any Hiragana/Katakana runes were counted, the result is `_HiraganaKatakana`. - If no script rune matched, returns `nil`. ``` -------------------------------- ### Info Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/types.md Represents the full outcome of language detection, returned by Detect and DetectWithOptions. Contains the detected language, script, and confidence score. ```APIDOC ## Info ### Description Represents the full outcome of language detection, returned by Detect and DetectWithOptions. Contains the detected language, script, and confidence score. ### Fields - **Lang** (Lang) - Required - Detected language constant, or `-1` - **Script** (*unicode.RangeTable) - Required - Detected script table (pointer), or `nil` - **Confidence** (float64) - Required - `0.0`–`1.0`; `> 0.8` means reliable ### Used By - `Detect` (return) - `DetectWithOptions` (return) - `Info.IsReliable()` (receiver) ``` -------------------------------- ### Scripts map definition Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Defines the Scripts map that associates Unicode script tables with display names. Used for human-readable output, e.g., whatlanggo.Scripts[info.Script]. Lookup with an unknown key yields the map default (""). ```go var Scripts = map[*unicode.RangeTable]string{ unicode.Arabic: "Arabic", ... unicode.Thai: "Thai", } ``` -------------------------------- ### DetectLangWithOptions Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Detects only the language of the given text with the provided options. Returns the detected language constant or -1 if no language is detected. ```APIDOC ## DetectLangWithOptions ### Description Detects only the language of the given text with the provided options. ### Method Function call ### Endpoint `func DetectLangWithOptions(text string, options Options) Lang` ### Parameters - **text** (string) - Required - The text to analyze. - **options** (Options) - Required - Whitelist/blacklist options; `Options{}` for unfiltered behavior. ### Returns - **Lang** - The detected language constant, or `-1`. ### Example ```go lang := whatlanggo.DetectLangWithOptions("Та нічого, все нормально. А в тебе як?", whatlanggo.Options{}) fmt.Println(lang.Iso6393()) // "ukr" ``` ### Example with Blacklist ```go options := whatlanggo.Options{ Blacklist: map[whatlanggo.Lang]bool{ whatlanggo.Jav: true, whatlanggo.Tgl: true, whatlanggo.Swe: true, whatlanggo.Ceb: true, }, } lang := whatlanggo.DetectLangWithOptions("All evil come from a single cause ...", options) fmt.Println(lang.Iso6393()) // "eng" ``` ``` -------------------------------- ### LangToString Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Converts a Lang constant to its English name. Returns a string representing the language name. ```APIDOC ## LangToString ### Description Converts a Lang constant to its English name. Returns a string representing the language name. ### Method Function ### Signature `func LangToString(lang Lang) string` ### Parameters - **lang** (Lang) - Required - The Lang constant to convert. ### Returns - **string** - The English name of the language. ### Example ```go name := whatlanggo.LangToString(lang) ``` ``` -------------------------------- ### DetectLangWithOptions function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Declares the DetectLangWithOptions function that detects only the language of the given text with provided options. Returns a Lang constant or -1. Implementation returns DetectWithOptions(text, options).Lang. ```go func DetectLangWithOptions(text string, options Options) Lang ``` -------------------------------- ### abs function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/utils.md Declares abs, a custom absolute value function used by calculateDistance. ```go func abs(n int) int ``` -------------------------------- ### DetectLang Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/detect.md Detects only the language of the given text. Returns a Lang constant or -1 if detection fails. ```APIDOC ## DetectLang ### Description Detects only the language of the given text. Returns a Lang constant or -1 if detection fails. ### Method func DetectLang(text string) Lang ### Parameters - **text** (string) - Required - The text to analyze. ### Returns `Lang` — one of the 84 `Lang` constants, or `-1` when detection fails. ### Example ```go lang := whatlanggo.DetectLang("Vouloir, c'est pouvoir") fmt.Println(lang.String()) // "French" ``` ### Example (unknown language) ```go lang := whatlanggo.DetectLang("123456789-=?") if lang == -1 { fmt.Println("no language could be identified") } ``` ``` -------------------------------- ### CodeToLang unknown code handling Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Handles unknown ISO 639-3 codes by checking for the -1 sentinel. ```go lang := whatlanggo.CodeToLang("xxx") if lang == -1 { fmt.Println("unknown ISO 639-3 code") } ``` -------------------------------- ### Manual threshold comparison Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/constants.md Applies the threshold manually with the same strict > comparison as IsReliable(). Detects Esperanto text and prints 'trusted result' only if confidence exceeds 0.8. ```go const threshold = 0.8 info := whatlanggo.Detect("Mi ne scias") if info.Confidence > threshold { fmt.Println("trusted result") // otherwise: treat as low-confidence; see errors.md for the branches } ``` -------------------------------- ### Filtering text with isStopChar Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/utils.md Filters out stop characters from a string before analysis, keeping only non-stop runes. ```go text := "Hello, 世界!" var clean []rune for _, r := range text { if !whatlanggo.isStopChar(r) { clean = append(clean, r) } } ``` -------------------------------- ### DetectScript(text string) *unicode.RangeTable Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Detects the script (e.g., Latin, Cyrillic) of the given text and returns the corresponding Unicode range table. ```APIDOC ## DetectScript(text string) *unicode.RangeTable ### Description Detects the script (e.g., Latin, Cyrillic) of the given text and returns the corresponding Unicode range table. ### Method Function ### Signature `DetectScript(text string) *unicode.RangeTable` ### Parameters - **text** (string) - Required - The text to detect script for. ### Returns - **\*unicode.RangeTable** - The Unicode range table for the detected script, or nil if unknown. ### Example ```go script := whatlanggo.DetectScript("Hello") ``` ``` -------------------------------- ### Distance-style use of abs Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/utils.md Illustrates how abs is used in calculateDistance to sum absolute differences between trigram positions. ```go d := 0 i := 0 // profile index n := 3 // trigram position in text for _, tg := range profileTrigrams { d += whatlanggo.abs(n - i) i++ } fmt.Println(d) // 3 ``` -------------------------------- ### Lang.Iso6391 Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Returns the ISO 639-1 code of a Lang as a string. Returns an empty string when there is no ISO 639-1 code. ```APIDOC ## Lang.Iso6391 ### Description Returns the ISO 639-1 code of a `Lang` as a string. Returns an empty string when there is no ISO 639-1 code. ### Method Method call ### Endpoint `func (lang Lang) Iso6391() string` ### Parameters - **lang** (Lang) - Required - Receiver, via method syntax `lang.Iso6391()`. ### Returns - **string** - The two-letter ISO 639-1 code, or `""` when there is no ISO 639-1 code (including the `-1` sentinel). ### Throws/Rejects None. ### Example ```go lang := whatlanggo.Eng fmt.Println(lang.Iso6391()) // "en" ``` ### Example (language without a code) ```go fmt.Println(whatlanggo.Ceb.Iso6391()) // "" (empty string) ``` ``` -------------------------------- ### toTrigramChar function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/trigrams.md Converts punctuation and digits to a space. Returns ' ' when isStopChar(ch) is true, otherwise returns ch unchanged. ```go func toTrigramChar(ch rune) rune ``` -------------------------------- ### LangToStringShort Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/overview.md Converts a Lang constant to its ISO 639-3 code. Returns a string representing the three-letter language code. ```APIDOC ## LangToStringShort ### Description Converts a Lang constant to its ISO 639-3 code. Returns a string representing the three-letter language code. ### Method Function ### Signature `func LangToStringShort(lang Lang) string` ### Parameters - **lang** (Lang) - Required - The Lang constant to convert. ### Returns - **string** - The ISO 639-3 code for the language. ### Example ```go code := whatlanggo.LangToStringShort(lang) ``` ``` -------------------------------- ### isStopChar function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/utils.md Declares isStopChar, which returns true for space, punctuation, digit, or Unicode symbol runes. Used by the trigram pipeline and DetectScript. ```go func isStopChar(r rune) bool ``` -------------------------------- ### Lang.Iso6391 basic call Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/lang.md Returns the ISO 639-1 code for a Lang constant. Returns empty string if no code exists. ```go lang := whatlanggo.Eng fmt.Println(lang.Iso6391()) // "en" ``` -------------------------------- ### DetectScript function signature Source: https://github.com/abadojack/whatlanggo/blob/master/_autodocs/api-reference/script.md Function signature for DetectScript, which returns the script of the given text as a *unicode.RangeTable pointer, or nil when no supported script is found. ```go func DetectScript(text string) *unicode.RangeTable ```