react-native-jieba 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/Jieba.podspec +2 -0
- package/README.md +28 -13
- package/android/CMakeLists.txt +2 -0
- package/android/generated/java/com/jieba/NativeJiebaSpec.java +4 -0
- package/android/generated/jni/JiebaSpec-generated.cpp +6 -0
- package/android/generated/jni/react/renderer/components/JiebaSpec/JiebaSpecJSI.h +8 -0
- package/android/src/main/java/com/jieba/JiebaAndroidHelperModule.kt +7 -22
- package/android/src/main/java/com/jieba/JiebaDict.kt +72 -0
- package/android/src/main/java/com/jieba/JiebaPackage.kt +4 -0
- package/cpp/JiebaDictAndroid.cpp +26 -0
- package/cpp/JiebaDictAndroid.h +14 -0
- package/cpp/JiebaImpl.cpp +23 -0
- package/cpp/JiebaImpl.h +1 -0
- package/cpp/cppjieba/CHANGELOG.md +18 -0
- package/cpp/cppjieba/README.md +288 -0
- package/cpp/cppjieba/deps/limonp/CHANGELOG.md +175 -0
- package/cpp/cppjieba/deps/limonp/README.md +43 -0
- package/cpp/cppjieba/dict/README.md +62 -0
- package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec-generated.mm +7 -0
- package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec.h +1 -0
- package/ios/generated/ReactCodegen/JiebaSpecJSI.h +8 -0
- package/lib/module/NativeJieba.js.map +1 -1
- package/lib/module/NativeJieba.web.js +8 -2
- package/lib/module/NativeJieba.web.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/init.js +33 -6
- package/lib/module/init.js.map +1 -1
- package/lib/module/init.web.js +19 -7
- package/lib/module/init.web.js.map +1 -1
- package/lib/typescript/src/NativeJieba.d.ts +1 -0
- package/lib/typescript/src/NativeJieba.d.ts.map +1 -1
- package/lib/typescript/src/NativeJieba.web.d.ts +1 -0
- package/lib/typescript/src/NativeJieba.web.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/init.d.ts +25 -2
- package/lib/typescript/src/init.d.ts.map +1 -1
- package/lib/typescript/src/init.web.d.ts +17 -3
- package/lib/typescript/src/init.web.d.ts.map +1 -1
- package/package.json +6 -1
- package/src/NativeJieba.ts +1 -0
- package/src/NativeJieba.web.ts +11 -3
- package/src/index.tsx +2 -1
- package/src/init.ts +35 -7
- package/src/init.web.ts +30 -9
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## 0.3.0
|
|
9
|
+
|
|
10
|
+
### Breaking changes
|
|
11
|
+
|
|
12
|
+
- **`initJieba` has been renamed to `prepareJieba`.** The old name implied a
|
|
13
|
+
mandatory global init step, which it no longer is on native. Update imports and
|
|
14
|
+
calls:
|
|
15
|
+
|
|
16
|
+
```diff
|
|
17
|
+
- import { initJieba } from 'react-native-jieba';
|
|
18
|
+
- await initJieba();
|
|
19
|
+
+ import { prepareJieba } from 'react-native-jieba';
|
|
20
|
+
+ await prepareJieba();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
On **web** `prepareJieba()` is still required (it loads the `jieba-wasm`
|
|
24
|
+
binary). On **iOS/Android** it is now optional — see below.
|
|
25
|
+
|
|
26
|
+
`InitJiebaOptions` is likewise renamed to `PrepareJiebaOptions`.
|
|
27
|
+
|
|
28
|
+
### Added
|
|
29
|
+
|
|
30
|
+
- **`prepareJieba()` is now optional on native.** You can call `cut`, `tag`, etc.
|
|
31
|
+
directly without any setup — the dictionary is located/extracted automatically on
|
|
32
|
+
first use (iOS resolves it from the app bundle; Android lazily extracts the bundled
|
|
33
|
+
assets to `filesDir/jieba-dict/` via an fbjni bridge). Calling `prepareJieba()`
|
|
34
|
+
remains useful as an async warm-up to avoid a one-time blocking pause on Android's
|
|
35
|
+
first call.
|
|
36
|
+
- **`isJiebaReady(): boolean`** — a synchronous readiness check, backed by the real
|
|
37
|
+
native engine state (a new `isReady()` JSI method) rather than a JS flag, so it
|
|
38
|
+
stays correct even when the dictionary is resolved lazily on the first call.
|
|
39
|
+
|
|
40
|
+
## 0.2.0
|
|
41
|
+
|
|
42
|
+
### Added
|
|
43
|
+
|
|
44
|
+
- Web support via [`jieba-wasm`](https://github.com/fengkx/jieba-wasm) for
|
|
45
|
+
react-native-web, exposing the same API as the native build.
|
|
46
|
+
|
|
47
|
+
## 0.1.0
|
|
48
|
+
|
|
49
|
+
- Initial release: cppjieba C++ Turbo Module for iOS and Android with `cut`,
|
|
50
|
+
`cutAll`, `cutForSearch`, `cutHMM`, `cutSmall`, `tag`, `extract`, `insertUserWord`,
|
|
51
|
+
and `find`.
|
package/Jieba.podspec
CHANGED
|
@@ -14,6 +14,8 @@ Pod::Spec.new do |s|
|
|
|
14
14
|
s.source = { :git => "https://github.com/leonsilicon/react-native-jieba.git", :tag => "#{s.version}" }
|
|
15
15
|
|
|
16
16
|
s.source_files = "ios/**/*.{h,m,mm}", "cpp/*.{hpp,cpp,c,h}", "ios/generated/*.{h,cpp,mm}"
|
|
17
|
+
# JiebaDictAndroid.{cpp,h} depend on fbjni and are Android-only.
|
|
18
|
+
s.exclude_files = "cpp/JiebaDictAndroid.{cpp,h}"
|
|
17
19
|
s.private_header_files = "ios/**/*.h"
|
|
18
20
|
|
|
19
21
|
s.resources = ["cpp/cppjieba/dict/*.utf8"]
|
package/README.md
CHANGED
|
@@ -48,11 +48,21 @@ export default {
|
|
|
48
48
|
|
|
49
49
|
## Usage
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
On **native (iOS/Android)** there is no setup — just call `cut`, `tag`, etc. The dictionary is located/extracted automatically: on iOS the path is resolved from the app bundle, and on Android the bundled assets are extracted to `filesDir/jieba-dict/` on the first call.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { cut } from 'react-native-jieba';
|
|
55
|
+
|
|
56
|
+
cut('我来到北京清华大学');
|
|
57
|
+
// ['我', '来到', '北京', '清华大学']
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`prepareJieba()` is an **optional async warm-up** on native: on Android's first run the asset extraction happens off the call site (asynchronously), avoiding a one-time blocking pause on the first `cut`/`tag`. On **web** it is **required** — it loads the `jieba-wasm` binary, which cannot happen synchronously. Use `isJiebaReady()` to check synchronously whether segmentation can run.
|
|
52
61
|
|
|
53
62
|
```ts
|
|
54
63
|
import {
|
|
55
|
-
|
|
64
|
+
prepareJieba,
|
|
65
|
+
isJiebaReady,
|
|
56
66
|
cut,
|
|
57
67
|
cutAll,
|
|
58
68
|
cutForSearch,
|
|
@@ -60,7 +70,10 @@ import {
|
|
|
60
70
|
extract,
|
|
61
71
|
} from 'react-native-jieba';
|
|
62
72
|
|
|
63
|
-
|
|
73
|
+
// Native: optional warm-up. Web: required (loads wasm).
|
|
74
|
+
await prepareJieba();
|
|
75
|
+
|
|
76
|
+
isJiebaReady(); // true
|
|
64
77
|
|
|
65
78
|
cut('我来到北京清华大学');
|
|
66
79
|
// ['我', '来到', '北京', '清华大学']
|
|
@@ -80,11 +93,12 @@ extract('我是拖拉机学院手扶拖拉机专业的。', 5);
|
|
|
80
93
|
|
|
81
94
|
## API
|
|
82
95
|
|
|
83
|
-
On native, all segmentation calls are synchronous JSI calls — no Promises. On web they are synchronous too, but `
|
|
96
|
+
On native, all segmentation calls are synchronous JSI calls — no Promises. On web they are synchronous too, but `prepareJieba()` must finish first (it loads the wasm module).
|
|
84
97
|
|
|
85
98
|
| Function | Signature | Notes |
|
|
86
99
|
| --- | --- | --- |
|
|
87
|
-
| `
|
|
100
|
+
| `prepareJieba(options?)` | `(options?: { wasmUrl?: string \| URL \| Request }) => Promise<void>` | **Web:** must be awaited once before any other call. **Native:** optional warm-up — calling segmentation directly works too. Idempotent. `wasmUrl` is web-only and rarely needed (override where the `.wasm` is served). |
|
|
101
|
+
| `isJiebaReady()` | `() => boolean` | Synchronous check for whether segmentation can run now. Native-backed (reflects the real engine state, not whether `prepareJieba` was called): `true` on iOS from launch, `true` on Android once the dict is resolved (eagerly via `prepareJieba` or lazily on the first call). |
|
|
88
102
|
| `cut(sentence, hmm?)` | `(string, boolean) => string[]` | Precise mode. `hmm` defaults to `true`. |
|
|
89
103
|
| `cutAll(sentence)` | `(string) => string[]` | Full mode (every possible word). |
|
|
90
104
|
| `cutForSearch(sentence, hmm?)` | `(string, boolean) => string[]` | Search-engine mode. |
|
|
@@ -130,15 +144,15 @@ export default defineConfig({
|
|
|
130
144
|
// App.tsx
|
|
131
145
|
import { useEffect, useState } from 'react';
|
|
132
146
|
import { Platform, Text, View } from 'react-native';
|
|
133
|
-
import {
|
|
147
|
+
import { prepareJieba, cut, cutForSearch, tag, extract } from 'react-native-jieba';
|
|
134
148
|
|
|
135
149
|
export default function App() {
|
|
136
150
|
const [words, setWords] = useState<string[] | null>(null);
|
|
137
151
|
|
|
138
152
|
useEffect(() => {
|
|
139
|
-
// On web this loads + instantiates the wasm module (async).
|
|
140
|
-
// On native
|
|
141
|
-
|
|
153
|
+
// On web this loads + instantiates the wasm module (async) and is required.
|
|
154
|
+
// On native you can skip this and call cut() directly.
|
|
155
|
+
prepareJieba()
|
|
142
156
|
.then(() => {
|
|
143
157
|
setWords(cut('我来到北京清华大学'));
|
|
144
158
|
// → ['我', '来到', '北京', '清华大学']
|
|
@@ -169,16 +183,17 @@ export default function App() {
|
|
|
169
183
|
To self-host the binary (CDN or custom path), pass `wasmUrl`:
|
|
170
184
|
|
|
171
185
|
```ts
|
|
172
|
-
await
|
|
186
|
+
await prepareJieba({ wasmUrl: 'https://cdn.example.com/jieba_rs_wasm_bg.wasm' });
|
|
173
187
|
```
|
|
174
188
|
|
|
175
189
|
## How it works
|
|
176
190
|
|
|
177
191
|
- The Turbo Module lives in `cpp/JiebaImpl.{h,cpp}` and wraps `cppjieba::Jieba` as a JSI Cxx module.
|
|
178
|
-
- iOS resolves the dictionary directory from `NSBundle` in `ios/OnLoad.mm` before the module is registered, so `
|
|
179
|
-
- Android ships the dictionary as AAR assets and extracts them
|
|
192
|
+
- iOS resolves the dictionary directory from `NSBundle` in `ios/OnLoad.mm` before the module is registered, so segmentation works immediately and `prepareJieba()` is a no-op on iOS.
|
|
193
|
+
- Android ships the dictionary as AAR assets and extracts them to `filesDir/jieba-dict/`. This happens automatically: if `prepareJieba()` is never called, the C++ module extracts them synchronously on the first segmentation call via an fbjni call into `JiebaDict.extractDictDirFromNative` (a one-time cost). `prepareJieba()` does the same extraction asynchronously up front (through `JiebaAndroidHelperModule` → the codegen-exposed `setDictPath` JSI method) so that first call doesn't block.
|
|
194
|
+
- `isJiebaReady()` is backed by the codegen-exposed `isReady()` JSI method, which reads the native engine state directly — so it stays correct even when the dictionary is resolved lazily on the first call.
|
|
180
195
|
- cppjieba and its `limonp` dependency are vendored as git submodules under `cpp/cppjieba/`. The published npm tarball contains only the headers and dictionaries that are actually needed at build time.
|
|
181
|
-
- On web, `react-native-web`'s bundler resolution picks up `src/NativeJieba.web.ts` and `src/init.web.ts`. `
|
|
196
|
+
- On web, `react-native-web`'s bundler resolution picks up `src/NativeJieba.web.ts` and `src/init.web.ts`. `prepareJieba()` lazily imports `jieba-wasm`, awaits its async wasm initializer, and routes the JS API to the wasm exports.
|
|
182
197
|
|
|
183
198
|
## Contributing
|
|
184
199
|
|
package/android/CMakeLists.txt
CHANGED
|
@@ -6,6 +6,7 @@ set (CMAKE_VERBOSE_MAKEFILE ON)
|
|
|
6
6
|
add_library(
|
|
7
7
|
react-native-jieba STATIC
|
|
8
8
|
../cpp/JiebaImpl.cpp
|
|
9
|
+
../cpp/JiebaDictAndroid.cpp
|
|
9
10
|
)
|
|
10
11
|
|
|
11
12
|
set_target_properties(
|
|
@@ -26,4 +27,5 @@ target_link_libraries(
|
|
|
26
27
|
react-native-jieba jsi
|
|
27
28
|
reactnative
|
|
28
29
|
react_codegen_JiebaSpec
|
|
30
|
+
fbjni
|
|
29
31
|
)
|
|
@@ -32,6 +32,10 @@ public abstract class NativeJiebaSpec extends ReactContextBaseJavaModule impleme
|
|
|
32
32
|
return NAME;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
36
|
+
@DoNotStrip
|
|
37
|
+
public abstract boolean isReady();
|
|
38
|
+
|
|
35
39
|
@ReactMethod
|
|
36
40
|
@DoNotStrip
|
|
37
41
|
public abstract void setDictPath(String path);
|
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
|
|
13
13
|
namespace facebook::react {
|
|
14
14
|
|
|
15
|
+
static facebook::jsi::Value __hostFunction_NativeJiebaSpecJSI_isReady(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
|
|
16
|
+
static jmethodID cachedMethodId = nullptr;
|
|
17
|
+
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, BooleanKind, "isReady", "()Z", args, count, cachedMethodId);
|
|
18
|
+
}
|
|
19
|
+
|
|
15
20
|
static facebook::jsi::Value __hostFunction_NativeJiebaSpecJSI_setDictPath(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
|
|
16
21
|
static jmethodID cachedMethodId = nullptr;
|
|
17
22
|
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, VoidKind, "setDictPath", "(Ljava/lang/String;)V", args, count, cachedMethodId);
|
|
@@ -64,6 +69,7 @@ static facebook::jsi::Value __hostFunction_NativeJiebaSpecJSI_find(facebook::jsi
|
|
|
64
69
|
|
|
65
70
|
NativeJiebaSpecJSI::NativeJiebaSpecJSI(const JavaTurboModule::InitParams ¶ms)
|
|
66
71
|
: JavaTurboModule(params) {
|
|
72
|
+
methodMap_["isReady"] = MethodMetadata {0, __hostFunction_NativeJiebaSpecJSI_isReady};
|
|
67
73
|
methodMap_["setDictPath"] = MethodMetadata {1, __hostFunction_NativeJiebaSpecJSI_setDictPath};
|
|
68
74
|
methodMap_["cut"] = MethodMetadata {2, __hostFunction_NativeJiebaSpecJSI_cut};
|
|
69
75
|
methodMap_["cutAll"] = MethodMetadata {1, __hostFunction_NativeJiebaSpecJSI_cutAll};
|
|
@@ -22,6 +22,7 @@ public:
|
|
|
22
22
|
|
|
23
23
|
protected:
|
|
24
24
|
NativeJiebaCxxSpec(std::shared_ptr<CallInvoker> jsInvoker) : TurboModule(std::string{NativeJiebaCxxSpec::kModuleName}, jsInvoker) {
|
|
25
|
+
methodMap_["isReady"] = MethodMetadata {.argCount = 0, .invoker = __isReady};
|
|
25
26
|
methodMap_["setDictPath"] = MethodMetadata {.argCount = 1, .invoker = __setDictPath};
|
|
26
27
|
methodMap_["cut"] = MethodMetadata {.argCount = 2, .invoker = __cut};
|
|
27
28
|
methodMap_["cutAll"] = MethodMetadata {.argCount = 1, .invoker = __cutAll};
|
|
@@ -35,6 +36,13 @@ protected:
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
private:
|
|
39
|
+
static jsi::Value __isReady(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* /*args*/, size_t /*count*/) {
|
|
40
|
+
static_assert(
|
|
41
|
+
bridging::getParameterCount(&T::isReady) == 1,
|
|
42
|
+
"Expected isReady(...) to have 1 parameters");
|
|
43
|
+
return bridging::callFromJs<bool>(rt, &T::isReady, static_cast<NativeJiebaCxxSpec*>(&turboModule)->jsInvoker_, static_cast<T*>(&turboModule));
|
|
44
|
+
}
|
|
45
|
+
|
|
38
46
|
static jsi::Value __setDictPath(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
|
|
39
47
|
static_assert(
|
|
40
48
|
bridging::getParameterCount(&T::setDictPath) == 2,
|
|
@@ -3,31 +3,24 @@ package com.jieba
|
|
|
3
3
|
import com.facebook.react.bridge.Promise
|
|
4
4
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
5
|
import com.facebook.react.module.annotations.ReactModule
|
|
6
|
-
import java.io.File
|
|
7
|
-
import java.io.FileOutputStream
|
|
8
6
|
|
|
9
7
|
@ReactModule(name = JiebaAndroidHelperModule.NAME)
|
|
10
8
|
class JiebaAndroidHelperModule(reactContext: ReactApplicationContext) :
|
|
11
9
|
NativeJiebaAndroidHelperSpec(reactContext) {
|
|
12
10
|
|
|
11
|
+
init {
|
|
12
|
+
// Capture an application context so the C++ side can lazily extract the
|
|
13
|
+
// dictionaries on first use, even if JS never calls prepareJieba().
|
|
14
|
+
JiebaDict.setContext(reactContext)
|
|
15
|
+
}
|
|
16
|
+
|
|
13
17
|
override fun getName(): String = NAME
|
|
14
18
|
|
|
15
19
|
override fun prepareDictDir(promise: Promise) {
|
|
16
20
|
val context = reactApplicationContext
|
|
17
21
|
Thread {
|
|
18
22
|
try {
|
|
19
|
-
|
|
20
|
-
if (!dictDir.exists()) dictDir.mkdirs()
|
|
21
|
-
for (filename in DICT_FILES) {
|
|
22
|
-
val target = File(dictDir, filename)
|
|
23
|
-
if (target.exists() && target.length() > 0) continue
|
|
24
|
-
context.assets.open(filename).use { input ->
|
|
25
|
-
FileOutputStream(target).use { output ->
|
|
26
|
-
input.copyTo(output)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
promise.resolve(dictDir.absolutePath)
|
|
23
|
+
promise.resolve(JiebaDict.extractDictDir(context))
|
|
31
24
|
} catch (t: Throwable) {
|
|
32
25
|
promise.reject("E_JIEBA_DICT_EXTRACT", t.message, t)
|
|
33
26
|
}
|
|
@@ -36,13 +29,5 @@ class JiebaAndroidHelperModule(reactContext: ReactApplicationContext) :
|
|
|
36
29
|
|
|
37
30
|
companion object {
|
|
38
31
|
const val NAME = "JiebaAndroidHelper"
|
|
39
|
-
private const val DICT_DIR_NAME = "jieba-dict"
|
|
40
|
-
private val DICT_FILES = arrayOf(
|
|
41
|
-
"jieba.dict.utf8",
|
|
42
|
-
"hmm_model.utf8",
|
|
43
|
-
"user.dict.utf8",
|
|
44
|
-
"idf.utf8",
|
|
45
|
-
"stop_words.utf8",
|
|
46
|
-
)
|
|
47
32
|
}
|
|
48
33
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package com.jieba
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import java.io.File
|
|
5
|
+
import java.io.FileOutputStream
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Shared dictionary extraction logic for Android.
|
|
9
|
+
*
|
|
10
|
+
* The cppjieba dictionaries ship as compressed APK assets, but cppjieba needs
|
|
11
|
+
* real filesystem paths. This object extracts them once into `filesDir/jieba-dict`
|
|
12
|
+
* and returns the directory.
|
|
13
|
+
*
|
|
14
|
+
* It is used from two places:
|
|
15
|
+
* - [JiebaAndroidHelperModule.prepareDictDir] — the async warm-up exposed to JS
|
|
16
|
+
* via `prepareJieba()`.
|
|
17
|
+
* - [extractDictDirFromNative] — a synchronous entry point invoked from C++
|
|
18
|
+
* (`JiebaImpl::getJieba`) on the very first segmentation call if `prepareJieba()`
|
|
19
|
+
* was never awaited, so that callers don't have to call `prepareJieba` at all.
|
|
20
|
+
*/
|
|
21
|
+
object JiebaDict {
|
|
22
|
+
const val DICT_DIR_NAME = "jieba-dict"
|
|
23
|
+
|
|
24
|
+
val DICT_FILES = arrayOf(
|
|
25
|
+
"jieba.dict.utf8",
|
|
26
|
+
"hmm_model.utf8",
|
|
27
|
+
"user.dict.utf8",
|
|
28
|
+
"idf.utf8",
|
|
29
|
+
"stop_words.utf8",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Application context captured at module construction so the native (C++)
|
|
34
|
+
* fallback can extract assets without a `ReactApplicationContext` handle.
|
|
35
|
+
*/
|
|
36
|
+
@Volatile
|
|
37
|
+
private var appContext: Context? = null
|
|
38
|
+
|
|
39
|
+
@JvmStatic
|
|
40
|
+
fun setContext(context: Context) {
|
|
41
|
+
if (appContext == null) {
|
|
42
|
+
appContext = context.applicationContext
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Synchronously extract the dictionary assets and return the directory path. */
|
|
47
|
+
@JvmStatic
|
|
48
|
+
fun extractDictDir(context: Context): String {
|
|
49
|
+
val dictDir = File(context.filesDir, DICT_DIR_NAME)
|
|
50
|
+
if (!dictDir.exists()) dictDir.mkdirs()
|
|
51
|
+
for (filename in DICT_FILES) {
|
|
52
|
+
val target = File(dictDir, filename)
|
|
53
|
+
if (target.exists() && target.length() > 0) continue
|
|
54
|
+
context.assets.open(filename).use { input ->
|
|
55
|
+
FileOutputStream(target).use { output ->
|
|
56
|
+
input.copyTo(output)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return dictDir.absolutePath
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Called from C++ via JNI. Returns the dict directory path, or an empty
|
|
65
|
+
* string if no context has been captured yet (extraction not possible).
|
|
66
|
+
*/
|
|
67
|
+
@JvmStatic
|
|
68
|
+
fun extractDictDirFromNative(): String {
|
|
69
|
+
val context = appContext ?: return ""
|
|
70
|
+
return extractDictDir(context)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -8,6 +8,10 @@ import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
|
8
8
|
|
|
9
9
|
class JiebaPackage : BaseReactPackage() {
|
|
10
10
|
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
11
|
+
// Capture an application context as early as possible so the C++ module can
|
|
12
|
+
// lazily extract the bundled dictionaries on first use without requiring a
|
|
13
|
+
// call to prepareJieba().
|
|
14
|
+
JiebaDict.setContext(reactContext)
|
|
11
15
|
return if (name == JiebaAndroidHelperModule.NAME) {
|
|
12
16
|
JiebaAndroidHelperModule(reactContext)
|
|
13
17
|
} else {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#include "JiebaDictAndroid.h"
|
|
2
|
+
|
|
3
|
+
#include <fbjni/fbjni.h>
|
|
4
|
+
|
|
5
|
+
namespace facebook::react {
|
|
6
|
+
|
|
7
|
+
std::string extractJiebaDictDirAndroid() {
|
|
8
|
+
using namespace facebook::jni;
|
|
9
|
+
|
|
10
|
+
// Calling a JNI method requires a thread attached to the JVM. cut()/tag()
|
|
11
|
+
// run on the JS thread, which fbjni attaches; ThreadScope makes this robust
|
|
12
|
+
// for any caller thread.
|
|
13
|
+
ThreadScope ts;
|
|
14
|
+
|
|
15
|
+
static const auto cls = findClassStatic("com/jieba/JiebaDict");
|
|
16
|
+
static const auto method =
|
|
17
|
+
cls->getStaticMethod<jstring()>("extractDictDirFromNative");
|
|
18
|
+
|
|
19
|
+
local_ref<jstring> result = method(cls);
|
|
20
|
+
if (!result) {
|
|
21
|
+
return "";
|
|
22
|
+
}
|
|
23
|
+
return result->toStdString();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
|
|
5
|
+
namespace facebook::react {
|
|
6
|
+
|
|
7
|
+
// Android-only: synchronously extracts the bundled dictionary assets (via a
|
|
8
|
+
// JNI call into Kotlin) and returns the directory path. Returns an empty string
|
|
9
|
+
// if extraction is not possible yet (e.g. no application context captured).
|
|
10
|
+
//
|
|
11
|
+
// Defined in JiebaDictAndroid.cpp and only compiled on Android.
|
|
12
|
+
std::string extractJiebaDictDirAndroid();
|
|
13
|
+
|
|
14
|
+
}
|
package/cpp/JiebaImpl.cpp
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
#include "JiebaImpl.h"
|
|
2
2
|
|
|
3
|
+
#ifdef __ANDROID__
|
|
4
|
+
#include "JiebaDictAndroid.h"
|
|
5
|
+
#endif
|
|
6
|
+
|
|
3
7
|
#include <cppjieba/Jieba.hpp>
|
|
4
8
|
|
|
5
9
|
#include <memory>
|
|
@@ -58,6 +62,14 @@ void JiebaImpl::setDictPathFromNative(const std::string& dictPath) {
|
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
|
|
65
|
+
bool JiebaImpl::isReady(jsi::Runtime& rt) {
|
|
66
|
+
std::lock_guard<std::mutex> lock(dictMutex());
|
|
67
|
+
// Ready once the engine is constructed, or once a dict path is resolved (the
|
|
68
|
+
// engine is then built lazily on first use). On iOS the path is set at +load;
|
|
69
|
+
// on Android it is set eagerly by prepareJieba or lazily on the first call.
|
|
70
|
+
return jiebaStorage() != nullptr || !dictPathStorage().empty();
|
|
71
|
+
}
|
|
72
|
+
|
|
61
73
|
void JiebaImpl::setDictPath(jsi::Runtime& rt, jsi::String path) {
|
|
62
74
|
setDictPathFromNative(path.utf8(rt));
|
|
63
75
|
}
|
|
@@ -65,6 +77,17 @@ void JiebaImpl::setDictPath(jsi::Runtime& rt, jsi::String path) {
|
|
|
65
77
|
cppjieba::Jieba& JiebaImpl::getJieba() {
|
|
66
78
|
std::lock_guard<std::mutex> lock(dictMutex());
|
|
67
79
|
if (!jiebaStorage()) {
|
|
80
|
+
#ifdef __ANDROID__
|
|
81
|
+
// If prepareJieba() was never awaited, extract the bundled assets now (a
|
|
82
|
+
// one-time, synchronous cost on the very first call) so callers don't have
|
|
83
|
+
// to call prepareJieba at all.
|
|
84
|
+
if (dictPathStorage().empty()) {
|
|
85
|
+
std::string extracted = extractJiebaDictDirAndroid();
|
|
86
|
+
if (!extracted.empty()) {
|
|
87
|
+
dictPathStorage() = extracted;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
#endif
|
|
68
91
|
const std::string& base = dictPathStorage();
|
|
69
92
|
if (base.empty()) {
|
|
70
93
|
throw std::runtime_error(
|
package/cpp/JiebaImpl.h
CHANGED
|
@@ -19,6 +19,7 @@ public:
|
|
|
19
19
|
|
|
20
20
|
static void setDictPathFromNative(const std::string& dictPath);
|
|
21
21
|
|
|
22
|
+
bool isReady(jsi::Runtime& rt);
|
|
22
23
|
void setDictPath(jsi::Runtime& rt, jsi::String path);
|
|
23
24
|
jsi::Array cut(jsi::Runtime& rt, jsi::String sentence, bool hmm);
|
|
24
25
|
jsi::Array cutAll(jsi::Runtime& rt, jsi::String sentence);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# CHANGELOG
|
|
2
|
+
|
|
3
|
+
## v5.6.7
|
|
4
|
+
|
|
5
|
+
+ docs: document dictionary file formats
|
|
6
|
+
+ test: add `了解` segmentation regression coverage
|
|
7
|
+
+ test: replace copyrighted benchmark test data with synthetic text
|
|
8
|
+
|
|
9
|
+
## v5.6.6
|
|
10
|
+
|
|
11
|
+
+ fix: add `习总书记` to the main dictionary for the reported `和习` segmentation case
|
|
12
|
+
+ chore: remove stray `韩玉鉴赏` entry from the default user dictionary
|
|
13
|
+
|
|
14
|
+
## v5.6.5
|
|
15
|
+
|
|
16
|
+
+ deps: upgrade limonp to v1.0.2
|
|
17
|
+
|
|
18
|
+
Older release history is preserved in git tags.
|