···1+(*---------------------------------------------------------------------------
2+ Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>
3+ SPDX-License-Identifier: MIT
4+ ---------------------------------------------------------------------------*)
5+6+let detect_language input_text =
7+ let detector = Langdetect.create_default () in
8+ let results = Langdetect.detect detector input_text in
9+ List.iter
10+ (fun (r : Langdetect.result) -> Printf.printf "%s %.4f\n" r.lang r.prob)
11+ results
12+13+let read_all_stdin () =
14+ let buf = Buffer.create 4096 in
15+ try
16+ while true do
17+ Buffer.add_channel buf stdin 4096
18+ done;
19+ Buffer.contents buf
20+ with End_of_file -> Buffer.contents buf
21+22+let read_file path =
23+ let ic = open_in path in
24+ let n = in_channel_length ic in
25+ let s = really_input_string ic n in
26+ close_in ic;
27+ s
28+29+let run file_opt =
30+ let text =
31+ match file_opt with
32+ | Some path -> read_file path
33+ | None -> read_all_stdin ()
34+ in
35+ if String.length (String.trim text) = 0 then
36+ `Error (false, "No input text provided")
37+ else begin
38+ detect_language text;
39+ `Ok ()
40+ end
41+42+open Cmdliner
43+44+let file_arg =
45+ let doc = "Input file to detect language from. If not provided, reads from stdin." in
46+ Arg.(value & pos 0 (some file) None & info [] ~docv:"FILE" ~doc)
47+48+let cmd =
49+ let doc = "Detect the language of text" in
50+ let man =
51+ [
52+ `S Manpage.s_description;
53+ `P "Detects the natural language of input text using n-gram frequency analysis.";
54+ `P "Outputs detected language codes and their probabilities as space-separated values, one per line, sorted by probability (highest first).";
55+ `S Manpage.s_examples;
56+ `P "Detect language from a file:";
57+ `Pre " langdetect document.txt";
58+ `P "Detect language from stdin:";
59+ `Pre " echo 'Hello world' | langdetect";
60+ ]
61+ in
62+ let info = Cmd.info "langdetect" ~version:"%%VERSION%%" ~doc ~man in
63+ Cmd.v info Term.(ret (const run $ file_arg))
64+65+let () = exit (Cmd.eval cmd)
···48 (targets langdetect-tests.wasm.js)
49 (deps langdetect_js_tests.bc.wasm.js)
50 (action (copy %{deps} %{targets})))
51+52+; Install web assets to share/langdetect-js/
53+; Includes HTML demo, JS files, WASM loaders, and WASM assets with source maps
54+(install
55+ (package langdetect-js)
56+ (section share)
57+ (files
58+ (langdetect.html as langdetect-js/langdetect.html)
59+ ; JS files (work standalone in browsers)
60+ (langdetect.js as langdetect-js/langdetect.js)
61+ (langdetect-tests.js as langdetect-js/langdetect-tests.js)
62+ ; WASM loaders (in same dir so relative asset paths work)
63+ (langdetect_js_main.bc.wasm.js as langdetect-js/langdetect_js_main.bc.wasm.js)
64+ (langdetect_js_tests.bc.wasm.js as langdetect-js/langdetect_js_tests.bc.wasm.js)
65+ ; WASM assets - must be in langdetect-js/ so relative paths from loaders work
66+ (glob_files_rec (langdetect_js_main.bc.wasm.assets/* with_prefix langdetect-js/langdetect_js_main.bc.wasm.assets))
67+ (glob_files_rec (langdetect_js_tests.bc.wasm.assets/* with_prefix langdetect-js/langdetect_js_tests.bc.wasm.assets))))