summaryrefslogtreecommitdiff
path: root/sorsyl/test/test_syllabifier.ml
blob: 08f3d0d1c9141ddf837aff26ec88b6b8a5cb09eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
open Printf
open Sorsyl

let test_syllabifier () =
  (* Create sonority calculator *)
  let data_dir =
    if Sys.file_exists "./data" then "./data"
    else if Sys.file_exists "../data" then "../data"
    else if Sys.file_exists "../../../data" then "../../../data"
    else (
      Printf.eprintf "Current directory: %s\n" (Sys.getcwd ());
      failwith "Cannot find data directory")
  in
  let sonority = Sonority.create data_dir in

  (* Test basic syllabification *)
  let test_word word ipa lang =
    printf "\nTesting: %s [%s] (%s)\n" word ipa lang;
    let result = Syllabifier.syllabify ~sonority ~ipa ~word ~lang in
    printf "Clean IPA: %s\n" result.clean_ipa;
    printf "Syllables: %d\n" (List.length result.syllables);
    Base.List.iteri result.syllables ~f:(fun i syl ->
        printf "\nSyllable %d:\n" (i + 1);
        printf "  All: %s\n" (Syllable.all syl);
        printf "  Onset: %s\n" syl.onset;
        printf "  Nucleus: %s\n" syl.nucleus;
        printf "  Coda: %s\n" syl.coda;
        printf "  Tone: %s\n" syl.tone;
        printf "  Stressed: %b\n" syl.stressed)
  in

  (* Test cases *)
  test_word "hello" "hɛˈloʊ" "en";
  test_word "computer" "kəmˈpjuːtər" "en";
  test_word "language" "ˈlæŋɡwɪdʒ" "en";
  test_word "syllable" "ˈsɪləbəl" "en";

  (* Test with tones (if supported) *)
  test_word "ma" "ma˧" "zh";

  (* Test pretty printing *)
  printf "\n\nPretty print example:\n";
  let result =
    Syllabifier.syllabify ~sonority ~ipa:"ˈsɪləbəl" ~word:"syllable" ~lang:"en"
  in
  Base.List.iter result.syllables ~f:(fun syl ->
      printf "%s\n" (Syllable.pretty_print syl))

let () = test_syllabifier ()