From 4e29d15645422558ed977d023bd27c15656c8f82 Mon Sep 17 00:00:00 2001 From: benITo47 Date: Tue, 3 Nov 2026 11:35:35 +0100 Subject: [PATCH 1/6] Build Phonemis with Verbalis, its new preprocessing Phonemis now preprocesses text with Verbalis, a nested submodule in third-party/verbalis. The podspec takes Phonemis's sources and header search paths from its phonemis_sources.rb, so they include Verbalis and follow Phonemis's layout. CI and the third-party README check the submodule out recursively; Android needs no change, since Phonemis's CMake builds Verbalis from the submodule. The submodule URL follows Phonemis's move to software-mansion-labs. Phonemis now includes and where it uses them, so the host tests no longer force-include them for libstdc++. --- .cspell-wordlist.txt | 1 + .github/workflows/ci.yml | 2 +- .github/workflows/clang-tidy.yml | 2 +- .gitmodules | 2 +- .../cpp/tests/CMakeLists.txt | 12 ------------ .../react-native-executorch.podspec | 15 ++++++--------- .../scripts/create-package.sh | 2 +- .../react-native-executorch/third-party/README.md | 3 ++- .../third-party/common/phonemis | 2 +- 9 files changed, 14 insertions(+), 27 deletions(-) diff --git a/.cspell-wordlist.txt b/.cspell-wordlist.txt index 3c17403a49..defed32659 100644 --- a/.cspell-wordlist.txt +++ b/.cspell-wordlist.txt @@ -7,6 +7,7 @@ RNET libexecutorch libxnnpack libvulkan +verbalis xcframework metallib ASPP diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b46ab8cf9..5df7eb4c5a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -186,7 +186,7 @@ jobs: - name: Check out the submodules the tests build run: | git submodule update --init --depth 1 third-party/googletest - GIT_LFS_SKIP_SMUDGE=1 git submodule update --init --depth 1 \ + GIT_LFS_SKIP_SMUDGE=1 git submodule update --init --recursive --depth 1 \ packages/react-native-executorch/third-party/common/phonemis working-directory: ${{ github.workspace }} diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 2a6143ac51..5c4fbcc608 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -27,7 +27,7 @@ jobs: # The phonemis submodule carries headers that cpp/extensions/speech # includes; without it those sources fail to parse and clang-tidy # reports misleading findings on the broken AST. - submodules: true + submodules: recursive - name: Setup uses: ./.github/actions/setup diff --git a/.gitmodules b/.gitmodules index 6236aeac75..3cdc51a302 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,5 +3,5 @@ url = https://github.com/google/googletest.git [submodule "packages/react-native-executorch/third-party/common/phonemis"] path = packages/react-native-executorch/third-party/common/phonemis - url = https://github.com/IgorSwat/Phonemis + url = https://github.com/software-mansion-labs/Phonemis branch = main diff --git a/packages/react-native-executorch/cpp/tests/CMakeLists.txt b/packages/react-native-executorch/cpp/tests/CMakeLists.txt index f25fa07ec2..2d69e01c67 100644 --- a/packages/react-native-executorch/cpp/tests/CMakeLists.txt +++ b/packages/react-native-executorch/cpp/tests/CMakeLists.txt @@ -227,18 +227,6 @@ endif() if(RNE_TESTS_ENABLE_PHONEMIS) add_subdirectory("${PHONEMIS_DIR}" "${CMAKE_BINARY_DIR}/phonemis" EXCLUDE_FROM_ALL) list(APPEND RNE_SOURCES ${PHONEMIS_SOURCES}) - - # Two phonemis headers use std::optional and std::u32string without including - # / . libc++ pulls both in transitively, so the Android - # (NDK) and iOS builds never notice; libstdc++ does not, and the member - # declarations then fail to parse, leaving a pile of "no declaration matches" - # errors on their definitions. Force-include the two headers rather than patch - # a pinned submodule; drop this once upstream adds the includes. - # SHELL: keeps the two -include flags paired; without it CMake de-duplicates - # the repeated -include and the second header becomes an input file. - if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") - target_compile_options(phonemis PRIVATE "SHELL:-include optional" "SHELL:-include string") - endif() endif() add_library(rne_under_test STATIC ${RNE_SOURCES}) diff --git a/packages/react-native-executorch/react-native-executorch.podspec b/packages/react-native-executorch/react-native-executorch.podspec index bf9921f91a..a412d21a42 100644 --- a/packages/react-native-executorch/react-native-executorch.podspec +++ b/packages/react-native-executorch/react-native-executorch.podspec @@ -1,5 +1,6 @@ require "fileutils" require "json" +require_relative "third-party/common/phonemis/phonemis_sources" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) @@ -99,10 +100,10 @@ Pod::Spec.new do |s| # phonemis is built from in-tree source (third-party/common/phonemis submodule); # its runner entrypoint is excluded so only the library sources compile. + phonemis_root = "third-party/common/phonemis" phonemis_source_files = [ "cpp/extensions/speech/phonemizer.{cpp,h}", - "third-party/common/phonemis/src/**/*.{cpp,hpp,h}", - ] + ] + PhonemisSources.source_files(phonemis_root) source_files = [ "ios/**/*.{h,m,mm}", @@ -114,8 +115,7 @@ Pod::Spec.new do |s| # cpp/tests holds the host GoogleTest suites, built by cpp/tests/CMakeLists.txt # and never by the app: `cpp/**` above sweeps them in, and the pod carries no # googletest headers, so an app build fails on . - exclude_files = [ - "third-party/common/phonemis/src/phonemis/main.cpp", + exclude_files = PhonemisSources.exclude_files(phonemis_root) + [ "cpp/tests/**/*", ] exclude_files += opencv_source_files unless enable_opencv @@ -253,7 +253,7 @@ Pod::Spec.new do |s| "$(inherited)", "EXECUTORCH_ENABLE_EXECUTION_PROFILING=1", ].join(' '), - "HEADER_SEARCH_PATHS" => [ + "HEADER_SEARCH_PATHS" => ([ "\"$(PODS_TARGET_SRCROOT)/cpp\"", "\"$(PODS_TARGET_SRCROOT)/#{third_party_include}\"", "\"$(PODS_TARGET_SRCROOT)/third-party/include/cpuinfo\"", @@ -262,10 +262,7 @@ Pod::Spec.new do |s| "\"$(PODS_TARGET_SRCROOT)/third-party/include/executorch/extension/llm/tokenizers/third-party/json/include\"", "\"$(PODS_TARGET_SRCROOT)/third-party/include/executorch/extension/llm/tokenizers/third-party/re2\"", "\"$(PODS_TARGET_SRCROOT)/third-party/include/executorch/extension/llm/tokenizers/third-party/abseil-cpp\"", - "\"$(PODS_TARGET_SRCROOT)/third-party/common/phonemis/src\"", - # phonemis's vendored xsimd includes itself as `xsimd/...` (its CMakeLists adds this too). - "\"$(PODS_TARGET_SRCROOT)/third-party/common/phonemis/src/third-party\"", - ].join(' '), + ] + PhonemisSources.header_search_paths(phonemis_root).map { |p| "\"$(PODS_TARGET_SRCROOT)/#{p}\"" }).join(' '), "WARNING_CFLAGS" => "-Wno-documentation", 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'x86_64', } diff --git a/packages/react-native-executorch/scripts/create-package.sh b/packages/react-native-executorch/scripts/create-package.sh index 1450f932d1..5f1c3d777f 100755 --- a/packages/react-native-executorch/scripts/create-package.sh +++ b/packages/react-native-executorch/scripts/create-package.sh @@ -6,7 +6,7 @@ set -euo pipefail # from packages/react-native-executorch. # Phonemis is a git submodule whose sources the podspec compiles directly -# (see third-party/common/phonemis/src in react-native-executorch.podspec). +# (listed by third-party/common/phonemis/phonemis_sources.rb, which the podspec requires). # Init it explicitly so the files are present in the packed tarball. # Run from repo root so the submodule path resolves regardless of cwd. git -C "$(git rev-parse --show-toplevel)" submodule update --init --recursive \ diff --git a/packages/react-native-executorch/third-party/README.md b/packages/react-native-executorch/third-party/README.md index ed292fd314..4570d6d28e 100644 --- a/packages/react-native-executorch/third-party/README.md +++ b/packages/react-native-executorch/third-party/README.md @@ -9,7 +9,8 @@ Native ExecuTorch artifacts are split into **committed source** and static archives into `ExecutorchLib.xcframework` (used only when (re)building the iOS release artifacts). - `common/phonemis` — git submodule (TTS phonemizer), built from source. Run - `git submodule update --init` to fetch it. + `git submodule update --init --recursive` to fetch it with Verbalis, its nested + submodule. ## Downloaded on demand (NOT committed) diff --git a/packages/react-native-executorch/third-party/common/phonemis b/packages/react-native-executorch/third-party/common/phonemis index d2595d8136..8fd520e32e 160000 --- a/packages/react-native-executorch/third-party/common/phonemis +++ b/packages/react-native-executorch/third-party/common/phonemis @@ -1 +1 @@ -Subproject commit d2595d8136aa51fd6a55b8bfe2e940a6cd2f063d +Subproject commit 8fd520e32e9bb9813592dc3ee61ee7cdac864388 From 02ae70c2dee1ad74504e657cf82598577a8255e4 Mon Sep 17 00:00:00 2001 From: benITo47 Date: Wed, 4 Nov 2026 14:18:16 +0100 Subject: [PATCH 2/6] Check out Verbalis where Phonemis needs it, and credit it in LICENSE - compile_flags.txt: add Verbalis's include path, so clangd and clang-tidy parse the speech sources, which reach through Phonemis's headers. - clang-tidy.yml: check out only Phonemis and Verbalis, as ci.yml does, instead of every submodule. - ci.yml: check out Phonemis in the TypeScript API tests job, since the podspec they evaluate requires phonemis_sources.rb. - create-package.sh: pack only Verbalis's cpp/ and license files. - LICENSE: list Verbalis (MIT), which bundles the cn2an port. - cpp/tests/README.md: check out Phonemis recursively. --- .github/workflows/ci.yml | 7 +++++ .github/workflows/clang-tidy.yml | 14 ++++++--- LICENSE | 31 ++++++++++++++++++- .../react-native-executorch/compile_flags.txt | 1 + .../cpp/tests/README.md | 2 +- .../scripts/create-package.sh | 6 ++++ 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5df7eb4c5a..3404c4fdf5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -287,6 +287,13 @@ jobs: - name: Checkout uses: actions/checkout@v6 + # The podspec suites evaluate the podspec, which requires phonemis' + # phonemis_sources.rb. Nothing reads phonemis' Git LFS `data/`. + - name: Check out phonemis + run: | + GIT_LFS_SKIP_SMUDGE=1 git submodule update --init --depth 1 \ + packages/react-native-executorch/third-party/common/phonemis + - name: Setup uses: ./.github/actions/setup diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 5c4fbcc608..069defd1f0 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -23,11 +23,15 @@ jobs: steps: - name: Checkout uses: actions/checkout@v6 - with: - # The phonemis submodule carries headers that cpp/extensions/speech - # includes; without it those sources fail to parse and clang-tidy - # reports misleading findings on the broken AST. - submodules: recursive + + # cpp/extensions/speech includes phonemis' headers, and through them + # Verbalis'; without them those sources fail to parse and clang-tidy + # reports misleading findings on the broken AST. phonemis' `data/` is + # Git LFS that nothing here reads, so the checkout skips it. + - name: Check out phonemis + run: | + GIT_LFS_SKIP_SMUDGE=1 git submodule update --init --recursive --depth 1 \ + packages/react-native-executorch/third-party/common/phonemis - name: Setup uses: ./.github/actions/setup diff --git a/LICENSE b/LICENSE index aa15d47034..7cbd9676d2 100644 --- a/LICENSE +++ b/LICENSE @@ -32,13 +32,17 @@ its own license, reproduced in full further down this file. OpenCV Apache-2.0 vision pipelines and multimodal LLMs KleidiCV Apache-2.0 OpenCV Arm HAL, Android only phonemis MIT grapheme-to-phoneme for text-to-speech + Verbalis MIT text preprocessing for phonemis MLX MIT MLX backend, iOS only Which of these end up in an application binary depends on the backends and libraries it enables; see the Native Libraries documentation. ExecuTorch in turn bundles further components, among them XNNPACK, pthreadpool, cpuinfo, the tokenizers library and PCRE2, each under its own permissive license and -covered by the notices shipped with the ExecuTorch distribution. +covered by the notices shipped with the ExecuTorch distribution. Likewise, +Verbalis bundles a port of cn2an (MIT) for Chinese numbers, and phonemis and +Verbalis list their own components in the THIRD_PARTY_NOTICES.md files under +third-party/common/phonemis. =============================================================================== @@ -107,6 +111,31 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +=============================================================================== +Verbalis, MIT +=============================================================================== + +Copyright (c) 2026 Software Mansion + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + =============================================================================== MLX, MIT =============================================================================== diff --git a/packages/react-native-executorch/compile_flags.txt b/packages/react-native-executorch/compile_flags.txt index 02a120dc77..ca161d47eb 100644 --- a/packages/react-native-executorch/compile_flags.txt +++ b/packages/react-native-executorch/compile_flags.txt @@ -5,6 +5,7 @@ -isystem../../node_modules/react-native/ReactCommon/jsi -isystemthird-party/include -isystemthird-party/common/phonemis/src +-isystemthird-party/common/phonemis/third-party/verbalis/cpp/include -isystemthird-party/include/executorch/extension/llm/tokenizers/include -isystemthird-party/include/executorch/extension/llm/tokenizers/third-party/json/include -isystemthird-party/include/executorch/extension/llm/tokenizers/third-party/re2 diff --git a/packages/react-native-executorch/cpp/tests/README.md b/packages/react-native-executorch/cpp/tests/README.md index a694639e93..10199fa77a 100644 --- a/packages/react-native-executorch/cpp/tests/README.md +++ b/packages/react-native-executorch/cpp/tests/README.md @@ -62,7 +62,7 @@ RNET_HEADERS_ONLY=1 node scripts/download-libs.js # 2. The submodules the tests compile. phonemis' `data/` is Git LFS and nothing # here reads it, so the checkout skips it. git submodule update --init --depth 1 ../../third-party/googletest -GIT_LFS_SKIP_SMUDGE=1 git submodule update --init --depth 1 third-party/common/phonemis +GIT_LFS_SKIP_SMUDGE=1 git submodule update --init --recursive --depth 1 third-party/common/phonemis # 3. Hermes + a minimal ExecuTorch host build (several minutes, cached afterwards) scripts/build-native-test-deps.sh diff --git a/packages/react-native-executorch/scripts/create-package.sh b/packages/react-native-executorch/scripts/create-package.sh index 5f1c3d777f..8f41bf74a6 100755 --- a/packages/react-native-executorch/scripts/create-package.sh +++ b/packages/react-native-executorch/scripts/create-package.sh @@ -17,6 +17,8 @@ git -C "$(git rev-parse --show-toplevel)" submodule update --init --recursive \ # `!scripts/build*` which npm-packlist honors and re-includes those files # despite our exclusion rules. Restore on exit so the working tree stays clean. PHONEMIS_DIR="third-party/common/phonemis" +# Of Verbalis, the podspec compiles only cpp/; keep it and the license files. +VERBALIS_DIR="$PHONEMIS_DIR/third-party/verbalis" # npm only picks up a LICENSE that sits in the package directory, and ours # lives at the repo root, so the published tarball carried no license text at @@ -27,11 +29,15 @@ cp "$(git rev-parse --show-toplevel)/LICENSE" LICENSE cleanup() { git -C "$PHONEMIS_DIR" checkout -- data test scripts requirements.txt 2>/dev/null || true + git -C "$VERBALIS_DIR" checkout -- . 2>/dev/null || true rm -f LICENSE } trap cleanup EXIT rm -rf "$PHONEMIS_DIR/data" "$PHONEMIS_DIR/test" "$PHONEMIS_DIR/scripts" rm -f "$PHONEMIS_DIR/requirements.txt" +find "$VERBALIS_DIR" -mindepth 1 -maxdepth 1 \ + ! -name .git ! -name cpp ! -name LICENSE ! -name licenses ! -name THIRD_PARTY_NOTICES.md \ + -exec rm -rf {} + # The publish workflow installs before calling this script, so it sets # RNE_SKIP_INSTALL to avoid a second pass. A local run still installs. From 2b6849f2b3ec35af22627b23ff6ea5d67822f4fd Mon Sep 17 00:00:00 2001 From: benITo47 Date: Wed, 4 Nov 2026 14:23:27 +0100 Subject: [PATCH 3/6] Move Phonemis to its main, and leave its README sources out of the package Phonemis main has the merged Verbalis work, and notices that credit Verbalis. create-package.sh also leaves out Phonemis's README image and README source, and restores the whole submodule after packing. --- packages/react-native-executorch/scripts/create-package.sh | 6 +++--- .../react-native-executorch/third-party/common/phonemis | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/react-native-executorch/scripts/create-package.sh b/packages/react-native-executorch/scripts/create-package.sh index 8f41bf74a6..c87898fe56 100755 --- a/packages/react-native-executorch/scripts/create-package.sh +++ b/packages/react-native-executorch/scripts/create-package.sh @@ -28,13 +28,13 @@ VERBALIS_DIR="$PHONEMIS_DIR/third-party/verbalis" cp "$(git rev-parse --show-toplevel)/LICENSE" LICENSE cleanup() { - git -C "$PHONEMIS_DIR" checkout -- data test scripts requirements.txt 2>/dev/null || true + git -C "$PHONEMIS_DIR" checkout -- . 2>/dev/null || true git -C "$VERBALIS_DIR" checkout -- . 2>/dev/null || true rm -f LICENSE } trap cleanup EXIT -rm -rf "$PHONEMIS_DIR/data" "$PHONEMIS_DIR/test" "$PHONEMIS_DIR/scripts" -rm -f "$PHONEMIS_DIR/requirements.txt" +rm -rf "$PHONEMIS_DIR/data" "$PHONEMIS_DIR/test" "$PHONEMIS_DIR/scripts" "$PHONEMIS_DIR/assets" +rm -f "$PHONEMIS_DIR/requirements.txt" "$PHONEMIS_DIR/readme.source.md" find "$VERBALIS_DIR" -mindepth 1 -maxdepth 1 \ ! -name .git ! -name cpp ! -name LICENSE ! -name licenses ! -name THIRD_PARTY_NOTICES.md \ -exec rm -rf {} + diff --git a/packages/react-native-executorch/third-party/common/phonemis b/packages/react-native-executorch/third-party/common/phonemis index 8fd520e32e..5ec260e331 160000 --- a/packages/react-native-executorch/third-party/common/phonemis +++ b/packages/react-native-executorch/third-party/common/phonemis @@ -1 +1 @@ -Subproject commit 8fd520e32e9bb9813592dc3ee61ee7cdac864388 +Subproject commit 5ec260e33127de044bda7147f52cea27a7e2e491 From e40c74ef68646958ea4812c711f0e39691666b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C5=82uszniak?= Date: Wed, 4 Nov 2026 14:50:47 +0100 Subject: [PATCH 4/6] Pin the phonemes phonemis produces on Kokoro's en-us data PhonemizerOutputTest runs the en-us pipeline on the shipped tagger and lexicon, fetched as fixtures, and pins the phonemes and word locations for numbers, money, dates, abbreviations, times and units. --- .../cpp/tests/CMakeLists.txt | 20 +++ .../cpp/tests/README.md | 7 +- .../tests/extensions/PhonemizerOutputTest.cpp | 121 ++++++++++++++++++ .../scripts/fetch-test-fixtures.sh | 21 ++- 4 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp diff --git a/packages/react-native-executorch/cpp/tests/CMakeLists.txt b/packages/react-native-executorch/cpp/tests/CMakeLists.txt index 2d69e01c67..abf95ee2e5 100644 --- a/packages/react-native-executorch/cpp/tests/CMakeLists.txt +++ b/packages/react-native-executorch/cpp/tests/CMakeLists.txt @@ -325,6 +325,22 @@ else() " scripts/fetch-test-fixtures.sh") endif() +# With Kokoro's en-us tagger and lexicon, phonemis produces real phonemes, so +# what it outputs can be pinned, not only how it is called. Same story again. +set(PHONEMIZER_FIXTURE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/fixtures/phonemizer/en-us") +if(RNE_TESTS_ENABLE_PHONEMIS AND EXISTS "${PHONEMIZER_FIXTURE_DIR}/lexicon.json" + AND EXISTS "${PHONEMIZER_FIXTURE_DIR}/tags.json") + set(RNE_HAVE_PHONEMIZER_FIXTURE ON) + target_compile_definitions(rne_test_support PUBLIC + RNE_PHONEMIZER_EN_US_LEXICON="${PHONEMIZER_FIXTURE_DIR}/lexicon.json" + RNE_PHONEMIZER_EN_US_TAGGER="${PHONEMIZER_FIXTURE_DIR}/tags.json") +elseif(RNE_TESTS_ENABLE_PHONEMIS) + set(RNE_HAVE_PHONEMIZER_FIXTURE OFF) + message(WARNING + "Phonemizer fixtures missing, skipping the PhonemizerOutputTest suite. Fetch them with:\n" + " scripts/fetch-test-fixtures.sh") +endif() + # --- Test binaries ---------------------------------------------------------- enable_testing() include(GoogleTest) @@ -358,6 +374,10 @@ if(RNE_HAVE_TOKENIZER_FIXTURE) list(APPEND TEST_SUITES extensions/TokenizerTest.cpp) endif() +if(RNE_HAVE_PHONEMIZER_FIXTURE) + list(APPEND TEST_SUITES extensions/PhonemizerOutputTest.cpp) +endif() + foreach(suite_path IN LISTS TEST_SUITES) get_filename_component(suite_name "${suite_path}" NAME_WE) add_executable(${suite_name} "${suite_path}") diff --git a/packages/react-native-executorch/cpp/tests/README.md b/packages/react-native-executorch/cpp/tests/README.md index 10199fa77a..b5072cf97f 100644 --- a/packages/react-native-executorch/cpp/tests/README.md +++ b/packages/react-native-executorch/cpp/tests/README.md @@ -45,7 +45,7 @@ rather than a lookalike. | `support/JsiTestEnv.*` | Fixture owning a Hermes runtime with the module installed, plus `eval*` helpers and the `isCodedError` / `throwsCoded` assertions | | `core/` | `dtype`, `error`, `conversions`, `tensor`, `schema`, `model`, `utils` | | `extensions/` | `math`, `speech`, `nlp` (tokenizer), `llm`, and — behind OpenCV — `cv` and `ocr` ops; `phonemizer` behind phonemis | -| `fixtures/` | Downloaded `.pte` program and `tokenizer.json` (gitignored) | +| `fixtures/` | Downloaded `.pte` program, `tokenizer.json` and en-us phonemizer data (gitignored) | One binary per suite, so a crash in one area cannot take the run down with it and `ctest -R MathOpsTest` targets a single suite. @@ -113,7 +113,7 @@ CMake package when present and otherwise locates the two libraries directly. ## The fixtures -`scripts/fetch-test-fixtures.sh` downloads two, both pinned to an exact Hugging +`scripts/fetch-test-fixtures.sh` downloads them, all pinned to an exact Hugging Face revision and checksum-verified. `run-native-tests.sh` fetches them automatically; they land in `fixtures/` and are gitignored rather than committed. @@ -121,6 +121,9 @@ automatically; they land in `fixtures/` and are gitignored rather than committed publishes. Anything reading ExecuTorch `MethodMeta` needs a real program. - A **tokenizer.json** — Whisper tiny.en's, ~2.4 MB. A plain BPE vocabulary, so the nlp extension's encode/decode path runs for real. +- Kokoro's **en-us phonemizer data** — the tagger and lexicon the app ships, + ~8.4 MB together. `PhonemizerOutputTest` pins the phonemes and word locations + phonemis produces with them. The useful part is that this needs **no XNNPACK delegate**, even though the fixture is XNNPACK-delegated. `ModelHostObject`'s constructor only calls diff --git a/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp b/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp new file mode 100644 index 0000000000..b9d6702f80 --- /dev/null +++ b/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp @@ -0,0 +1,121 @@ +#include +#include +#include + +#include "support/JsiTestEnv.h" + +namespace rnexecutorch::tests { +namespace { + +using PhonemizerOutputTest = JsiTestEnv; + +// PhonemizerTest pins the JSI contract with no phonemis data. These run the +// en-us pipeline on Kokoro's shipped tagger and lexicon +// (scripts/fetch-test-fixtures.sh), so they pin what TTS is actually fed: how +// numbers, dates and abbreviations are read out, and which phonemes each input +// word is located at, which is what word timings are built from. +// +// The neural fallback is not loaded (it needs ExecuTorch execution), so every +// sentence sticks to words the lexicon knows; an unknown one would come out +// empty here but not in the app. + +struct Case { + const char *text; + const char *phonemes; + // Each located word as "text:phonemes", joined with '|'. + const char *words; +}; + +// Phonemizes every case with one phonemizer, since loading the lexicon is +// what takes the time. The texts hold no quotes. +void expectPhonemized(JsiTestEnv &env, std::initializer_list cases) { + std::string texts; + for (const auto &c : cases) { + texts += std::format("'{}',", c.text); + } + // Each result as "phonemes\nwords", separated by '\x1e'. + auto results = env.evalString(std::format(R"( + const p = __rnexecutorch_jsi__.speech.createPhonemizer({{ + lang: 'en-us', taggerSource: '{}', lexiconSource: '{}' }}); + return [{}].map((text) => {{ + const r = p.phonemize(text, {{ words: true }}); + return r.phonemes + '\n' + r.words + .map((w) => w.text + ':' + r.phonemes.substr(w.phonemeOffset, w.phonemeLength)) + .join('|'); + }}).join('\x1e'); + )", + RNE_PHONEMIZER_EN_US_TAGGER, RNE_PHONEMIZER_EN_US_LEXICON, texts)); + + size_t begin = 0; + for (const auto &c : cases) { + SCOPED_TRACE(c.text); + auto end = results.find('\x1e', begin); + EXPECT_EQ(results.substr(begin, end - begin), std::string(c.phonemes) + "\n" + c.words); + begin = end == std::string::npos ? results.size() : end + 1; + } +} + +TEST_F(PhonemizerOutputTest, ReadsPlainWordsFromTheLexicon) { + expectPhonemized(*this, {{"Hello world.", "həlˈO wˈɜɹld.", "Hello:həlˈO|world.:wˈɜɹld."}}); +} + +TEST_F(PhonemizerOutputTest, SpellsOutNumbers) { + expectPhonemized(*this, { + {"I have 25 cats.", "ˌI hæv twˈɛntifˈIv kˈæts.", + "I:ˌI|have:hæv|25:twˈɛntifˈIv|cats.:kˈæts."}, + {"It happened in 1999.", "ˌɪt hˈæpənd ɪn nˌIntˈin nˈIndinˈIn.", + "It:ˌɪt|happened:hˈæpənd|in:ɪn|1999.:nˌIntˈin nˈIndinˈIn."}, + {"The 21st century began.", "ðə twˈɛntifˈɜɹst sˈɛnʧəɹi bəɡˈæn.", + "The:ðə|21st:twˈɛntifˈɜɹst|century:sˈɛnʧəɹi|began.:bəɡˈæn."}, + {"Pi is roughly 3.14.", "pˈI ɪz ɹˈʌfli θɹˈi pˈYnt wˈʌn fˈɔɹ.", + "Pi:pˈI|is:ɪz|roughly:ɹˈʌfli|3.14.:θɹˈi pˈYnt wˈʌn fˈɔɹ."}, + {"We sold 1,000,000 copies.", "wˌi sˈOld wˈʌn mˈɪljᵊn kˈɑpiz.", + "We:wˌi|sold:sˈOld|1,000,000:wˈʌn mˈɪljᵊn|copies.:kˈɑpiz."}, + {"Chapter IV starts here.", "ʧˈæptəɹ fˈɔɹ stˈɑɹts hˈɪɹ.", + "Chapter:ʧˈæptəɹ|IV:fˈɔɹ|starts:stˈɑɹts|here.:hˈɪɹ."}, + {"Call 555-1234 now.", "kˈɔl fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ nˈW.", + "Call:kˈɔl|555-1234:fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ|now.:nˈW."}, + }); +} + +TEST_F(PhonemizerOutputTest, ReadsMoneyAndPercentages) { + expectPhonemized(*this, { + {"It costs $4.50 today.", "ˌɪt kˈɔsts fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts tədˈA.", + "It:ˌɪt|costs:kˈɔsts|$4.50:fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts|today.:tədˈA."}, + {"About 50% of people agree.", "əbˈWt fˈɪfti pəɹsˈɛnt ʌv pˈipᵊl əɡɹˈi.", + "About:əbˈWt|50%:fˈɪfti pəɹsˈɛnt|of:ʌv|people:pˈipᵊl|agree.:əɡɹˈi."}, + }); +} + +TEST_F(PhonemizerOutputTest, ReadsDates) { + expectPhonemized(*this, { + {"On 12/25/2024 we left.", "ˈɔn disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ wi lˈɛft.", + "On:ˈɔn|12/25/2024:disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ|we:wi|left.:lˈɛft."}, + {"Born on May 5, 2024.", "bˈɔɹn ˌɔn mˈA fˈɪfθ, twˈɛnti twˈɛntifˈɔɹ.", + "Born:bˈɔɹn|on:ˌɔn|May:mˈA|5,:fˈɪfθ,|2024.:twˈɛnti twˈɛntifˈɔɹ."}, + }); +} + +TEST_F(PhonemizerOutputTest, ExpandsAbbreviationsAndSymbols) { + expectPhonemized(*this, { + {"Dr. Smith lives on Main St.", "dˈɑktəɹ smˈɪθ lˈIvz ˌɔn mˈAn stɹˈit.", + "Dr.:dˈɑktəɹ|Smith:smˈɪθ|lives:lˈIvz|on:ˌɔn|Main:mˈAn|St.:stɹˈit."}, + {"Bring fruit, e.g. apples.", "bɹˈɪŋ fɹˈut, fɔɹ ɪɡzˈæmpəl ˈæpᵊlz.", + "Bring:bɹˈɪŋ|fruit,:fɹˈut,|e.g.:fɔɹ ɪɡzˈæmpəl|apples.:ˈæpᵊlz."}, + {"Rock & roll.", "ɹˈɑk ænd ɹˈOl.", "Rock:ɹˈɑk|&:ænd|roll.:ɹˈOl."}, + }); +} + +// A time or a measure is read as one phrase, and comes back as one word that +// spans both input words, so their word timings are shared. +TEST_F(PhonemizerOutputTest, LocatesAPhraseReadAsOneUnitAsOneWord) { + expectPhonemized(*this, { + {"Meet me at 3:30 pm.", "mˈit mˌi æt θɹˈi θˈɜɹɾi pˈiˈɛm.", + "Meet:mˈit|me:mˌi|at:æt|3:30 pm.:θɹˈi θˈɜɹɾi pˈiˈɛm."}, + {"She ran 5 km.", "ʃˌi ɹˈæn fˈIv kəlˈɑməɾəɹz.", + "She:ʃˌi|ran:ɹˈæn|5 km.:fˈIv kəlˈɑməɾəɹz."}, + }); +} + +} // namespace +} // namespace rnexecutorch::tests diff --git a/packages/react-native-executorch/scripts/fetch-test-fixtures.sh b/packages/react-native-executorch/scripts/fetch-test-fixtures.sh index f1440f0805..222d07eaa4 100755 --- a/packages/react-native-executorch/scripts/fetch-test-fixtures.sh +++ b/packages/react-native-executorch/scripts/fetch-test-fixtures.sh @@ -12,8 +12,11 @@ # * A HuggingFace tokenizer.json, which the nlp extension loads through # ExecuTorch's HFTokenizer. Whisper tiny.en's is a plain BPE vocabulary, # ~2.4 MB. +# * Kokoro's en-us phonemizer data, the tagger and lexicon the app ships +# (~8.4 MB together). With them phonemis produces real phonemes, so the +# phonemizer's text normalization and word locating are pinned end to end. # -# Both are pinned to an exact Hugging Face revision and verified against a +# All are pinned to an exact Hugging Face revision and verified against a # recorded sha256, so a re-tag upstream cannot silently change what the tests # assert. Idempotent: a no-op when a fixture is already present and matches. # @@ -43,7 +46,7 @@ fetch_fixture() { return fi - mkdir -p "$FIXTURE_DIR" + mkdir -p "$(dirname "$target")" local url="https://huggingface.co/${repo}/resolve/${revision}/${path}" echo "↓ ${url}" curl -fsSL -o "${target}.tmp" "$url" @@ -75,3 +78,17 @@ fetch_fixture \ "c99612ff807ef223f6316f33a4be7c587835a5ce" \ "tokenizer.json" \ "5eb60cec1e77aeeb6869a2bb5a8e01a84c3fe5d072d75369343021fe6f5310d0" + +fetch_fixture \ + "phonemizer/en-us/tags.json" \ + "software-mansion/Kokoro-82M-ExecuTorch" \ + "4836d50e0fa48d7cdd415210f6c2c0e1acba5acb" \ + "phonemizer/en-us/tags.json" \ + "028f7d6a0ee7638d91fab27ef8809c0b36de312e6409b6b8e844bffd0871e3e7" + +fetch_fixture \ + "phonemizer/en-us/lexicon.json" \ + "software-mansion/Kokoro-82M-ExecuTorch" \ + "4836d50e0fa48d7cdd415210f6c2c0e1acba5acb" \ + "phonemizer/en-us/lexicon.json" \ + "b29e9c13190cf211979e8d9c38258cc1cb8e701e5ca1102a0c1f00acd4f55f8e" From 68ff81705fe5d9fa406b516f01b0911aab9010b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C5=82uszniak?= Date: Wed, 4 Nov 2026 15:04:23 +0100 Subject: [PATCH 5/6] Pin phonemis output for every language Kokoro uses PhonemizerOutputTest adds en-gb and zh on their shipped data. PreprocessingTest pins what phonemis feeds the neural model for de, fr, es, it, pl, pt and hi. Known misreadings point at Verbalis#8. --- .../cpp/tests/CMakeLists.txt | 23 ++-- .../cpp/tests/README.md | 10 +- .../tests/extensions/PhonemizerOutputTest.cpp | 118 ++++++++++------- .../tests/extensions/PreprocessingTest.cpp | 125 ++++++++++++++++++ .../scripts/fetch-test-fixtures.sh | 29 +++- 5 files changed, 241 insertions(+), 64 deletions(-) create mode 100644 packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp diff --git a/packages/react-native-executorch/cpp/tests/CMakeLists.txt b/packages/react-native-executorch/cpp/tests/CMakeLists.txt index abf95ee2e5..ffa2f821c8 100644 --- a/packages/react-native-executorch/cpp/tests/CMakeLists.txt +++ b/packages/react-native-executorch/cpp/tests/CMakeLists.txt @@ -325,17 +325,20 @@ else() " scripts/fetch-test-fixtures.sh") endif() -# With Kokoro's en-us tagger and lexicon, phonemis produces real phonemes, so -# what it outputs can be pinned, not only how it is called. Same story again. -set(PHONEMIZER_FIXTURE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/fixtures/phonemizer/en-us") -if(RNE_TESTS_ENABLE_PHONEMIS AND EXISTS "${PHONEMIZER_FIXTURE_DIR}/lexicon.json" - AND EXISTS "${PHONEMIZER_FIXTURE_DIR}/tags.json") - set(RNE_HAVE_PHONEMIZER_FIXTURE ON) +# With Kokoro's tagger, segmenter and lexicons, phonemis produces real +# phonemes, so what it outputs can be pinned, not only how it is called. Same +# story again. +set(PHONEMIZER_FIXTURE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/fixtures/phonemizer") +set(RNE_HAVE_PHONEMIZER_FIXTURE ${RNE_TESTS_ENABLE_PHONEMIS}) +foreach(file en-us/tags.json en-us/lexicon.json en-gb/lexicon.json zh/segmenter.json zh/lexicon.json) + if(NOT EXISTS "${PHONEMIZER_FIXTURE_DIR}/${file}") + set(RNE_HAVE_PHONEMIZER_FIXTURE OFF) + endif() +endforeach() +if(RNE_HAVE_PHONEMIZER_FIXTURE) target_compile_definitions(rne_test_support PUBLIC - RNE_PHONEMIZER_EN_US_LEXICON="${PHONEMIZER_FIXTURE_DIR}/lexicon.json" - RNE_PHONEMIZER_EN_US_TAGGER="${PHONEMIZER_FIXTURE_DIR}/tags.json") + RNE_PHONEMIZER_FIXTURE_DIR="${PHONEMIZER_FIXTURE_DIR}") elseif(RNE_TESTS_ENABLE_PHONEMIS) - set(RNE_HAVE_PHONEMIZER_FIXTURE OFF) message(WARNING "Phonemizer fixtures missing, skipping the PhonemizerOutputTest suite. Fetch them with:\n" " scripts/fetch-test-fixtures.sh") @@ -363,7 +366,7 @@ if(RNE_TESTS_ENABLE_OPENCV) endif() if(RNE_TESTS_ENABLE_PHONEMIS) - list(APPEND TEST_SUITES extensions/PhonemizerTest.cpp) + list(APPEND TEST_SUITES extensions/PhonemizerTest.cpp extensions/PreprocessingTest.cpp) endif() if(RNE_HAVE_MODEL_FIXTURE) diff --git a/packages/react-native-executorch/cpp/tests/README.md b/packages/react-native-executorch/cpp/tests/README.md index b5072cf97f..efb36e2727 100644 --- a/packages/react-native-executorch/cpp/tests/README.md +++ b/packages/react-native-executorch/cpp/tests/README.md @@ -45,7 +45,7 @@ rather than a lookalike. | `support/JsiTestEnv.*` | Fixture owning a Hermes runtime with the module installed, plus `eval*` helpers and the `isCodedError` / `throwsCoded` assertions | | `core/` | `dtype`, `error`, `conversions`, `tensor`, `schema`, `model`, `utils` | | `extensions/` | `math`, `speech`, `nlp` (tokenizer), `llm`, and — behind OpenCV — `cv` and `ocr` ops; `phonemizer` behind phonemis | -| `fixtures/` | Downloaded `.pte` program, `tokenizer.json` and en-us phonemizer data (gitignored) | +| `fixtures/` | Downloaded `.pte` program, `tokenizer.json` and phonemizer data (gitignored) | One binary per suite, so a crash in one area cannot take the run down with it and `ctest -R MathOpsTest` targets a single suite. @@ -121,9 +121,11 @@ automatically; they land in `fixtures/` and are gitignored rather than committed publishes. Anything reading ExecuTorch `MethodMeta` needs a real program. - A **tokenizer.json** — Whisper tiny.en's, ~2.4 MB. A plain BPE vocabulary, so the nlp extension's encode/decode path runs for real. -- Kokoro's **en-us phonemizer data** — the tagger and lexicon the app ships, - ~8.4 MB together. `PhonemizerOutputTest` pins the phonemes and word locations - phonemis produces with them. +- Kokoro's **phonemizer data** for en-us, en-gb and zh, the tagger, segmenter + and lexicons the app ships, ~26 MB together. `PhonemizerOutputTest` pins the + phonemes and word locations phonemis produces with them. The other languages + need a neural model, so `PreprocessingTest` pins only the text phonemis feeds + it, which needs no data. The useful part is that this needs **no XNNPACK delegate**, even though the fixture is XNNPACK-delegated. `ModelHostObject`'s constructor only calls diff --git a/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp b/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp index b9d6702f80..a306aa529d 100644 --- a/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp +++ b/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp @@ -10,8 +10,8 @@ namespace { using PhonemizerOutputTest = JsiTestEnv; // PhonemizerTest pins the JSI contract with no phonemis data. These run the -// en-us pipeline on Kokoro's shipped tagger and lexicon -// (scripts/fetch-test-fixtures.sh), so they pin what TTS is actually fed: how +// en-us, en-gb and zh pipelines on Kokoro's shipped tagger, segmenter and +// lexicons (scripts/fetch-test-fixtures.sh), so they pin what TTS is fed: how // numbers, dates and abbreviations are read out, and which phonemes each input // word is located at, which is what word timings are built from. // @@ -19,6 +19,18 @@ using PhonemizerOutputTest = JsiTestEnv; // sentence sticks to words the lexicon knows; an unknown one would come out // empty here but not in the app. +// A pipeline and its data, relative to RNE_PHONEMIZER_FIXTURE_DIR. +struct Language { + const char *lang; + const char *tagger; + const char *lexicon; +}; + +// en-gb's tagger is the same file as en-us's. zh's tagger slot holds its segmenter. +constexpr Language kEnUs{"en-us", "en-us/tags.json", "en-us/lexicon.json"}; +constexpr Language kEnGb{"en-gb", "en-us/tags.json", "en-gb/lexicon.json"}; +constexpr Language kZh{"zh", "zh/segmenter.json", "zh/lexicon.json"}; + struct Case { const char *text; const char *phonemes; @@ -28,7 +40,7 @@ struct Case { // Phonemizes every case with one phonemizer, since loading the lexicon is // what takes the time. The texts hold no quotes. -void expectPhonemized(JsiTestEnv &env, std::initializer_list cases) { +void expectPhonemized(JsiTestEnv &env, const Language &language, std::initializer_list cases) { std::string texts; for (const auto &c : cases) { texts += std::format("'{}',", c.text); @@ -36,7 +48,7 @@ void expectPhonemized(JsiTestEnv &env, std::initializer_list cases) { // Each result as "phonemes\nwords", separated by '\x1e'. auto results = env.evalString(std::format(R"( const p = __rnexecutorch_jsi__.speech.createPhonemizer({{ - lang: 'en-us', taggerSource: '{}', lexiconSource: '{}' }}); + lang: '{}', taggerSource: '{}/{}', lexiconSource: '{}/{}' }}); return [{}].map((text) => {{ const r = p.phonemize(text, {{ words: true }}); return r.phonemes + '\n' + r.words @@ -44,7 +56,8 @@ void expectPhonemized(JsiTestEnv &env, std::initializer_list cases) { .join('|'); }}).join('\x1e'); )", - RNE_PHONEMIZER_EN_US_TAGGER, RNE_PHONEMIZER_EN_US_LEXICON, texts)); + language.lang, RNE_PHONEMIZER_FIXTURE_DIR, language.tagger, + RNE_PHONEMIZER_FIXTURE_DIR, language.lexicon, texts)); size_t begin = 0; for (const auto &c : cases) { @@ -56,65 +69,76 @@ void expectPhonemized(JsiTestEnv &env, std::initializer_list cases) { } TEST_F(PhonemizerOutputTest, ReadsPlainWordsFromTheLexicon) { - expectPhonemized(*this, {{"Hello world.", "həlˈO wˈɜɹld.", "Hello:həlˈO|world.:wˈɜɹld."}}); + expectPhonemized(*this, kEnUs, {{"Hello world.", "həlˈO wˈɜɹld.", "Hello:həlˈO|world.:wˈɜɹld."}}); } TEST_F(PhonemizerOutputTest, SpellsOutNumbers) { - expectPhonemized(*this, { - {"I have 25 cats.", "ˌI hæv twˈɛntifˈIv kˈæts.", - "I:ˌI|have:hæv|25:twˈɛntifˈIv|cats.:kˈæts."}, - {"It happened in 1999.", "ˌɪt hˈæpənd ɪn nˌIntˈin nˈIndinˈIn.", - "It:ˌɪt|happened:hˈæpənd|in:ɪn|1999.:nˌIntˈin nˈIndinˈIn."}, - {"The 21st century began.", "ðə twˈɛntifˈɜɹst sˈɛnʧəɹi bəɡˈæn.", - "The:ðə|21st:twˈɛntifˈɜɹst|century:sˈɛnʧəɹi|began.:bəɡˈæn."}, - {"Pi is roughly 3.14.", "pˈI ɪz ɹˈʌfli θɹˈi pˈYnt wˈʌn fˈɔɹ.", - "Pi:pˈI|is:ɪz|roughly:ɹˈʌfli|3.14.:θɹˈi pˈYnt wˈʌn fˈɔɹ."}, - {"We sold 1,000,000 copies.", "wˌi sˈOld wˈʌn mˈɪljᵊn kˈɑpiz.", - "We:wˌi|sold:sˈOld|1,000,000:wˈʌn mˈɪljᵊn|copies.:kˈɑpiz."}, - {"Chapter IV starts here.", "ʧˈæptəɹ fˈɔɹ stˈɑɹts hˈɪɹ.", - "Chapter:ʧˈæptəɹ|IV:fˈɔɹ|starts:stˈɑɹts|here.:hˈɪɹ."}, - {"Call 555-1234 now.", "kˈɔl fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ nˈW.", - "Call:kˈɔl|555-1234:fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ|now.:nˈW."}, - }); + expectPhonemized(*this, kEnUs, { + {"I have 25 cats.", "ˌI hæv twˈɛntifˈIv kˈæts.", "I:ˌI|have:hæv|25:twˈɛntifˈIv|cats.:kˈæts."}, + {"It happened in 1999.", "ˌɪt hˈæpənd ɪn nˌIntˈin nˈIndinˈIn.", "It:ˌɪt|happened:hˈæpənd|in:ɪn|1999.:nˌIntˈin nˈIndinˈIn."}, + {"The 21st century began.", "ðə twˈɛntifˈɜɹst sˈɛnʧəɹi bəɡˈæn.", "The:ðə|21st:twˈɛntifˈɜɹst|century:sˈɛnʧəɹi|began.:bəɡˈæn."}, + {"Pi is roughly 3.14.", "pˈI ɪz ɹˈʌfli θɹˈi pˈYnt wˈʌn fˈɔɹ.", "Pi:pˈI|is:ɪz|roughly:ɹˈʌfli|3.14.:θɹˈi pˈYnt wˈʌn fˈɔɹ."}, + {"We sold 1,000,000 copies.", "wˌi sˈOld wˈʌn mˈɪljᵊn kˈɑpiz.", "We:wˌi|sold:sˈOld|1,000,000:wˈʌn mˈɪljᵊn|copies.:kˈɑpiz."}, + {"Chapter IV starts here.", "ʧˈæptəɹ fˈɔɹ stˈɑɹts hˈɪɹ.", "Chapter:ʧˈæptəɹ|IV:fˈɔɹ|starts:stˈɑɹts|here.:hˈɪɹ."}, + {"Call 555-1234 now.", "kˈɔl fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ nˈW.", "Call:kˈɔl|555-1234:fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ|now.:nˈW."}, + }); } TEST_F(PhonemizerOutputTest, ReadsMoneyAndPercentages) { - expectPhonemized(*this, { - {"It costs $4.50 today.", "ˌɪt kˈɔsts fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts tədˈA.", - "It:ˌɪt|costs:kˈɔsts|$4.50:fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts|today.:tədˈA."}, - {"About 50% of people agree.", "əbˈWt fˈɪfti pəɹsˈɛnt ʌv pˈipᵊl əɡɹˈi.", - "About:əbˈWt|50%:fˈɪfti pəɹsˈɛnt|of:ʌv|people:pˈipᵊl|agree.:əɡɹˈi."}, - }); + expectPhonemized(*this, kEnUs, { + {"It costs $4.50 today.", "ˌɪt kˈɔsts fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts tədˈA.", "It:ˌɪt|costs:kˈɔsts|$4.50:fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts|today.:tədˈA."}, + {"About 50% of people agree.", "əbˈWt fˈɪfti pəɹsˈɛnt ʌv pˈipᵊl əɡɹˈi.", "About:əbˈWt|50%:fˈɪfti pəɹsˈɛnt|of:ʌv|people:pˈipᵊl|agree.:əɡɹˈi."}, + }); } TEST_F(PhonemizerOutputTest, ReadsDates) { - expectPhonemized(*this, { - {"On 12/25/2024 we left.", "ˈɔn disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ wi lˈɛft.", - "On:ˈɔn|12/25/2024:disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ|we:wi|left.:lˈɛft."}, - {"Born on May 5, 2024.", "bˈɔɹn ˌɔn mˈA fˈɪfθ, twˈɛnti twˈɛntifˈɔɹ.", - "Born:bˈɔɹn|on:ˌɔn|May:mˈA|5,:fˈɪfθ,|2024.:twˈɛnti twˈɛntifˈɔɹ."}, - }); + expectPhonemized(*this, kEnUs, { + {"On 12/25/2024 we left.", "ˈɔn disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ wi lˈɛft.", "On:ˈɔn|12/25/2024:disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ|we:wi|left.:lˈɛft."}, + {"Born on May 5, 2024.", "bˈɔɹn ˌɔn mˈA fˈɪfθ, twˈɛnti twˈɛntifˈɔɹ.", "Born:bˈɔɹn|on:ˌɔn|May:mˈA|5,:fˈɪfθ,|2024.:twˈɛnti twˈɛntifˈɔɹ."}, + }); } TEST_F(PhonemizerOutputTest, ExpandsAbbreviationsAndSymbols) { - expectPhonemized(*this, { - {"Dr. Smith lives on Main St.", "dˈɑktəɹ smˈɪθ lˈIvz ˌɔn mˈAn stɹˈit.", - "Dr.:dˈɑktəɹ|Smith:smˈɪθ|lives:lˈIvz|on:ˌɔn|Main:mˈAn|St.:stɹˈit."}, - {"Bring fruit, e.g. apples.", "bɹˈɪŋ fɹˈut, fɔɹ ɪɡzˈæmpəl ˈæpᵊlz.", - "Bring:bɹˈɪŋ|fruit,:fɹˈut,|e.g.:fɔɹ ɪɡzˈæmpəl|apples.:ˈæpᵊlz."}, - {"Rock & roll.", "ɹˈɑk ænd ɹˈOl.", "Rock:ɹˈɑk|&:ænd|roll.:ɹˈOl."}, - }); + expectPhonemized(*this, kEnUs, { + {"Dr. Smith lives on Main St.", "dˈɑktəɹ smˈɪθ lˈIvz ˌɔn mˈAn stɹˈit.", "Dr.:dˈɑktəɹ|Smith:smˈɪθ|lives:lˈIvz|on:ˌɔn|Main:mˈAn|St.:stɹˈit."}, + {"Bring fruit, e.g. apples.", "bɹˈɪŋ fɹˈut, fɔɹ ɪɡzˈæmpəl ˈæpᵊlz.", "Bring:bɹˈɪŋ|fruit,:fɹˈut,|e.g.:fɔɹ ɪɡzˈæmpəl|apples.:ˈæpᵊlz."}, + {"Rock & roll.", "ɹˈɑk ænd ɹˈOl.", "Rock:ɹˈɑk|&:ænd|roll.:ɹˈOl."}, + }); } // A time or a measure is read as one phrase, and comes back as one word that // spans both input words, so their word timings are shared. TEST_F(PhonemizerOutputTest, LocatesAPhraseReadAsOneUnitAsOneWord) { - expectPhonemized(*this, { - {"Meet me at 3:30 pm.", "mˈit mˌi æt θɹˈi θˈɜɹɾi pˈiˈɛm.", - "Meet:mˈit|me:mˌi|at:æt|3:30 pm.:θɹˈi θˈɜɹɾi pˈiˈɛm."}, - {"She ran 5 km.", "ʃˌi ɹˈæn fˈIv kəlˈɑməɾəɹz.", - "She:ʃˌi|ran:ɹˈæn|5 km.:fˈIv kəlˈɑməɾəɹz."}, - }); + expectPhonemized(*this, kEnUs, { + {"Meet me at 3:30 pm.", "mˈit mˌi æt θɹˈi θˈɜɹɾi pˈiˈɛm.", "Meet:mˈit|me:mˌi|at:æt|3:30 pm.:θɹˈi θˈɜɹɾi pˈiˈɛm."}, + {"She ran 5 km.", "ʃˌi ɹˈæn fˈIv kəlˈɑməɾəɹz.", "She:ʃˌi|ran:ɹˈæn|5 km.:fˈIv kəlˈɑməɾəɹz."}, + }); +} + +TEST_F(PhonemizerOutputTest, ReadsBritishEnglish) { + expectPhonemized(*this, kEnGb, { + {"It costs £4.50 today.", "ˌɪt kˈɒsts fˈɔː pˈWndz and fˈɪfti pˈɛns tədˈA.", "It:ˌɪt|costs:kˈɒsts|£4.50:fˈɔː pˈWndz and fˈɪfti pˈɛns|today.:tədˈA."}, + {"On 24/12/2024 we left.", "ˈɒn ðə twˈɛntifˈɔːθ ɒv dɪsˈɛmbə twˈɛnti twˈɛntifˈɔː wiː lˈɛft.", "On:ˈɒn|24/12/2024:ðə twˈɛntifˈɔːθ ɒv dɪsˈɛmbə twˈɛnti twˈɛntifˈɔː|we:wiː|left.:lˈɛft."}, + {"The 21st century.", "ðə twˈɛntifˈɜːst sˈɛnʧʊɹi.", "The:ðə|21st:twˈɛntifˈɜːst|century.:sˈɛnʧʊɹi."}, + {"About 50% agree.", "əbˈWt fˈɪfti pəsˈɛnt əɡɹˈiː.", "About:əbˈWt|50%:fˈɪfti pəsˈɛnt|agree.:əɡɹˈiː."}, + {"Meet me at 3:30 pm.", "mˈiːt mˌiː at θɹˈiː θˈɜːti pˈiːˈɛm.", "Meet:mˈiːt|me:mˌiː|at:at|3:30 pm.:θɹˈiː θˈɜːti pˈiːˈɛm."}, + {"Dr. Smith ran 5 km.", "dˈɒktə smˈɪθ ɹˈan fˈIv kˈɪləmˌiːtəz.", "Dr.:dˈɒktə|Smith:smˈɪθ|ran:ɹˈan|5 km.:fˈIv kˈɪləmˌiːtəz."}, + // Wrong: 946 should be read digit by digit (Verbalis#8). + {"Call 0207 946 0018 now.", "kˈɔːl zˈɪəɹQ tˈuː zˈɪəɹQ sˈɛvᵊn nˈIn hˈʌndɹəd fˈɔːtisˈɪks zˈɪəɹQ zˈɪəɹQ wˈʌn ˈAt nˈW.", "Call:kˈɔːl|0207:zˈɪəɹQ tˈuː zˈɪəɹQ sˈɛvᵊn|946:nˈIn hˈʌndɹəd fˈɔːtisˈɪks|0018:zˈɪəɹQ zˈɪəɹQ wˈʌn ˈAt|now.:nˈW."}, + }); +} + +// zh segments the text into words, so a located word can span several characters. +TEST_F(PhonemizerOutputTest, ReadsChinese) { + expectPhonemized(*this, kZh, { + {"我有25只猫。", "wo↓ jou↓ ɚ↘ʂɨ↗u↓ ꭧɨ↓ mau→.", "我:wo↓|有:jou↓|25:ɚ↘ʂɨ↗u↓|只:ꭧɨ↓|猫。:mau→."}, + {"价格是4.50元。", "ʨja↘kɤ↗ ʂɨ↘ sɨ↘tjɛ↓n u↓li↗ŋ ɥɛ↗n.", "价格:ʨja↘kɤ↗|是:ʂɨ↘|4.50:sɨ↘tjɛ↓n u↓li↗ŋ|元。:ɥɛ↗n."}, + {"2024年12月24日我们走了。", "ɚ↘li↗ŋɚ↘ sɨ↘njɛ↗n ʂɨ↗ɚ↘ɥe↘ ɚ↘ʂɨ↗sɨ↘ɻɨ↘ wo↓mən ʦou↓ lɤ.", "2024年12月24日:ɚ↘li↗ŋɚ↘ sɨ↘njɛ↗n ʂɨ↗ɚ↘ɥe↘ ɚ↘ʂɨ↗sɨ↘ɻɨ↘|我们:wo↓mən|走:ʦou↓|了。:lɤ."}, + {"下午3:30见。", "ɕja↘u↓ sa→ntjɛ↓n sa→nʂɨ↗fə→n ʨjɛ↘n.", "下午:ɕja↘u↓|3:30:sa→ntjɛ↓n sa→nʂɨ↗fə→n|见。:ʨjɛ↘n."}, + {"第3天。", "ti↘sa→ntʰjɛ→n.", "第3天。:ti↘sa→ntʰjɛ→n."}, + {"大约50%的人同意。", "ta↘ɥe→ pai↓fə→nꭧɨ→u↓ʂɨ↗ tɤ ɻə↗n tʰʊ↗ŋi↘.", "大约:ta↘ɥe→|50%:pai↓fə→nꭧɨ→u↓ʂɨ↗|的:tɤ|人:ɻə↗n|同意。:tʰʊ↗ŋi↘."}, + {"1999年。", "i→ʨjou↓ʨjou↓ʨjou↓njɛ↗n.", "1999年。:i→ʨjou↓ʨjou↓ʨjou↓njɛ↗n."}, + }); } } // namespace diff --git a/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp b/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp new file mode 100644 index 0000000000..e0eb296bfa --- /dev/null +++ b/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp @@ -0,0 +1,125 @@ +#include +#include + +#include + +#include +#include + +namespace rnexecutorch::tests { +namespace { + +using phonemis::utils::conversions::u32_to_utf8; +using phonemis::utils::conversions::utf8_to_u32; + +// These languages are phonemized by a neural model, which these tests cannot +// run, so PhonemizerOutputTest covers only en-us, en-gb and zh. What phonemis +// feeds the model is still pinned here: the text after preprocessing (Verbalis), +// which reads numbers, dates, money, times and abbreviations out as words. It +// needs no data, so this goes below the JSI layer, to phonemis directly. + +struct Case { + const char *text; + const char *preprocessed; +}; + +void expectPreprocessed(const char *lang, std::initializer_list cases) { + phonemis::Pipeline pipeline(phonemis::Config{.lang = lang}); + for (const auto &c : cases) { + SCOPED_TRACE(c.text); + EXPECT_EQ(u32_to_utf8(pipeline.preprocess(utf8_to_u32(c.text))), c.preprocessed); + } +} +TEST(PreprocessingTest, ReadsGerman) { + expectPreprocessed("de", { + {"Ich habe 25 Katzen.", "Ich habe fünfundzwanzig Katzen."}, + {"Es kostet 4,50 €.", "Es kostet vier Euro fünfzig."}, + {"Am 24.12.2024 fuhren wir.", "Am vierundzwanzigsten Dezember zweitausendvierundzwanzig fuhren wir."}, + {"Um 15:30 Uhr.", "Um fünfzehn Uhr dreißig."}, + {"Der 3. Mai.", "Der dritte Mai."}, + {"Dr. Müller lief z. B. 3,5 km.", "Doktor Müller lief zum Beispiel drei Komma fünf Kilometer."}, + {"Etwa 50 % stimmen zu.", "Etwa fünfzig Prozent stimmen zu."}, + {"Im Jahr 1999.", "Im Jahr neunzehnhundertneunundneunzig."}, + }); +} + +TEST(PreprocessingTest, ReadsFrench) { + expectPreprocessed("fr", { + {"J'ai 25 chats.", "J'ai vingt-cinq chats."}, + {"Ça coûte 4,50 €.", "Ça coûte quatre euros cinquante."}, + {"Le 24/12/2024 nous sommes partis.", "Le vingt-quatre décembre deux mille vingt-quatre nous sommes partis."}, + {"À 15h30.", "À quinze heures trente."}, + {"Le 3e jour.", "Le troisième jour."}, + {"M. Dupont a couru 3,5 km.", "monsieur Dupont a couru trois virgule cinq kilomètres."}, + {"Environ 50 % sont d'accord.", "Environ cinquante pour cent sont d'accord."}, + {"En 1999.", "En mille neuf cent quatre-vingt-dix-neuf."}, + }); +} + +TEST(PreprocessingTest, ReadsSpanish) { + expectPreprocessed("es", { + {"Tengo 25 gatos.", "Tengo veinticinco gatos."}, + {"Cuesta 4,50 €.", "Cuesta cuatro euros con cincuenta."}, + {"El 24/12/2024 salimos.", "El veinticuatro de diciembre de dos mil veinticuatro salimos."}, + {"A las 15:30.", "A las quince y media."}, + // Wrong: should be "tercer" (Verbalis#8). + {"El 3.º día.", "El tercero día."}, + {"El Sr. García corrió 3,5 km.", "El señor García corrió tres coma cinco kilómetros."}, + {"Un 50 % está de acuerdo.", "Un cincuenta por ciento está de acuerdo."}, + {"En 1999.", "En mil novecientos noventa y nueve."}, + }); +} + +TEST(PreprocessingTest, ReadsItalian) { + expectPreprocessed("it", { + {"Ho 25 gatti.", "Ho venticinque gatti."}, + {"Costa 4,50 €.", "Costa quattro euro e cinquanta centesimi."}, + {"Il 24/12/2024 siamo partiti.", "Il ventiquattro dicembre duemilaventiquattro siamo partiti."}, + {"Alle 15:30.", "Alle quindici e trenta."}, + {"Il 3° giorno.", "Il terzo giorno."}, + {"Il Sig. Rossi ha corso 3,5 km.", "Il signor Rossi ha corso tre virgola cinque chilometri."}, + {"Il 50% è d'accordo.", "Il cinquanta per cento è d'accordo."}, + {"Nel 1999.", "Nel millenovecentonovantanove."}, + }); +} + +TEST(PreprocessingTest, ReadsPolish) { + expectPreprocessed("pl", { + {"Mam 25 kotów.", "Mam dwadzieścia pięć kotów."}, + {"Kosztuje 4,50 zł.", "Kosztuje cztery złote pięćdziesiąt groszy."}, + {"Dnia 24.12.2024 wyjechaliśmy.", "Dnia dwudziestego czwartego grudnia dwa tysiące dwudziestego czwartego wyjechaliśmy."}, + {"O 15:30.", "O piętnastej trzydzieści."}, + {"3 maja.", "trzeciego maja."}, + {"Dr Kowalski przebiegł np. 3,5 km.", "Doktor Kowalski przebiegł na przykład trzy i pół kilometra."}, + {"Około 50% się zgadza.", "Około pięćdziesięciu procent się zgadza."}, + {"W 1999 roku.", "W tysiąc dziewięćset dziewięćdziesiątym dziewiątym roku."}, + }); +} + +TEST(PreprocessingTest, ReadsPortuguese) { + expectPreprocessed("pt", { + {"Tenho 25 gatos.", "Tenho vinte e cinco gatos."}, + {"Custa R$ 4,50.", "Custa quatro reais e cinquenta centavos."}, + {"Em 24/12/2024 partimos.", "Em vinte e quatro de dezembro de dois mil e vinte e quatro partimos."}, + {"Às 15h30.", "Às quinze horas e trinta."}, + {"O 3.º dia.", "O terceiro dia."}, + // Wrong: "Sr." should be "senhor" (Verbalis#8). + {"O Sr. Silva correu 3,5 km.", "O Sr. Silva correu três vírgula cinco quilômetros."}, + {"Cerca de 50% concordam.", "Cerca de cinquenta por cento concordam."}, + {"Em 1999.", "Em mil novecentos e noventa e nove."}, + }); +} + +TEST(PreprocessingTest, ReadsHindi) { + expectPreprocessed("hi", { + {"मेरे पास 25 बिल्लियाँ हैं।", "मेरे पास पच्चीस बिल्लियाँ हैं।"}, + {"इसकी कीमत ₹4.50 है।", "इसकी कीमत चार रुपये पचास पैसे है।"}, + {"24/12/2024 को हम गए।", "चौबीस दिसंबर दो हज़ार चौबीस को हम गए।"}, + // Wrong: says बजे twice (Verbalis#8). + {"3:30 बजे।", "तीन बजकर तीस मिनट बजे।"}, + {"50% लोग सहमत हैं।", "पचास प्रतिशत लोग सहमत हैं।"}, + {"1999 में।", "एक हज़ार नौ सौ निन्यानवे में।"}, + }); +} +} // namespace +} // namespace rnexecutorch::tests diff --git a/packages/react-native-executorch/scripts/fetch-test-fixtures.sh b/packages/react-native-executorch/scripts/fetch-test-fixtures.sh index 222d07eaa4..238d47e03c 100755 --- a/packages/react-native-executorch/scripts/fetch-test-fixtures.sh +++ b/packages/react-native-executorch/scripts/fetch-test-fixtures.sh @@ -12,9 +12,11 @@ # * A HuggingFace tokenizer.json, which the nlp extension loads through # ExecuTorch's HFTokenizer. Whisper tiny.en's is a plain BPE vocabulary, # ~2.4 MB. -# * Kokoro's en-us phonemizer data, the tagger and lexicon the app ships -# (~8.4 MB together). With them phonemis produces real phonemes, so the -# phonemizer's text normalization and word locating are pinned end to end. +# * Kokoro's phonemizer data for the languages it phonemizes without a +# neural model: the en-us tagger (en-gb's is the same file), the en-us and +# en-gb lexicons, and the zh segmenter and lexicon (~26 MB together). With +# them phonemis produces real phonemes, so the phonemizer's text +# normalization and word locating are pinned end to end. # # All are pinned to an exact Hugging Face revision and verified against a # recorded sha256, so a re-tag upstream cannot silently change what the tests @@ -92,3 +94,24 @@ fetch_fixture \ "4836d50e0fa48d7cdd415210f6c2c0e1acba5acb" \ "phonemizer/en-us/lexicon.json" \ "b29e9c13190cf211979e8d9c38258cc1cb8e701e5ca1102a0c1f00acd4f55f8e" + +fetch_fixture \ + "phonemizer/en-gb/lexicon.json" \ + "software-mansion/Kokoro-82M-ExecuTorch" \ + "4836d50e0fa48d7cdd415210f6c2c0e1acba5acb" \ + "phonemizer/en-gb/lexicon.json" \ + "f4ad98b7725c681be79df533288e20df71db40a89d0cfdead2192f0d61449399" + +fetch_fixture \ + "phonemizer/zh/segmenter.json" \ + "software-mansion/Kokoro-82M-ExecuTorch" \ + "4836d50e0fa48d7cdd415210f6c2c0e1acba5acb" \ + "phonemizer/zh/segmenter.json" \ + "4079bc04a9028e7fb0e7b015a327b5c65301feb3069d4ba391ff823fa357c200" + +fetch_fixture \ + "phonemizer/zh/lexicon.json" \ + "software-mansion/Kokoro-82M-ExecuTorch" \ + "4836d50e0fa48d7cdd415210f6c2c0e1acba5acb" \ + "phonemizer/zh/lexicon.json" \ + "f43304c9c2426545f8491c89ba235bab9e5bd740fdd42c4eeaca6df216f20735" From 19fa161caba8bd4c09000241d4e04a14e5fdc732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C5=82uszniak?= Date: Wed, 4 Nov 2026 15:11:25 +0100 Subject: [PATCH 6/6] Move phonemis readings into snapshots, and keep invariants as tests A phonemis or Verbalis bump that changes a reading now fails with the old and new value and is accepted with RNE_UPDATE_SNAPSHOTS=1, so the PR shows the diff. LeavesNoDigitsOrSymbols and LocatesEveryWordOfTheInput hold across versions and catch real regressions. --- .../cpp/tests/CMakeLists.txt | 7 +- .../cpp/tests/README.md | 24 +- .../tests/extensions/PhonemizerOutputTest.cpp | 224 ++++++++++-------- .../tests/extensions/PreprocessingTest.cpp | 137 +++-------- .../cpp/tests/snapshots/phonemizer.txt | 133 +++++++++++ .../cpp/tests/snapshots/preprocessing.txt | 121 ++++++++++ .../cpp/tests/support/Snapshots.cpp | 107 +++++++++ .../cpp/tests/support/Snapshots.h | 41 ++++ 8 files changed, 587 insertions(+), 207 deletions(-) create mode 100644 packages/react-native-executorch/cpp/tests/snapshots/phonemizer.txt create mode 100644 packages/react-native-executorch/cpp/tests/snapshots/preprocessing.txt create mode 100644 packages/react-native-executorch/cpp/tests/support/Snapshots.cpp create mode 100644 packages/react-native-executorch/cpp/tests/support/Snapshots.h diff --git a/packages/react-native-executorch/cpp/tests/CMakeLists.txt b/packages/react-native-executorch/cpp/tests/CMakeLists.txt index ffa2f821c8..36cbcd6b29 100644 --- a/packages/react-native-executorch/cpp/tests/CMakeLists.txt +++ b/packages/react-native-executorch/cpp/tests/CMakeLists.txt @@ -284,7 +284,7 @@ if(RNE_TESTS_ENABLE_OPENCV) endif() # --- Test support ----------------------------------------------------------- -add_library(rne_test_support STATIC support/JsiTestEnv.cpp) +add_library(rne_test_support STATIC support/JsiTestEnv.cpp support/Snapshots.cpp) target_link_libraries(rne_test_support PUBLIC rne_under_test gtest gmock) target_include_directories(rne_test_support PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") @@ -294,6 +294,11 @@ target_include_directories(rne_test_support PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}" target_compile_definitions(rne_test_support PUBLIC RNE_ERROR_TS_SOURCE="${PACKAGE_DIR}/src/core/error.ts") +# Snapshots of what third-party code produces, committed and updated with +# RNE_UPDATE_SNAPSHOTS=1 (support/Snapshots.h). +target_compile_definitions(rne_test_support PUBLIC + RNE_SNAPSHOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}/snapshots") + # --- Model fixture ---------------------------------------------------------- # Suites that read ExecuTorch MethodMeta need a real .pte. It is downloaded # rather than committed (scripts/fetch-test-fixtures.sh), so the suites that diff --git a/packages/react-native-executorch/cpp/tests/README.md b/packages/react-native-executorch/cpp/tests/README.md index efb36e2727..10416c19a5 100644 --- a/packages/react-native-executorch/cpp/tests/README.md +++ b/packages/react-native-executorch/cpp/tests/README.md @@ -46,6 +46,7 @@ rather than a lookalike. | `core/` | `dtype`, `error`, `conversions`, `tensor`, `schema`, `model`, `utils` | | `extensions/` | `math`, `speech`, `nlp` (tokenizer), `llm`, and — behind OpenCV — `cv` and `ocr` ops; `phonemizer` behind phonemis | | `fixtures/` | Downloaded `.pte` program, `tokenizer.json` and phonemizer data (gitignored) | +| `snapshots/` | Committed outputs of phonemis, see below | One binary per suite, so a crash in one area cannot take the run down with it and `ctest -R MathOpsTest` targets a single suite. @@ -122,10 +123,25 @@ automatically; they land in `fixtures/` and are gitignored rather than committed - A **tokenizer.json** — Whisper tiny.en's, ~2.4 MB. A plain BPE vocabulary, so the nlp extension's encode/decode path runs for real. - Kokoro's **phonemizer data** for en-us, en-gb and zh, the tagger, segmenter - and lexicons the app ships, ~26 MB together. `PhonemizerOutputTest` pins the - phonemes and word locations phonemis produces with them. The other languages - need a neural model, so `PreprocessingTest` pins only the text phonemis feeds - it, which needs no data. + and lexicons the app ships, ~26 MB together. `PhonemizerOutputTest` runs + phonemis on them. The other languages need a neural model, so + `PreprocessingTest` covers only the text phonemis feeds it. + +## Snapshots + +What phonemis reads a text as changes with every phonemis or Verbalis bump, so +those readings are not hard-coded in the tests. `snapshots/*.txt` lists the +inputs and their committed outputs; a bump that changes one fails +`MatchesTheSnapshot` with the old and new value. Accept the change and review +the diff in the PR: + +```bash +RNE_UPDATE_SNAPSHOTS=1 scripts/run-native-tests.sh -R 'PhonemizerOutput|Preprocessing' +``` + +To add a case, add its ` | ` line and run the same command. The +`LeavesNoDigitsOrSymbols` and `LocatesEveryWordOfTheInput` checks hold across +versions and have no snapshot: a failure there is a real regression. The useful part is that this needs **no XNNPACK delegate**, even though the fixture is XNNPACK-delegated. `ModelHostObject`'s constructor only calls diff --git a/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp b/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp index a306aa529d..a036cedb31 100644 --- a/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp +++ b/packages/react-native-executorch/cpp/tests/extensions/PhonemizerOutputTest.cpp @@ -1,144 +1,168 @@ #include -#include +#include +#include #include +#include + +#include +#include #include "support/JsiTestEnv.h" +#include "support/Snapshots.h" namespace rnexecutorch::tests { namespace { using PhonemizerOutputTest = JsiTestEnv; +using phonemis::utils::conversions::u32_to_utf8; +using phonemis::utils::conversions::utf8_to_u32; // PhonemizerTest pins the JSI contract with no phonemis data. These run the // en-us, en-gb and zh pipelines on Kokoro's shipped tagger, segmenter and -// lexicons (scripts/fetch-test-fixtures.sh), so they pin what TTS is fed: how -// numbers, dates and abbreviations are read out, and which phonemes each input -// word is located at, which is what word timings are built from. -// -// The neural fallback is not loaded (it needs ExecuTorch execution), so every -// sentence sticks to words the lexicon knows; an unknown one would come out -// empty here but not in the app. - -// A pipeline and its data, relative to RNE_PHONEMIZER_FIXTURE_DIR. -struct Language { - const char *lang; +// lexicons (scripts/fetch-test-fixtures.sh), so they pin what TTS is fed: the +// phonemes, and which of them each input word is located at, which is what +// word timings are built from. + +const std::string kSnapshot = std::string(RNE_SNAPSHOT_DIR) + "/phonemizer.txt"; + +// A pipeline's data, relative to RNE_PHONEMIZER_FIXTURE_DIR. en-gb's tagger is +// the same file as en-us's; zh's tagger slot holds its segmenter. +struct Data { const char *tagger; const char *lexicon; }; -// en-gb's tagger is the same file as en-us's. zh's tagger slot holds its segmenter. -constexpr Language kEnUs{"en-us", "en-us/tags.json", "en-us/lexicon.json"}; -constexpr Language kEnGb{"en-gb", "en-us/tags.json", "en-gb/lexicon.json"}; -constexpr Language kZh{"zh", "zh/segmenter.json", "zh/lexicon.json"}; +const std::map kData = { + {"en-us", {"en-us/tags.json", "en-us/lexicon.json"}}, + {"en-gb", {"en-us/tags.json", "en-gb/lexicon.json"}}, + {"zh", {"zh/segmenter.json", "zh/lexicon.json"}}, +}; -struct Case { - const char *text; - const char *phonemes; +struct Phonemized { + std::string phonemes; // Each located word as "text:phonemes", joined with '|'. - const char *words; + std::string words; + // What breaks the invariants of LocatesEveryWordOfTheInput, if anything. + std::string problems; }; -// Phonemizes every case with one phonemizer, since loading the lexicon is -// what takes the time. The texts hold no quotes. -void expectPhonemized(JsiTestEnv &env, const Language &language, std::initializer_list cases) { - std::string texts; - for (const auto &c : cases) { - texts += std::format("'{}',", c.text); +// `s` as a JS string literal. +std::string jsString(const std::string &s) { + std::string out = "'"; + for (char ch : s) { + if (ch == '\\' || ch == '\'') { + out += '\\'; + } + out += ch; } - // Each result as "phonemes\nwords", separated by '\x1e'. + return out + "'"; +} + +// Phonemizes every text with one phonemizer, since loading the lexicon is what +// takes the time. +std::vector phonemize(JsiTestEnv &env, const std::string &lang, const std::vector &texts) { + auto data = kData.find(lang); + if (data == kData.end()) { + throw std::invalid_argument("no phonemizer data for " + lang); + } + std::string list; + for (const auto &text : texts) { + list += jsString(text) + ","; + } + // Each text as "phonemes\x1fwords\x1fproblems", separated by '\x1e'. auto results = env.evalString(std::format(R"( const p = __rnexecutorch_jsi__.speech.createPhonemizer({{ lang: '{}', taggerSource: '{}/{}', lexiconSource: '{}/{}' }}); return [{}].map((text) => {{ const r = p.phonemize(text, {{ words: true }}); - return r.phonemes + '\n' + r.words + const problems = []; + if (r.phonemes.length === 0) problems.push('no phonemes'); + if (r.words.length === 0) problems.push('no words located'); + let end = 0, phonemeEnd = 0; + for (const w of r.words) {{ + if (text.substr(w.offset, w.text.length) !== w.text) problems.push(`'${{w.text}}' is not at ${{w.offset}}`); + if (w.offset < end) problems.push(`'${{w.text}}' overlaps the word before`); + if (w.phonemeLength === 0) problems.push(`'${{w.text}}' has no phonemes`); + if (w.phonemeOffset < phonemeEnd) problems.push(`'${{w.text}}' phonemes overlap the word before`); + if (w.phonemeOffset + w.phonemeLength > r.phonemes.length) problems.push(`'${{w.text}}' phonemes run past the end`); + end = w.offset + w.text.length; + phonemeEnd = w.phonemeOffset + w.phonemeLength; + }} + for (let i = 0; i < text.length; ++i) {{ + if (/\s/.test(text[i])) continue; + if (!r.words.some((w) => w.offset <= i && i < w.offset + w.text.length)) {{ + problems.push(`'${{text[i]}}' at ${{i}} is in no word`); + break; + }} + }} + const words = r.words .map((w) => w.text + ':' + r.phonemes.substr(w.phonemeOffset, w.phonemeLength)) .join('|'); + return [r.phonemes, words, problems.join('; ')].join('\x1f'); }}).join('\x1e'); )", - language.lang, RNE_PHONEMIZER_FIXTURE_DIR, language.tagger, - RNE_PHONEMIZER_FIXTURE_DIR, language.lexicon, texts)); + lang, RNE_PHONEMIZER_FIXTURE_DIR, data->second.tagger, + RNE_PHONEMIZER_FIXTURE_DIR, data->second.lexicon, list)); + std::vector out; size_t begin = 0; - for (const auto &c : cases) { - SCOPED_TRACE(c.text); + for (size_t i = 0; i < texts.size(); ++i) { auto end = results.find('\x1e', begin); - EXPECT_EQ(results.substr(begin, end - begin), std::string(c.phonemes) + "\n" + c.words); + auto record = results.substr(begin, end - begin); + auto first = record.find('\x1f'); + auto second = record.find('\x1f', first + 1); + out.push_back({record.substr(0, first), record.substr(first + 1, second - first - 1), + record.substr(second + 1)}); begin = end == std::string::npos ? results.size() : end + 1; } + return out; } -TEST_F(PhonemizerOutputTest, ReadsPlainWordsFromTheLexicon) { - expectPhonemized(*this, kEnUs, {{"Hello world.", "həlˈO wˈɜɹld.", "Hello:həlˈO|world.:wˈɜɹld."}}); -} - -TEST_F(PhonemizerOutputTest, SpellsOutNumbers) { - expectPhonemized(*this, kEnUs, { - {"I have 25 cats.", "ˌI hæv twˈɛntifˈIv kˈæts.", "I:ˌI|have:hæv|25:twˈɛntifˈIv|cats.:kˈæts."}, - {"It happened in 1999.", "ˌɪt hˈæpənd ɪn nˌIntˈin nˈIndinˈIn.", "It:ˌɪt|happened:hˈæpənd|in:ɪn|1999.:nˌIntˈin nˈIndinˈIn."}, - {"The 21st century began.", "ðə twˈɛntifˈɜɹst sˈɛnʧəɹi bəɡˈæn.", "The:ðə|21st:twˈɛntifˈɜɹst|century:sˈɛnʧəɹi|began.:bəɡˈæn."}, - {"Pi is roughly 3.14.", "pˈI ɪz ɹˈʌfli θɹˈi pˈYnt wˈʌn fˈɔɹ.", "Pi:pˈI|is:ɪz|roughly:ɹˈʌfli|3.14.:θɹˈi pˈYnt wˈʌn fˈɔɹ."}, - {"We sold 1,000,000 copies.", "wˌi sˈOld wˈʌn mˈɪljᵊn kˈɑpiz.", "We:wˌi|sold:sˈOld|1,000,000:wˈʌn mˈɪljᵊn|copies.:kˈɑpiz."}, - {"Chapter IV starts here.", "ʧˈæptəɹ fˈɔɹ stˈɑɹts hˈɪɹ.", "Chapter:ʧˈæptəɹ|IV:fˈɔɹ|starts:stˈɑɹts|here.:hˈɪɹ."}, - {"Call 555-1234 now.", "kˈɔl fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ nˈW.", "Call:kˈɔl|555-1234:fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ|now.:nˈW."}, - }); -} - -TEST_F(PhonemizerOutputTest, ReadsMoneyAndPercentages) { - expectPhonemized(*this, kEnUs, { - {"It costs $4.50 today.", "ˌɪt kˈɔsts fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts tədˈA.", "It:ˌɪt|costs:kˈɔsts|$4.50:fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts|today.:tədˈA."}, - {"About 50% of people agree.", "əbˈWt fˈɪfti pəɹsˈɛnt ʌv pˈipᵊl əɡɹˈi.", "About:əbˈWt|50%:fˈɪfti pəɹsˈɛnt|of:ʌv|people:pˈipᵊl|agree.:əɡɹˈi."}, - }); -} - -TEST_F(PhonemizerOutputTest, ReadsDates) { - expectPhonemized(*this, kEnUs, { - {"On 12/25/2024 we left.", "ˈɔn disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ wi lˈɛft.", "On:ˈɔn|12/25/2024:disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ|we:wi|left.:lˈɛft."}, - {"Born on May 5, 2024.", "bˈɔɹn ˌɔn mˈA fˈɪfθ, twˈɛnti twˈɛntifˈɔɹ.", "Born:bˈɔɹn|on:ˌɔn|May:mˈA|5,:fˈɪfθ,|2024.:twˈɛnti twˈɛntifˈɔɹ."}, - }); -} - -TEST_F(PhonemizerOutputTest, ExpandsAbbreviationsAndSymbols) { - expectPhonemized(*this, kEnUs, { - {"Dr. Smith lives on Main St.", "dˈɑktəɹ smˈɪθ lˈIvz ˌɔn mˈAn stɹˈit.", "Dr.:dˈɑktəɹ|Smith:smˈɪθ|lives:lˈIvz|on:ˌɔn|Main:mˈAn|St.:stɹˈit."}, - {"Bring fruit, e.g. apples.", "bɹˈɪŋ fɹˈut, fɔɹ ɪɡzˈæmpəl ˈæpᵊlz.", "Bring:bɹˈɪŋ|fruit,:fɹˈut,|e.g.:fɔɹ ɪɡzˈæmpəl|apples.:ˈæpᵊlz."}, - {"Rock & roll.", "ɹˈɑk ænd ɹˈOl.", "Rock:ɹˈɑk|&:ænd|roll.:ɹˈOl."}, - }); -} - -// A time or a measure is read as one phrase, and comes back as one word that -// spans both input words, so their word timings are shared. -TEST_F(PhonemizerOutputTest, LocatesAPhraseReadAsOneUnitAsOneWord) { - expectPhonemized(*this, kEnUs, { - {"Meet me at 3:30 pm.", "mˈit mˌi æt θɹˈi θˈɜɹɾi pˈiˈɛm.", "Meet:mˈit|me:mˌi|at:æt|3:30 pm.:θɹˈi θˈɜɹɾi pˈiˈɛm."}, - {"She ran 5 km.", "ʃˌi ɹˈæn fˈIv kəlˈɑməɾəɹz.", "She:ʃˌi|ran:ɹˈæn|5 km.:fˈIv kəlˈɑməɾəɹz."}, - }); +// Every case of the snapshot, phonemized, in order: one phonemizer per language. +std::vector phonemizeAll(JsiTestEnv &env, const std::vector &cases) { + std::map> byLang; + for (size_t i = 0; i < cases.size(); ++i) { + byLang[cases[i].lang].push_back(i); + } + std::vector out(cases.size()); + for (const auto &[lang, indices] : byLang) { + std::vector texts; + for (auto i : indices) { + texts.push_back(cases[i].input); + } + auto results = phonemize(env, lang, texts); + for (size_t j = 0; j < indices.size(); ++j) { + out[indices[j]] = std::move(results[j]); + } + } + return out; } -TEST_F(PhonemizerOutputTest, ReadsBritishEnglish) { - expectPhonemized(*this, kEnGb, { - {"It costs £4.50 today.", "ˌɪt kˈɒsts fˈɔː pˈWndz and fˈɪfti pˈɛns tədˈA.", "It:ˌɪt|costs:kˈɒsts|£4.50:fˈɔː pˈWndz and fˈɪfti pˈɛns|today.:tədˈA."}, - {"On 24/12/2024 we left.", "ˈɒn ðə twˈɛntifˈɔːθ ɒv dɪsˈɛmbə twˈɛnti twˈɛntifˈɔː wiː lˈɛft.", "On:ˈɒn|24/12/2024:ðə twˈɛntifˈɔːθ ɒv dɪsˈɛmbə twˈɛnti twˈɛntifˈɔː|we:wiː|left.:lˈɛft."}, - {"The 21st century.", "ðə twˈɛntifˈɜːst sˈɛnʧʊɹi.", "The:ðə|21st:twˈɛntifˈɜːst|century.:sˈɛnʧʊɹi."}, - {"About 50% agree.", "əbˈWt fˈɪfti pəsˈɛnt əɡɹˈiː.", "About:əbˈWt|50%:fˈɪfti pəsˈɛnt|agree.:əɡɹˈiː."}, - {"Meet me at 3:30 pm.", "mˈiːt mˌiː at θɹˈiː θˈɜːti pˈiːˈɛm.", "Meet:mˈiːt|me:mˌiː|at:at|3:30 pm.:θɹˈiː θˈɜːti pˈiːˈɛm."}, - {"Dr. Smith ran 5 km.", "dˈɒktə smˈɪθ ɹˈan fˈIv kˈɪləmˌiːtəz.", "Dr.:dˈɒktə|Smith:smˈɪθ|ran:ɹˈan|5 km.:fˈIv kˈɪləmˌiːtəz."}, - // Wrong: 946 should be read digit by digit (Verbalis#8). - {"Call 0207 946 0018 now.", "kˈɔːl zˈɪəɹQ tˈuː zˈɪəɹQ sˈɛvᵊn nˈIn hˈʌndɹəd fˈɔːtisˈɪks zˈɪəɹQ zˈɪəɹQ wˈʌn ˈAt nˈW.", "Call:kˈɔːl|0207:zˈɪəɹQ tˈuː zˈɪəɹQ sˈɛvᵊn|946:nˈIn hˈʌndɹəd fˈɔːtisˈɪks|0018:zˈɪəɹQ zˈɪəɹQ wˈʌn ˈAt|now.:nˈW."}, - }); +// Holds across versions: however a text is read, it has phonemes, and each of +// its words is located once, in order, at phonemes of its own. A phrase read +// as one unit ("3:30 pm") may come back as one word spanning several. +TEST_F(PhonemizerOutputTest, LocatesEveryWordOfTheInput) { + auto cases = snapshots::read(kSnapshot); + auto results = phonemizeAll(*this, cases); + for (size_t i = 0; i < cases.size(); ++i) { + EXPECT_EQ(results[i].problems, "") << "[" << cases[i].lang << "] " << cases[i].input; + } } -// zh segments the text into words, so a located word can span several characters. -TEST_F(PhonemizerOutputTest, ReadsChinese) { - expectPhonemized(*this, kZh, { - {"我有25只猫。", "wo↓ jou↓ ɚ↘ʂɨ↗u↓ ꭧɨ↓ mau→.", "我:wo↓|有:jou↓|25:ɚ↘ʂɨ↗u↓|只:ꭧɨ↓|猫。:mau→."}, - {"价格是4.50元。", "ʨja↘kɤ↗ ʂɨ↘ sɨ↘tjɛ↓n u↓li↗ŋ ɥɛ↗n.", "价格:ʨja↘kɤ↗|是:ʂɨ↘|4.50:sɨ↘tjɛ↓n u↓li↗ŋ|元。:ɥɛ↗n."}, - {"2024年12月24日我们走了。", "ɚ↘li↗ŋɚ↘ sɨ↘njɛ↗n ʂɨ↗ɚ↘ɥe↘ ɚ↘ʂɨ↗sɨ↘ɻɨ↘ wo↓mən ʦou↓ lɤ.", "2024年12月24日:ɚ↘li↗ŋɚ↘ sɨ↘njɛ↗n ʂɨ↗ɚ↘ɥe↘ ɚ↘ʂɨ↗sɨ↘ɻɨ↘|我们:wo↓mən|走:ʦou↓|了。:lɤ."}, - {"下午3:30见。", "ɕja↘u↓ sa→ntjɛ↓n sa→nʂɨ↗fə→n ʨjɛ↘n.", "下午:ɕja↘u↓|3:30:sa→ntjɛ↓n sa→nʂɨ↗fə→n|见。:ʨjɛ↘n."}, - {"第3天。", "ti↘sa→ntʰjɛ→n.", "第3天。:ti↘sa→ntʰjɛ→n."}, - {"大约50%的人同意。", "ta↘ɥe→ pai↓fə→nꭧɨ→u↓ʂɨ↗ tɤ ɻə↗n tʰʊ↗ŋi↘.", "大约:ta↘ɥe→|50%:pai↓fə→nꭧɨ→u↓ʂɨ↗|的:tɤ|人:ɻə↗n|同意。:tʰʊ↗ŋi↘."}, - {"1999年。", "i→ʨjou↓ʨjou↓ʨjou↓njɛ↗n.", "1999年。:i→ʨjou↓ʨjou↓ʨjou↓njɛ↗n."}, - }); +TEST_F(PhonemizerOutputTest, MatchesTheSnapshot) { + snapshots::verify(kSnapshot, [this](const std::vector &cases) { + auto results = phonemizeAll(*this, cases); + std::vector out; + for (size_t i = 0; i < cases.size(); ++i) { + phonemis::Pipeline pipeline(phonemis::Config{.lang = cases[i].lang}); + out.push_back({ + {"preprocessed", u32_to_utf8(pipeline.preprocess(utf8_to_u32(cases[i].input)))}, + {"phonemes", results[i].phonemes}, + {"words", results[i].words}, + }); + } + return out; + }); } } // namespace diff --git a/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp b/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp index e0eb296bfa..99c3a4ab69 100644 --- a/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp +++ b/packages/react-native-executorch/cpp/tests/extensions/PreprocessingTest.cpp @@ -1,125 +1,58 @@ -#include #include +#include +#include #include #include #include +#include "support/Snapshots.h" + namespace rnexecutorch::tests { namespace { using phonemis::utils::conversions::u32_to_utf8; using phonemis::utils::conversions::utf8_to_u32; -// These languages are phonemized by a neural model, which these tests cannot -// run, so PhonemizerOutputTest covers only en-us, en-gb and zh. What phonemis -// feeds the model is still pinned here: the text after preprocessing (Verbalis), -// which reads numbers, dates, money, times and abbreviations out as words. It -// needs no data, so this goes below the JSI layer, to phonemis directly. +// Preprocessing (Verbalis, inside phonemis) reads numbers, dates, money, times +// and abbreviations out as words before anything is phonemized. It needs no +// data, so these go below the JSI layer, to phonemis directly, and cover every +// language: de, fr, es, it, pl, pt and hi are phonemized by a neural model that +// these tests cannot run, so for them this is all that is pinned. -struct Case { - const char *text; - const char *preprocessed; -}; +const std::string kSnapshot = std::string(RNE_SNAPSHOT_DIR) + "/preprocessing.txt"; -void expectPreprocessed(const char *lang, std::initializer_list cases) { +std::string preprocess(const std::string &lang, const std::string &text) { phonemis::Pipeline pipeline(phonemis::Config{.lang = lang}); - for (const auto &c : cases) { - SCOPED_TRACE(c.text); - EXPECT_EQ(u32_to_utf8(pipeline.preprocess(utf8_to_u32(c.text))), c.preprocessed); + return u32_to_utf8(pipeline.preprocess(utf8_to_u32(text))); +} + +// Holds across versions: what a reading looks like may change, but nothing is +// left for the phonemizer to spell out character by character. +TEST(PreprocessingTest, LeavesNoDigitsOrSymbols) { + constexpr std::string_view kUnread[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", + "$", "€", "£", "₹", "%", "/", "º", "°"}; + for (const auto *file : {"/preprocessing.txt", "/phonemizer.txt"}) { + for (const auto &c : snapshots::read(std::string(RNE_SNAPSHOT_DIR) + file)) { + auto out = preprocess(c.lang, c.input); + for (auto unread : kUnread) { + EXPECT_EQ(out.find(unread), std::string::npos) + << "[" << c.lang << "] " << c.input << " => " << out << " still holds " << unread; + } + } } } -TEST(PreprocessingTest, ReadsGerman) { - expectPreprocessed("de", { - {"Ich habe 25 Katzen.", "Ich habe fünfundzwanzig Katzen."}, - {"Es kostet 4,50 €.", "Es kostet vier Euro fünfzig."}, - {"Am 24.12.2024 fuhren wir.", "Am vierundzwanzigsten Dezember zweitausendvierundzwanzig fuhren wir."}, - {"Um 15:30 Uhr.", "Um fünfzehn Uhr dreißig."}, - {"Der 3. Mai.", "Der dritte Mai."}, - {"Dr. Müller lief z. B. 3,5 km.", "Doktor Müller lief zum Beispiel drei Komma fünf Kilometer."}, - {"Etwa 50 % stimmen zu.", "Etwa fünfzig Prozent stimmen zu."}, - {"Im Jahr 1999.", "Im Jahr neunzehnhundertneunundneunzig."}, - }); -} - -TEST(PreprocessingTest, ReadsFrench) { - expectPreprocessed("fr", { - {"J'ai 25 chats.", "J'ai vingt-cinq chats."}, - {"Ça coûte 4,50 €.", "Ça coûte quatre euros cinquante."}, - {"Le 24/12/2024 nous sommes partis.", "Le vingt-quatre décembre deux mille vingt-quatre nous sommes partis."}, - {"À 15h30.", "À quinze heures trente."}, - {"Le 3e jour.", "Le troisième jour."}, - {"M. Dupont a couru 3,5 km.", "monsieur Dupont a couru trois virgule cinq kilomètres."}, - {"Environ 50 % sont d'accord.", "Environ cinquante pour cent sont d'accord."}, - {"En 1999.", "En mille neuf cent quatre-vingt-dix-neuf."}, - }); -} - -TEST(PreprocessingTest, ReadsSpanish) { - expectPreprocessed("es", { - {"Tengo 25 gatos.", "Tengo veinticinco gatos."}, - {"Cuesta 4,50 €.", "Cuesta cuatro euros con cincuenta."}, - {"El 24/12/2024 salimos.", "El veinticuatro de diciembre de dos mil veinticuatro salimos."}, - {"A las 15:30.", "A las quince y media."}, - // Wrong: should be "tercer" (Verbalis#8). - {"El 3.º día.", "El tercero día."}, - {"El Sr. García corrió 3,5 km.", "El señor García corrió tres coma cinco kilómetros."}, - {"Un 50 % está de acuerdo.", "Un cincuenta por ciento está de acuerdo."}, - {"En 1999.", "En mil novecientos noventa y nueve."}, - }); -} -TEST(PreprocessingTest, ReadsItalian) { - expectPreprocessed("it", { - {"Ho 25 gatti.", "Ho venticinque gatti."}, - {"Costa 4,50 €.", "Costa quattro euro e cinquanta centesimi."}, - {"Il 24/12/2024 siamo partiti.", "Il ventiquattro dicembre duemilaventiquattro siamo partiti."}, - {"Alle 15:30.", "Alle quindici e trenta."}, - {"Il 3° giorno.", "Il terzo giorno."}, - {"Il Sig. Rossi ha corso 3,5 km.", "Il signor Rossi ha corso tre virgola cinque chilometri."}, - {"Il 50% è d'accordo.", "Il cinquanta per cento è d'accordo."}, - {"Nel 1999.", "Nel millenovecentonovantanove."}, - }); +TEST(PreprocessingTest, MatchesTheSnapshot) { + snapshots::verify(kSnapshot, [](const std::vector &cases) { + std::vector out; + for (const auto &c : cases) { + out.push_back({{"preprocessed", preprocess(c.lang, c.input)}}); + } + return out; + }); } -TEST(PreprocessingTest, ReadsPolish) { - expectPreprocessed("pl", { - {"Mam 25 kotów.", "Mam dwadzieścia pięć kotów."}, - {"Kosztuje 4,50 zł.", "Kosztuje cztery złote pięćdziesiąt groszy."}, - {"Dnia 24.12.2024 wyjechaliśmy.", "Dnia dwudziestego czwartego grudnia dwa tysiące dwudziestego czwartego wyjechaliśmy."}, - {"O 15:30.", "O piętnastej trzydzieści."}, - {"3 maja.", "trzeciego maja."}, - {"Dr Kowalski przebiegł np. 3,5 km.", "Doktor Kowalski przebiegł na przykład trzy i pół kilometra."}, - {"Około 50% się zgadza.", "Około pięćdziesięciu procent się zgadza."}, - {"W 1999 roku.", "W tysiąc dziewięćset dziewięćdziesiątym dziewiątym roku."}, - }); -} - -TEST(PreprocessingTest, ReadsPortuguese) { - expectPreprocessed("pt", { - {"Tenho 25 gatos.", "Tenho vinte e cinco gatos."}, - {"Custa R$ 4,50.", "Custa quatro reais e cinquenta centavos."}, - {"Em 24/12/2024 partimos.", "Em vinte e quatro de dezembro de dois mil e vinte e quatro partimos."}, - {"Às 15h30.", "Às quinze horas e trinta."}, - {"O 3.º dia.", "O terceiro dia."}, - // Wrong: "Sr." should be "senhor" (Verbalis#8). - {"O Sr. Silva correu 3,5 km.", "O Sr. Silva correu três vírgula cinco quilômetros."}, - {"Cerca de 50% concordam.", "Cerca de cinquenta por cento concordam."}, - {"Em 1999.", "Em mil novecentos e noventa e nove."}, - }); -} - -TEST(PreprocessingTest, ReadsHindi) { - expectPreprocessed("hi", { - {"मेरे पास 25 बिल्लियाँ हैं।", "मेरे पास पच्चीस बिल्लियाँ हैं।"}, - {"इसकी कीमत ₹4.50 है।", "इसकी कीमत चार रुपये पचास पैसे है।"}, - {"24/12/2024 को हम गए।", "चौबीस दिसंबर दो हज़ार चौबीस को हम गए।"}, - // Wrong: says बजे twice (Verbalis#8). - {"3:30 बजे।", "तीन बजकर तीस मिनट बजे।"}, - {"50% लोग सहमत हैं।", "पचास प्रतिशत लोग सहमत हैं।"}, - {"1999 में।", "एक हज़ार नौ सौ निन्यानवे में।"}, - }); -} } // namespace } // namespace rnexecutorch::tests diff --git a/packages/react-native-executorch/cpp/tests/snapshots/phonemizer.txt b/packages/react-native-executorch/cpp/tests/snapshots/phonemizer.txt new file mode 100644 index 0000000000..47a3c4f4d9 --- /dev/null +++ b/packages/react-native-executorch/cpp/tests/snapshots/phonemizer.txt @@ -0,0 +1,133 @@ +# What phonemis produces on Kokoro's shipped en-us, en-gb and zh data +# (PhonemizerOutputTest). Format and update command: support/Snapshots.h. +# The neural fallback isn't loaded, so inputs stick to words the lexicon knows. + +en-us | Hello world. + preprocessed: Hello world. + phonemes: həlˈO wˈɜɹld. + words: Hello:həlˈO|world.:wˈɜɹld. +en-us | I have 25 cats. + preprocessed: I have twenty-five cats. + phonemes: ˌI hæv twˈɛntifˈIv kˈæts. + words: I:ˌI|have:hæv|25:twˈɛntifˈIv|cats.:kˈæts. +en-us | It happened in 1999. + preprocessed: It happened in nineteen ninety-nine. + phonemes: ˌɪt hˈæpənd ɪn nˌIntˈin nˈIndinˈIn. + words: It:ˌɪt|happened:hˈæpənd|in:ɪn|1999.:nˌIntˈin nˈIndinˈIn. +en-us | The 21st century began. + preprocessed: The twenty-first century began. + phonemes: ðə twˈɛntifˈɜɹst sˈɛnʧəɹi bəɡˈæn. + words: The:ðə|21st:twˈɛntifˈɜɹst|century:sˈɛnʧəɹi|began.:bəɡˈæn. +en-us | Pi is roughly 3.14. + preprocessed: Pi is roughly three point one four. + phonemes: pˈI ɪz ɹˈʌfli θɹˈi pˈYnt wˈʌn fˈɔɹ. + words: Pi:pˈI|is:ɪz|roughly:ɹˈʌfli|3.14.:θɹˈi pˈYnt wˈʌn fˈɔɹ. +en-us | We sold 1,000,000 copies. + preprocessed: We sold one million copies. + phonemes: wˌi sˈOld wˈʌn mˈɪljᵊn kˈɑpiz. + words: We:wˌi|sold:sˈOld|1,000,000:wˈʌn mˈɪljᵊn|copies.:kˈɑpiz. +en-us | Chapter IV starts here. + preprocessed: Chapter four starts here. + phonemes: ʧˈæptəɹ fˈɔɹ stˈɑɹts hˈɪɹ. + words: Chapter:ʧˈæptəɹ|IV:fˈɔɹ|starts:stˈɑɹts|here.:hˈɪɹ. +en-us | Call 555-1234 now. + preprocessed: Call five five five, one two three four now. + phonemes: kˈɔl fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ nˈW. + words: Call:kˈɔl|555-1234:fˈIv fˈIv fˈIv, wˈʌn tˈu θɹˈi fˈɔɹ|now.:nˈW. +en-us | It costs $4.50 today. + preprocessed: It costs four dollars and fifty cents today. + phonemes: ˌɪt kˈɔsts fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts tədˈA. + words: It:ˌɪt|costs:kˈɔsts|$4.50:fˈɔɹ dˈɑləɹz ænd fˈɪfti sˈɛnts|today.:tədˈA. +en-us | About 50% of people agree. + preprocessed: About fifty percent of people agree. + phonemes: əbˈWt fˈɪfti pəɹsˈɛnt ʌv pˈipᵊl əɡɹˈi. + words: About:əbˈWt|50%:fˈɪfti pəɹsˈɛnt|of:ʌv|people:pˈipᵊl|agree.:əɡɹˈi. +en-us | On 12/25/2024 we left. + preprocessed: On December twenty-fifth twenty twenty-four we left. + phonemes: ˈɔn disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ wi lˈɛft. + words: On:ˈɔn|12/25/2024:disˈɛmbəɹ twˈɛntifˈɪfθ twˈɛnti twˈɛntifˈɔɹ|we:wi|left.:lˈɛft. +en-us | Born on May 5, 2024. + preprocessed: Born on May fifth, twenty twenty-four. + phonemes: bˈɔɹn ˌɔn mˈA fˈɪfθ, twˈɛnti twˈɛntifˈɔɹ. + words: Born:bˈɔɹn|on:ˌɔn|May:mˈA|5,:fˈɪfθ,|2024.:twˈɛnti twˈɛntifˈɔɹ. +en-us | Dr. Smith lives on Main St. + preprocessed: Doctor Smith lives on Main Street. + phonemes: dˈɑktəɹ smˈɪθ lˈIvz ˌɔn mˈAn stɹˈit. + words: Dr.:dˈɑktəɹ|Smith:smˈɪθ|lives:lˈIvz|on:ˌɔn|Main:mˈAn|St.:stɹˈit. +en-us | Bring fruit, e.g. apples. + preprocessed: Bring fruit, for example apples. + phonemes: bɹˈɪŋ fɹˈut, fɔɹ ɪɡzˈæmpəl ˈæpᵊlz. + words: Bring:bɹˈɪŋ|fruit,:fɹˈut,|e.g.:fɔɹ ɪɡzˈæmpəl|apples.:ˈæpᵊlz. +en-us | Rock & roll. + preprocessed: Rock and roll. + phonemes: ɹˈɑk ænd ɹˈOl. + words: Rock:ɹˈɑk|&:ænd|roll.:ɹˈOl. +# A time or a measure is read as one phrase, so it is located as one word. +en-us | Meet me at 3:30 pm. + preprocessed: Meet me at three thirty p.m. + phonemes: mˈit mˌi æt θɹˈi θˈɜɹɾi pˈiˈɛm. + words: Meet:mˈit|me:mˌi|at:æt|3:30 pm.:θɹˈi θˈɜɹɾi pˈiˈɛm. +en-us | She ran 5 km. + preprocessed: She ran five kilometers. + phonemes: ʃˌi ɹˈæn fˈIv kəlˈɑməɾəɹz. + words: She:ʃˌi|ran:ɹˈæn|5 km.:fˈIv kəlˈɑməɾəɹz. + +en-gb | It costs £4.50 today. + preprocessed: It costs four pounds and fifty pence today. + phonemes: ˌɪt kˈɒsts fˈɔː pˈWndz and fˈɪfti pˈɛns tədˈA. + words: It:ˌɪt|costs:kˈɒsts|£4.50:fˈɔː pˈWndz and fˈɪfti pˈɛns|today.:tədˈA. +en-gb | On 24/12/2024 we left. + preprocessed: On the twenty-fourth of December twenty twenty-four we left. + phonemes: ˈɒn ðə twˈɛntifˈɔːθ ɒv dɪsˈɛmbə twˈɛnti twˈɛntifˈɔː wiː lˈɛft. + words: On:ˈɒn|24/12/2024:ðə twˈɛntifˈɔːθ ɒv dɪsˈɛmbə twˈɛnti twˈɛntifˈɔː|we:wiː|left.:lˈɛft. +en-gb | The 21st century. + preprocessed: The twenty-first century. + phonemes: ðə twˈɛntifˈɜːst sˈɛnʧʊɹi. + words: The:ðə|21st:twˈɛntifˈɜːst|century.:sˈɛnʧʊɹi. +en-gb | About 50% agree. + preprocessed: About fifty percent agree. + phonemes: əbˈWt fˈɪfti pəsˈɛnt əɡɹˈiː. + words: About:əbˈWt|50%:fˈɪfti pəsˈɛnt|agree.:əɡɹˈiː. +en-gb | Meet me at 3:30 pm. + preprocessed: Meet me at three thirty p.m. + phonemes: mˈiːt mˌiː at θɹˈiː θˈɜːti pˈiːˈɛm. + words: Meet:mˈiːt|me:mˌiː|at:at|3:30 pm.:θɹˈiː θˈɜːti pˈiːˈɛm. +en-gb | Dr. Smith ran 5 km. + preprocessed: Doctor Smith ran five kilometers. + phonemes: dˈɒktə smˈɪθ ɹˈan fˈIv kˈɪləmˌiːtəz. + words: Dr.:dˈɒktə|Smith:smˈɪθ|ran:ɹˈan|5 km.:fˈIv kˈɪləmˌiːtəz. +# Wrong: 946 should be read digit by digit (Verbalis#8). +en-gb | Call 0207 946 0018 now. + preprocessed: Call zero two zero seven nine hundred forty-six zero zero one eight now. + phonemes: kˈɔːl zˈɪəɹQ tˈuː zˈɪəɹQ sˈɛvᵊn nˈIn hˈʌndɹəd fˈɔːtisˈɪks zˈɪəɹQ zˈɪəɹQ wˈʌn ˈAt nˈW. + words: Call:kˈɔːl|0207:zˈɪəɹQ tˈuː zˈɪəɹQ sˈɛvᵊn|946:nˈIn hˈʌndɹəd fˈɔːtisˈɪks|0018:zˈɪəɹQ zˈɪəɹQ wˈʌn ˈAt|now.:nˈW. + +# zh segments the text into words, so a located word can span several characters. +zh | 我有25只猫。 + preprocessed: 我有二十五只猫. + phonemes: wo↓ jou↓ ɚ↘ʂɨ↗u↓ ꭧɨ↓ mau→. + words: 我:wo↓|有:jou↓|25:ɚ↘ʂɨ↗u↓|只:ꭧɨ↓|猫。:mau→. +zh | 价格是4.50元。 + preprocessed: 价格是四点五零元. + phonemes: ʨja↘kɤ↗ ʂɨ↘ sɨ↘tjɛ↓n u↓li↗ŋ ɥɛ↗n. + words: 价格:ʨja↘kɤ↗|是:ʂɨ↘|4.50:sɨ↘tjɛ↓n u↓li↗ŋ|元。:ɥɛ↗n. +zh | 2024年12月24日我们走了。 + preprocessed: 二零二四年十二月二十四日我们走了. + phonemes: ɚ↘li↗ŋɚ↘ sɨ↘njɛ↗n ʂɨ↗ɚ↘ɥe↘ ɚ↘ʂɨ↗sɨ↘ɻɨ↘ wo↓mən ʦou↓ lɤ. + words: 2024年12月24日:ɚ↘li↗ŋɚ↘ sɨ↘njɛ↗n ʂɨ↗ɚ↘ɥe↘ ɚ↘ʂɨ↗sɨ↘ɻɨ↘|我们:wo↓mən|走:ʦou↓|了。:lɤ. +zh | 下午3:30见。 + preprocessed: 下午三点三十分见. + phonemes: ɕja↘u↓ sa→ntjɛ↓n sa→nʂɨ↗fə→n ʨjɛ↘n. + words: 下午:ɕja↘u↓|3:30:sa→ntjɛ↓n sa→nʂɨ↗fə→n|见。:ʨjɛ↘n. +zh | 第3天。 + preprocessed: 第三天. + phonemes: ti↘sa→ntʰjɛ→n. + words: 第3天。:ti↘sa→ntʰjɛ→n. +zh | 大约50%的人同意。 + preprocessed: 大约百分之五十的人同意. + phonemes: ta↘ɥe→ pai↓fə→nꭧɨ→u↓ʂɨ↗ tɤ ɻə↗n tʰʊ↗ŋi↘. + words: 大约:ta↘ɥe→|50%:pai↓fə→nꭧɨ→u↓ʂɨ↗|的:tɤ|人:ɻə↗n|同意。:tʰʊ↗ŋi↘. +zh | 1999年。 + preprocessed: 一九九九年. + phonemes: i→ʨjou↓ʨjou↓ʨjou↓njɛ↗n. + words: 1999年。:i→ʨjou↓ʨjou↓ʨjou↓njɛ↗n. diff --git a/packages/react-native-executorch/cpp/tests/snapshots/preprocessing.txt b/packages/react-native-executorch/cpp/tests/snapshots/preprocessing.txt new file mode 100644 index 0000000000..433caa5e43 --- /dev/null +++ b/packages/react-native-executorch/cpp/tests/snapshots/preprocessing.txt @@ -0,0 +1,121 @@ +# The text phonemis feeds the neural model of each language Kokoro phonemizes +# without a lexicon (PreprocessingTest). Format and update command: +# support/Snapshots.h. + +de | Ich habe 25 Katzen. + preprocessed: Ich habe fünfundzwanzig Katzen. +de | Es kostet 4,50 €. + preprocessed: Es kostet vier Euro fünfzig. +de | Am 24.12.2024 fuhren wir. + preprocessed: Am vierundzwanzigsten Dezember zweitausendvierundzwanzig fuhren wir. +de | Um 15:30 Uhr. + preprocessed: Um fünfzehn Uhr dreißig. +de | Der 3. Mai. + preprocessed: Der dritte Mai. +de | Dr. Müller lief z. B. 3,5 km. + preprocessed: Doktor Müller lief zum Beispiel drei Komma fünf Kilometer. +de | Etwa 50 % stimmen zu. + preprocessed: Etwa fünfzig Prozent stimmen zu. +de | Im Jahr 1999. + preprocessed: Im Jahr neunzehnhundertneunundneunzig. + +fr | J'ai 25 chats. + preprocessed: J'ai vingt-cinq chats. +fr | Ça coûte 4,50 €. + preprocessed: Ça coûte quatre euros cinquante. +fr | Le 24/12/2024 nous sommes partis. + preprocessed: Le vingt-quatre décembre deux mille vingt-quatre nous sommes partis. +fr | À 15h30. + preprocessed: À quinze heures trente. +fr | Le 3e jour. + preprocessed: Le troisième jour. +fr | M. Dupont a couru 3,5 km. + preprocessed: monsieur Dupont a couru trois virgule cinq kilomètres. +fr | Environ 50 % sont d'accord. + preprocessed: Environ cinquante pour cent sont d'accord. +fr | En 1999. + preprocessed: En mille neuf cent quatre-vingt-dix-neuf. + +es | Tengo 25 gatos. + preprocessed: Tengo veinticinco gatos. +es | Cuesta 4,50 €. + preprocessed: Cuesta cuatro euros con cincuenta. +es | El 24/12/2024 salimos. + preprocessed: El veinticuatro de diciembre de dos mil veinticuatro salimos. +es | A las 15:30. + preprocessed: A las quince y media. +# Wrong: should be "tercer" (Verbalis#8). +es | El 3.º día. + preprocessed: El tercero día. +es | El Sr. García corrió 3,5 km. + preprocessed: El señor García corrió tres coma cinco kilómetros. +es | Un 50 % está de acuerdo. + preprocessed: Un cincuenta por ciento está de acuerdo. +es | En 1999. + preprocessed: En mil novecientos noventa y nueve. + +it | Ho 25 gatti. + preprocessed: Ho venticinque gatti. +it | Costa 4,50 €. + preprocessed: Costa quattro euro e cinquanta centesimi. +it | Il 24/12/2024 siamo partiti. + preprocessed: Il ventiquattro dicembre duemilaventiquattro siamo partiti. +it | Alle 15:30. + preprocessed: Alle quindici e trenta. +it | Il 3° giorno. + preprocessed: Il terzo giorno. +it | Il Sig. Rossi ha corso 3,5 km. + preprocessed: Il signor Rossi ha corso tre virgola cinque chilometri. +it | Il 50% è d'accordo. + preprocessed: Il cinquanta per cento è d'accordo. +it | Nel 1999. + preprocessed: Nel millenovecentonovantanove. + +pl | Mam 25 kotów. + preprocessed: Mam dwadzieścia pięć kotów. +pl | Kosztuje 4,50 zł. + preprocessed: Kosztuje cztery złote pięćdziesiąt groszy. +pl | Dnia 24.12.2024 wyjechaliśmy. + preprocessed: Dnia dwudziestego czwartego grudnia dwa tysiące dwudziestego czwartego wyjechaliśmy. +pl | O 15:30. + preprocessed: O piętnastej trzydzieści. +pl | 3 maja. + preprocessed: trzeciego maja. +pl | Dr Kowalski przebiegł np. 3,5 km. + preprocessed: Doktor Kowalski przebiegł na przykład trzy i pół kilometra. +pl | Około 50% się zgadza. + preprocessed: Około pięćdziesięciu procent się zgadza. +pl | W 1999 roku. + preprocessed: W tysiąc dziewięćset dziewięćdziesiątym dziewiątym roku. + +pt | Tenho 25 gatos. + preprocessed: Tenho vinte e cinco gatos. +pt | Custa R$ 4,50. + preprocessed: Custa quatro reais e cinquenta centavos. +pt | Em 24/12/2024 partimos. + preprocessed: Em vinte e quatro de dezembro de dois mil e vinte e quatro partimos. +pt | Às 15h30. + preprocessed: Às quinze horas e trinta. +pt | O 3.º dia. + preprocessed: O terceiro dia. +# Wrong: "Sr." should be "senhor" (Verbalis#8). +pt | O Sr. Silva correu 3,5 km. + preprocessed: O Sr. Silva correu três vírgula cinco quilômetros. +pt | Cerca de 50% concordam. + preprocessed: Cerca de cinquenta por cento concordam. +pt | Em 1999. + preprocessed: Em mil novecentos e noventa e nove. + +hi | मेरे पास 25 बिल्लियाँ हैं। + preprocessed: मेरे पास पच्चीस बिल्लियाँ हैं। +hi | इसकी कीमत ₹4.50 है। + preprocessed: इसकी कीमत चार रुपये पचास पैसे है। +hi | 24/12/2024 को हम गए। + preprocessed: चौबीस दिसंबर दो हज़ार चौबीस को हम गए। +# Wrong: says बजे twice (Verbalis#8). +hi | 3:30 बजे। + preprocessed: तीन बजकर तीस मिनट बजे। +hi | 50% लोग सहमत हैं। + preprocessed: पचास प्रतिशत लोग सहमत हैं। +hi | 1999 में। + preprocessed: एक हज़ार नौ सौ निन्यानवे में। diff --git a/packages/react-native-executorch/cpp/tests/support/Snapshots.cpp b/packages/react-native-executorch/cpp/tests/support/Snapshots.cpp new file mode 100644 index 0000000000..cb1e92f8e8 --- /dev/null +++ b/packages/react-native-executorch/cpp/tests/support/Snapshots.cpp @@ -0,0 +1,107 @@ +#include "support/Snapshots.h" + +#include +#include +#include +#include +#include + +#include + +namespace rnexecutorch::tests::snapshots { +namespace { + +constexpr std::string_view kCaseSeparator = " | "; +constexpr std::string_view kValueIndent = " "; +constexpr std::string_view kValueSeparator = ": "; + +bool updating() { + const char *flag = std::getenv("RNE_UPDATE_SNAPSHOTS"); + return flag != nullptr && std::string_view(flag) == "1"; +} + +void write(const std::string &path, const std::vector &cases, const std::vector &trailing) { + std::ofstream out(path); + for (const auto &c : cases) { + for (const auto &comment : c.comments) { + out << comment << '\n'; + } + out << c.lang << kCaseSeparator << c.input << '\n'; + for (const auto &[key, value] : c.values) { + out << kValueIndent << key << kValueSeparator << value << '\n'; + } + } + for (const auto &comment : trailing) { + out << comment << '\n'; + } +} + +// The cases, plus the comment lines after the last one. +std::pair, std::vector> parse(const std::string &path) { + std::ifstream in(path); + if (!in) { + throw std::runtime_error("cannot open snapshot file " + path); + } + std::vector cases; + std::vector comments; + std::string line; + while (std::getline(in, line)) { + if (line.empty() || line.starts_with('#')) { + comments.push_back(line); + } else if (line.starts_with(kValueIndent)) { + auto separator = line.find(kValueSeparator); + if (cases.empty() || separator == std::string::npos) { + throw std::runtime_error("malformed snapshot line in " + path + ": " + line); + } + cases.back().values.emplace_back(line.substr(kValueIndent.size(), separator - kValueIndent.size()), + line.substr(separator + kValueSeparator.size())); + } else { + auto separator = line.find(kCaseSeparator); + if (separator == std::string::npos) { + throw std::runtime_error("malformed snapshot line in " + path + ": " + line); + } + cases.push_back({.lang = line.substr(0, separator), + .input = line.substr(separator + kCaseSeparator.size()), + .values = {}, + .comments = std::move(comments)}); + comments.clear(); + } + } + return {std::move(cases), std::move(comments)}; +} + +} // namespace + +std::vector read(const std::string &path) { return parse(path).first; } + +void verify(const std::string &path, const std::function(const std::vector &)> &produce) { + auto [cases, trailing] = parse(path); + auto actual = produce(cases); + ASSERT_EQ(actual.size(), cases.size()); + + if (updating()) { + for (size_t i = 0; i < cases.size(); ++i) { + cases[i].values = std::move(actual[i]); + } + write(path, cases, trailing); + return; + } + + for (size_t i = 0; i < cases.size(); ++i) { + if (cases[i].values != actual[i]) { + std::ostringstream message; + message << "snapshot of [" << cases[i].lang << "] " << cases[i].input << " changed\n"; + for (const auto &[key, value] : cases[i].values) { + message << " - " << key << kValueSeparator << value << '\n'; + } + for (const auto &[key, value] : actual[i]) { + message << " + " << key << kValueSeparator << value << '\n'; + } + message << "Accept with RNE_UPDATE_SNAPSHOTS=1 scripts/run-native-tests.sh, then review the diff of " + << path; + ADD_FAILURE() << message.str(); + } + } +} + +} // namespace rnexecutorch::tests::snapshots diff --git a/packages/react-native-executorch/cpp/tests/support/Snapshots.h b/packages/react-native-executorch/cpp/tests/support/Snapshots.h new file mode 100644 index 0000000000..9a1f023f97 --- /dev/null +++ b/packages/react-native-executorch/cpp/tests/support/Snapshots.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include +#include + +namespace rnexecutorch::tests::snapshots { + +/** + * Snapshot files pin what a third-party dependency produces (phonemis's + * readings) without hard-coding it in the tests, so a bump that changes the + * output is one command to accept and a diff to review, not strings to retype: + * + * RNE_UPDATE_SNAPSHOTS=1 scripts/run-native-tests.sh -R + * + * A file is a list of cases. Each starts with a ` | ` line and + * holds one indented `: ` line per output. Lines starting with `#` + * and blank lines are comments and survive an update; a case added without + * values is filled in by the next one. + */ + +using Values = std::vector>; + +struct Case { + std::string lang; + std::string input; + Values values; + // Comment lines just above the case. + std::vector comments; +}; + +std::vector read(const std::string &path); + +/** + * Compares what `produce` returns for each case, in order, with the file. + * With RNE_UPDATE_SNAPSHOTS=1 it rewrites the file instead, and passes. + */ +void verify(const std::string &path, const std::function(const std::vector &)> &produce); + +} // namespace rnexecutorch::tests::snapshots