react-native-jieba 0.2.0 → 0.4.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/Jieba.podspec +17 -1
  3. package/README.md +70 -16
  4. package/android/CMakeLists.txt +3 -0
  5. package/android/build.gradle +32 -0
  6. package/android/generated/java/com/jieba/NativeJiebaSpec.java +4 -0
  7. package/android/generated/jni/JiebaSpec-generated.cpp +6 -0
  8. package/android/generated/jni/react/renderer/components/JiebaSpec/JiebaSpecJSI.h +8 -0
  9. package/android/src/main/java/com/jieba/JiebaAndroidHelperModule.kt +7 -22
  10. package/android/src/main/java/com/jieba/JiebaDict.kt +82 -0
  11. package/android/src/main/java/com/jieba/JiebaPackage.kt +4 -0
  12. package/cpp/JiebaDictAndroid.cpp +26 -0
  13. package/cpp/JiebaDictAndroid.h +14 -0
  14. package/cpp/JiebaEngine.cpp +18 -0
  15. package/cpp/JiebaEngine.h +128 -0
  16. package/cpp/JiebaImpl.cpp +31 -6
  17. package/cpp/JiebaImpl.h +4 -3
  18. package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec-generated.mm +7 -0
  19. package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec.h +1 -0
  20. package/ios/generated/ReactCodegen/JiebaSpecJSI.h +8 -0
  21. package/lib/module/NativeJieba.js.map +1 -1
  22. package/lib/module/NativeJieba.web.js +8 -2
  23. package/lib/module/NativeJieba.web.js.map +1 -1
  24. package/lib/module/index.js +1 -1
  25. package/lib/module/index.js.map +1 -1
  26. package/lib/module/init.js +33 -6
  27. package/lib/module/init.js.map +1 -1
  28. package/lib/module/init.web.js +19 -7
  29. package/lib/module/init.web.js.map +1 -1
  30. package/lib/module/jieba.js +12 -0
  31. package/lib/module/jieba.js.map +1 -1
  32. package/lib/typescript/src/NativeJieba.d.ts +1 -0
  33. package/lib/typescript/src/NativeJieba.d.ts.map +1 -1
  34. package/lib/typescript/src/NativeJieba.web.d.ts +1 -0
  35. package/lib/typescript/src/NativeJieba.web.d.ts.map +1 -1
  36. package/lib/typescript/src/index.d.ts +2 -1
  37. package/lib/typescript/src/index.d.ts.map +1 -1
  38. package/lib/typescript/src/init.d.ts +25 -2
  39. package/lib/typescript/src/init.d.ts.map +1 -1
  40. package/lib/typescript/src/init.web.d.ts +17 -3
  41. package/lib/typescript/src/init.web.d.ts.map +1 -1
  42. package/lib/typescript/src/jieba.d.ts +11 -0
  43. package/lib/typescript/src/jieba.d.ts.map +1 -1
  44. package/package.json +8 -1
  45. package/src/NativeJieba.ts +1 -0
  46. package/src/NativeJieba.web.ts +11 -3
  47. package/src/index.tsx +2 -1
  48. package/src/init.ts +35 -7
  49. package/src/init.web.ts +30 -9
  50. package/src/jieba.ts +11 -0
  51. package/cpp/cppjieba/LICENSE +0 -20
  52. package/cpp/cppjieba/deps/limonp/LICENSE +0 -20
  53. package/cpp/cppjieba/deps/limonp/include/limonp/ArgvContext.hpp +0 -70
  54. package/cpp/cppjieba/deps/limonp/include/limonp/Closure.hpp +0 -206
  55. package/cpp/cppjieba/deps/limonp/include/limonp/Colors.hpp +0 -31
  56. package/cpp/cppjieba/deps/limonp/include/limonp/Condition.hpp +0 -38
  57. package/cpp/cppjieba/deps/limonp/include/limonp/Config.hpp +0 -103
  58. package/cpp/cppjieba/deps/limonp/include/limonp/ForcePublic.hpp +0 -7
  59. package/cpp/cppjieba/deps/limonp/include/limonp/LocalVector.hpp +0 -139
  60. package/cpp/cppjieba/deps/limonp/include/limonp/Logging.hpp +0 -92
  61. package/cpp/cppjieba/deps/limonp/include/limonp/NonCopyable.hpp +0 -21
  62. package/cpp/cppjieba/deps/limonp/include/limonp/StdExtension.hpp +0 -157
  63. package/cpp/cppjieba/deps/limonp/include/limonp/StringUtil.hpp +0 -386
  64. package/cpp/cppjieba/dict/hmm_model.utf8 +0 -34
  65. package/cpp/cppjieba/dict/idf.utf8 +0 -258826
  66. package/cpp/cppjieba/dict/jieba.dict.utf8 +0 -348982
  67. package/cpp/cppjieba/dict/stop_words.utf8 +0 -1534
  68. package/cpp/cppjieba/dict/user.dict.utf8 +0 -3
  69. package/cpp/cppjieba/include/cppjieba/DictTrie.hpp +0 -322
  70. package/cpp/cppjieba/include/cppjieba/FullSegment.hpp +0 -94
  71. package/cpp/cppjieba/include/cppjieba/HMMModel.hpp +0 -131
  72. package/cpp/cppjieba/include/cppjieba/HMMSegment.hpp +0 -211
  73. package/cpp/cppjieba/include/cppjieba/Jieba.hpp +0 -169
  74. package/cpp/cppjieba/include/cppjieba/KeywordExtractor.hpp +0 -152
  75. package/cpp/cppjieba/include/cppjieba/MPSegment.hpp +0 -137
  76. package/cpp/cppjieba/include/cppjieba/MixSegment.hpp +0 -109
  77. package/cpp/cppjieba/include/cppjieba/PosTagger.hpp +0 -77
  78. package/cpp/cppjieba/include/cppjieba/PreFilter.hpp +0 -54
  79. package/cpp/cppjieba/include/cppjieba/QuerySegment.hpp +0 -89
  80. package/cpp/cppjieba/include/cppjieba/SegmentBase.hpp +0 -46
  81. package/cpp/cppjieba/include/cppjieba/SegmentTagged.hpp +0 -23
  82. package/cpp/cppjieba/include/cppjieba/TextRankExtractor.hpp +0 -192
  83. package/cpp/cppjieba/include/cppjieba/Trie.hpp +0 -200
  84. package/cpp/cppjieba/include/cppjieba/Unicode.hpp +0 -231
  85. package/cpp/cppjieba/include/cppjieba/UnicodeFile.hpp +0 -68
@@ -0,0 +1,128 @@
1
+ #pragma once
2
+
3
+ #include <cppjieba/DictTrie.hpp>
4
+ #include <cppjieba/FullSegment.hpp>
5
+ #include <cppjieba/HMMModel.hpp>
6
+ #include <cppjieba/HMMSegment.hpp>
7
+ #include <cppjieba/KeywordExtractor.hpp>
8
+ #include <cppjieba/MPSegment.hpp>
9
+ #include <cppjieba/MixSegment.hpp>
10
+ #include <cppjieba/QuerySegment.hpp>
11
+
12
+ #include <memory>
13
+ #include <string>
14
+ #include <utility>
15
+ #include <vector>
16
+
17
+ namespace rnjieba {
18
+
19
+ /**
20
+ * A cppjieba engine whose keyword extractor is built ON DEMAND.
21
+ *
22
+ * `cppjieba::Jieba` holds `KeywordExtractor` by value, so its constructor always
23
+ * loads `idf.utf8` and hard-fails (XCHECK) when that file is absent. That makes the
24
+ * ~5MB IDF dictionary a mandatory dependency of plain segmentation, even though only
25
+ * `extract()` ever reads it.
26
+ *
27
+ * This type composes the same cppjieba primitives directly, sharing one `DictTrie` and
28
+ * one `HMMModel` across every segmenter exactly as `cppjieba::Jieba` does, but defers the
29
+ * `KeywordExtractor` until `extract()` is first called. Apps that only tokenize therefore
30
+ * never load the IDF dictionary — and may omit the file from their bundle entirely (see
31
+ * `excludeIdfDict` in the README).
32
+ */
33
+ class JiebaEngine {
34
+ public:
35
+ JiebaEngine(
36
+ std::string dictPath,
37
+ std::string hmmModelPath,
38
+ std::string userDictPath,
39
+ std::string idfPath,
40
+ std::string stopWordPath
41
+ )
42
+ : idfPath_(std::move(idfPath)),
43
+ stopWordPath_(std::move(stopWordPath)),
44
+ dictTrie_(dictPath, userDictPath),
45
+ model_(hmmModelPath),
46
+ mpSeg_(&dictTrie_),
47
+ hmmSeg_(&model_),
48
+ mixSeg_(&dictTrie_, &model_),
49
+ fullSeg_(&dictTrie_),
50
+ querySeg_(&dictTrie_, &model_) {}
51
+
52
+ void Cut(const std::string& sentence, std::vector<std::string>& words, bool hmm) const {
53
+ mixSeg_.Cut(sentence, words, hmm);
54
+ }
55
+ void CutAll(const std::string& sentence, std::vector<std::string>& words) const {
56
+ fullSeg_.Cut(sentence, words);
57
+ }
58
+ void CutForSearch(const std::string& sentence, std::vector<std::string>& words, bool hmm) const {
59
+ querySeg_.Cut(sentence, words, hmm);
60
+ }
61
+ void CutHMM(const std::string& sentence, std::vector<std::string>& words) const {
62
+ hmmSeg_.Cut(sentence, words);
63
+ }
64
+ void CutSmall(
65
+ const std::string& sentence,
66
+ std::vector<std::string>& words,
67
+ size_t maxWordLen
68
+ ) const {
69
+ mpSeg_.Cut(sentence, words, maxWordLen);
70
+ }
71
+ void Tag(
72
+ const std::string& sentence,
73
+ std::vector<std::pair<std::string, std::string>>& words
74
+ ) const {
75
+ mixSeg_.Tag(sentence, words);
76
+ }
77
+ bool InsertUserWord(const std::string& word, const std::string& tag) {
78
+ return dictTrie_.InsertUserWord(word, tag);
79
+ }
80
+ bool Find(const std::string& word) {
81
+ return dictTrie_.Find(word);
82
+ }
83
+
84
+ /**
85
+ * The TF-IDF keyword extractor, constructed on first use.
86
+ *
87
+ * Reuses this engine's already-loaded trie and HMM model, so the only additional cost is
88
+ * reading `idf.utf8` and `stop_words.utf8`. Throws `std::runtime_error` when the IDF
89
+ * dictionary is missing — mirroring the web backend, where `extract()` is likewise
90
+ * unsupported because jieba-wasm ships no IDF data.
91
+ */
92
+ const cppjieba::KeywordExtractor& Extractor() {
93
+ if (!extractor_) {
94
+ if (!UnicodeFileExists(idfPath_)) {
95
+ throw std::runtime_error(
96
+ "react-native-jieba: extract() is unavailable because the IDF dictionary was not "
97
+ "found at '" + idfPath_ + "'. This build excluded it (see the `excludeIdfDict` "
98
+ "build flag). Re-enable the IDF dictionary to use extract(); cut()/tag() are "
99
+ "unaffected."
100
+ );
101
+ }
102
+ extractor_ = std::make_unique<cppjieba::KeywordExtractor>(
103
+ &dictTrie_, &model_, idfPath_, stopWordPath_
104
+ );
105
+ }
106
+ return *extractor_;
107
+ }
108
+
109
+ private:
110
+ static bool UnicodeFileExists(const std::string& path);
111
+
112
+ std::string idfPath_;
113
+ std::string stopWordPath_;
114
+
115
+ cppjieba::DictTrie dictTrie_;
116
+ cppjieba::HMMModel model_;
117
+
118
+ // All share the trie and model above.
119
+ cppjieba::MPSegment mpSeg_;
120
+ cppjieba::HMMSegment hmmSeg_;
121
+ cppjieba::MixSegment mixSeg_;
122
+ cppjieba::FullSegment fullSeg_;
123
+ cppjieba::QuerySegment querySeg_;
124
+
125
+ std::unique_ptr<cppjieba::KeywordExtractor> extractor_;
126
+ };
127
+
128
+ }
package/cpp/JiebaImpl.cpp CHANGED
@@ -1,6 +1,10 @@
1
1
  #include "JiebaImpl.h"
2
2
 
3
- #include <cppjieba/Jieba.hpp>
3
+ #ifdef __ANDROID__
4
+ #include "JiebaDictAndroid.h"
5
+ #endif
6
+
7
+ #include "JiebaEngine.h"
4
8
 
5
9
  #include <memory>
6
10
  #include <mutex>
@@ -23,8 +27,8 @@ std::string& dictPathStorage() {
23
27
  return p;
24
28
  }
25
29
 
26
- std::unique_ptr<cppjieba::Jieba>& jiebaStorage() {
27
- static std::unique_ptr<cppjieba::Jieba> j;
30
+ std::unique_ptr<rnjieba::JiebaEngine>& jiebaStorage() {
31
+ static std::unique_ptr<rnjieba::JiebaEngine> j;
28
32
  return j;
29
33
  }
30
34
 
@@ -58,13 +62,32 @@ 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
  }
64
76
 
65
- cppjieba::Jieba& JiebaImpl::getJieba() {
77
+ rnjieba::JiebaEngine& 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(
@@ -72,7 +95,9 @@ cppjieba::Jieba& JiebaImpl::getJieba() {
72
95
  "The native module failed to locate bundled dict files."
73
96
  );
74
97
  }
75
- jiebaStorage() = std::make_unique<cppjieba::Jieba>(
98
+ // The IDF path is recorded but NOT read here: JiebaEngine loads it only if extract() is
99
+ // called, so a build that omits idf.utf8 still tokenizes normally.
100
+ jiebaStorage() = std::make_unique<rnjieba::JiebaEngine>(
76
101
  joinPath(base, "jieba.dict.utf8"),
77
102
  joinPath(base, "hmm_model.utf8"),
78
103
  joinPath(base, "user.dict.utf8"),
@@ -130,7 +155,7 @@ jsi::Array JiebaImpl::tag(jsi::Runtime& rt, jsi::String sentence) {
130
155
  jsi::Array JiebaImpl::extract(jsi::Runtime& rt, jsi::String sentence, double topK) {
131
156
  std::vector<cppjieba::KeywordExtractor::Word> keywords;
132
157
  size_t n = topK <= 0 ? 5 : static_cast<size_t>(topK);
133
- getJieba().extractor.Extract(sentence.utf8(rt), keywords, n);
158
+ getJieba().Extractor().Extract(sentence.utf8(rt), keywords, n);
134
159
  jsi::Array arr(rt, keywords.size());
135
160
  for (size_t i = 0; i < keywords.size(); ++i) {
136
161
  jsi::Object obj(rt);
package/cpp/JiebaImpl.h CHANGED
@@ -6,8 +6,8 @@
6
6
  #include <mutex>
7
7
  #include <string>
8
8
 
9
- namespace cppjieba {
10
- class Jieba;
9
+ namespace rnjieba {
10
+ class JiebaEngine;
11
11
  }
12
12
 
13
13
  namespace facebook::react {
@@ -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);
@@ -31,7 +32,7 @@ public:
31
32
  bool find(jsi::Runtime& rt, jsi::String word);
32
33
 
33
34
  private:
34
- cppjieba::Jieba& getJieba();
35
+ rnjieba::JiebaEngine& getJieba();
35
36
  };
36
37
 
37
38
  }
@@ -26,6 +26,10 @@
26
26
 
27
27
  namespace facebook::react {
28
28
 
29
+ static facebook::jsi::Value __hostFunction_NativeJiebaSpecJSI_isReady(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
30
+ return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, BooleanKind, "isReady", @selector(isReady), args, count);
31
+ }
32
+
29
33
  static facebook::jsi::Value __hostFunction_NativeJiebaSpecJSI_setDictPath(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
30
34
  return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, "setDictPath", @selector(setDictPath:), args, count);
31
35
  }
@@ -69,6 +73,9 @@ namespace facebook::react {
69
73
  NativeJiebaSpecJSI::NativeJiebaSpecJSI(const ObjCTurboModule::InitParams &params)
70
74
  : ObjCTurboModule(params) {
71
75
 
76
+ methodMap_["isReady"] = MethodMetadata {0, __hostFunction_NativeJiebaSpecJSI_isReady};
77
+
78
+
72
79
  methodMap_["setDictPath"] = MethodMetadata {1, __hostFunction_NativeJiebaSpecJSI_setDictPath};
73
80
 
74
81
 
@@ -35,6 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
35
35
 
36
36
  @protocol NativeJiebaSpec <RCTBridgeModule, RCTTurboModule>
37
37
 
38
+ - (NSNumber *)isReady;
38
39
  - (void)setDictPath:(NSString *)path;
39
40
  - (NSArray<NSString *> *)cut:(NSString *)sentence
40
41
  hmm:(BOOL)hmm;
@@ -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,
@@ -1 +1 @@
1
- {"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeJieba.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;AAkBpE,eAAeA,mBAAmB,CAACC,YAAY,CAAO,OAAO,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeJieba.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;AAmBpE,eAAeA,mBAAmB,CAACC,YAAY,CAAO,OAAO,CAAC","ignoreList":[]}
@@ -3,20 +3,26 @@
3
3
  // `jieba-wasm` exposes the same segmentation primitives as cppjieba, but a
4
4
  // smaller surface: cut / cutAll / cutForSearch / tag / add_word. The remaining
5
5
  // native methods are polyfilled on top of those where the semantics allow, and
6
- // throw otherwise. The wasm module is loaded lazily by `initJieba()` (see
6
+ // throw otherwise. The wasm module is loaded lazily by `prepareJieba()` (see
7
7
  // `init.web.ts`) and handed to this shim via `setJiebaWasm`.
8
8
 
9
9
  let wasm = null;
10
10
  export function setJiebaWasm(module) {
11
11
  wasm = module;
12
12
  }
13
+ export function isJiebaWasmReady() {
14
+ return wasm !== null;
15
+ }
13
16
  function getWasm() {
14
17
  if (!wasm) {
15
- throw new Error('react-native-jieba: jieba-wasm is not initialized. ' + 'Call (and await) initJieba() before any segmentation call.');
18
+ throw new Error('react-native-jieba: jieba-wasm is not loaded yet. ' + 'On web you must call (and await) prepareJieba() before any ' + 'segmentation call.');
16
19
  }
17
20
  return wasm;
18
21
  }
19
22
  const Jieba = {
23
+ isReady() {
24
+ return isJiebaWasmReady();
25
+ },
20
26
  setDictPath() {
21
27
  // No-op on web: jieba-wasm bundles its own dictionary.
22
28
  },
@@ -1 +1 @@
1
- {"version":3,"names":["wasm","setJiebaWasm","module","getWasm","Error","Jieba","setDictPath","cut","sentence","hmm","cutAll","cut_all","cutForSearch","cut_for_search","cutHMM","cutSmall","_maxWordLen","tag","extract","insertUserWord","word","add_word","undefined","find","segments","length"],"sourceRoot":"../../src","sources":["NativeJieba.web.ts"],"mappings":";;AAEA;AACA;AACA;AACA;AACA;;AAaA,IAAIA,IAA4B,GAAG,IAAI;AAEvC,OAAO,SAASC,YAAYA,CAACC,MAAuB,EAAQ;EAC1DF,IAAI,GAAGE,MAAM;AACf;AAEA,SAASC,OAAOA,CAAA,EAAoB;EAClC,IAAI,CAACH,IAAI,EAAE;IACT,MAAM,IAAII,KAAK,CACb,qDAAqD,GACnD,4DACJ,CAAC;EACH;EACA,OAAOJ,IAAI;AACb;AAEA,MAAMK,KAAW,GAAG;EAClBC,WAAWA,CAAA,EAAG;IACZ;EAAA,CACD;EACDC,GAAGA,CAACC,QAAQ,EAAEC,GAAG,EAAE;IACjB,OAAON,OAAO,CAAC,CAAC,CAACI,GAAG,CAACC,QAAQ,EAAEC,GAAG,CAAC;EACrC,CAAC;EACDC,MAAMA,CAACF,QAAQ,EAAE;IACf,OAAOL,OAAO,CAAC,CAAC,CAACQ,OAAO,CAACH,QAAQ,CAAC;EACpC,CAAC;EACDI,YAAYA,CAACJ,QAAQ,EAAEC,GAAG,EAAE;IAC1B,OAAON,OAAO,CAAC,CAAC,CAACU,cAAc,CAACL,QAAQ,EAAEC,GAAG,CAAC;EAChD,CAAC;EACDK,MAAMA,CAACN,QAAQ,EAAE;IACf;IACA;IACA,OAAOL,OAAO,CAAC,CAAC,CAACI,GAAG,CAACC,QAAQ,EAAE,IAAI,CAAC;EACtC,CAAC;EACDO,QAAQA,CAACP,QAAQ,EAAEQ,WAAW,EAAE;IAC9B;IACA;IACA,OAAOb,OAAO,CAAC,CAAC,CAACI,GAAG,CAACC,QAAQ,EAAE,IAAI,CAAC;EACtC,CAAC;EACDS,GAAGA,CAACT,QAAQ,EAAE;IACZ,OAAOL,OAAO,CAAC,CAAC,CAACc,GAAG,CAACT,QAAQ,CAAC;EAChC,CAAC;EACDU,OAAOA,CAAA,EAAG;IACR,MAAM,IAAId,KAAK,CACb,mEAAmE,GACjE,iEACJ,CAAC;EACH,CAAC;EACDe,cAAcA,CAACC,IAAI,EAAEH,GAAG,EAAE;IACxB;IACA,OACEd,OAAO,CAAC,CAAC,CAACkB,QAAQ,CAACD,IAAI,EAAEE,SAAS,EAAEL,GAAG,KAAK,EAAE,GAAGK,SAAS,GAAGL,GAAG,CAAC,IAAI,CAAC;EAE1E,CAAC;EACDM,IAAIA,CAACH,IAAI,EAAE;IACT;IACA;IACA,MAAMI,QAAQ,GAAGrB,OAAO,CAAC,CAAC,CAACI,GAAG,CAACa,IAAI,EAAE,KAAK,CAAC;IAC3C,OAAOI,QAAQ,CAACC,MAAM,KAAK,CAAC,IAAID,QAAQ,CAAC,CAAC,CAAC,KAAKJ,IAAI;EACtD;AACF,CAAC;AAED,eAAef,KAAK","ignoreList":[]}
1
+ {"version":3,"names":["wasm","setJiebaWasm","module","isJiebaWasmReady","getWasm","Error","Jieba","isReady","setDictPath","cut","sentence","hmm","cutAll","cut_all","cutForSearch","cut_for_search","cutHMM","cutSmall","_maxWordLen","tag","extract","insertUserWord","word","add_word","undefined","find","segments","length"],"sourceRoot":"../../src","sources":["NativeJieba.web.ts"],"mappings":";;AAEA;AACA;AACA;AACA;AACA;;AAaA,IAAIA,IAA4B,GAAG,IAAI;AAEvC,OAAO,SAASC,YAAYA,CAACC,MAAuB,EAAQ;EAC1DF,IAAI,GAAGE,MAAM;AACf;AAEA,OAAO,SAASC,gBAAgBA,CAAA,EAAY;EAC1C,OAAOH,IAAI,KAAK,IAAI;AACtB;AAEA,SAASI,OAAOA,CAAA,EAAoB;EAClC,IAAI,CAACJ,IAAI,EAAE;IACT,MAAM,IAAIK,KAAK,CACb,oDAAoD,GAClD,6DAA6D,GAC7D,oBACJ,CAAC;EACH;EACA,OAAOL,IAAI;AACb;AAEA,MAAMM,KAAW,GAAG;EAClBC,OAAOA,CAAA,EAAG;IACR,OAAOJ,gBAAgB,CAAC,CAAC;EAC3B,CAAC;EACDK,WAAWA,CAAA,EAAG;IACZ;EAAA,CACD;EACDC,GAAGA,CAACC,QAAQ,EAAEC,GAAG,EAAE;IACjB,OAAOP,OAAO,CAAC,CAAC,CAACK,GAAG,CAACC,QAAQ,EAAEC,GAAG,CAAC;EACrC,CAAC;EACDC,MAAMA,CAACF,QAAQ,EAAE;IACf,OAAON,OAAO,CAAC,CAAC,CAACS,OAAO,CAACH,QAAQ,CAAC;EACpC,CAAC;EACDI,YAAYA,CAACJ,QAAQ,EAAEC,GAAG,EAAE;IAC1B,OAAOP,OAAO,CAAC,CAAC,CAACW,cAAc,CAACL,QAAQ,EAAEC,GAAG,CAAC;EAChD,CAAC;EACDK,MAAMA,CAACN,QAAQ,EAAE;IACf;IACA;IACA,OAAON,OAAO,CAAC,CAAC,CAACK,GAAG,CAACC,QAAQ,EAAE,IAAI,CAAC;EACtC,CAAC;EACDO,QAAQA,CAACP,QAAQ,EAAEQ,WAAW,EAAE;IAC9B;IACA;IACA,OAAOd,OAAO,CAAC,CAAC,CAACK,GAAG,CAACC,QAAQ,EAAE,IAAI,CAAC;EACtC,CAAC;EACDS,GAAGA,CAACT,QAAQ,EAAE;IACZ,OAAON,OAAO,CAAC,CAAC,CAACe,GAAG,CAACT,QAAQ,CAAC;EAChC,CAAC;EACDU,OAAOA,CAAA,EAAG;IACR,MAAM,IAAIf,KAAK,CACb,mEAAmE,GACjE,iEACJ,CAAC;EACH,CAAC;EACDgB,cAAcA,CAACC,IAAI,EAAEH,GAAG,EAAE;IACxB;IACA,OACEf,OAAO,CAAC,CAAC,CAACmB,QAAQ,CAACD,IAAI,EAAEE,SAAS,EAAEL,GAAG,KAAK,EAAE,GAAGK,SAAS,GAAGL,GAAG,CAAC,IAAI,CAAC;EAE1E,CAAC;EACDM,IAAIA,CAACH,IAAI,EAAE;IACT;IACA;IACA,MAAMI,QAAQ,GAAGtB,OAAO,CAAC,CAAC,CAACK,GAAG,CAACa,IAAI,EAAE,KAAK,CAAC;IAC3C,OAAOI,QAAQ,CAACC,MAAM,KAAK,CAAC,IAAID,QAAQ,CAAC,CAAC,CAAC,KAAKJ,IAAI;EACtD;AACF,CAAC;AAED,eAAehB,KAAK","ignoreList":[]}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
 
3
3
  export { cut, cutAll, cutForSearch, cutHMM, cutSmall, tag, extract, insertUserWord, find } from "./jieba.js";
4
- export { initJieba } from './init';
4
+ export { prepareJieba, isJiebaReady } from './init';
5
5
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["cut","cutAll","cutForSearch","cutHMM","cutSmall","tag","extract","insertUserWord","find","initJieba"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SACEA,GAAG,EACHC,MAAM,EACNC,YAAY,EACZC,MAAM,EACNC,QAAQ,EACRC,GAAG,EACHC,OAAO,EACPC,cAAc,EACdC,IAAI,QACC,YAAS;AAEhB,SAASC,SAAS,QAAQ,QAAQ","ignoreList":[]}
1
+ {"version":3,"names":["cut","cutAll","cutForSearch","cutHMM","cutSmall","tag","extract","insertUserWord","find","prepareJieba","isJiebaReady"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SACEA,GAAG,EACHC,MAAM,EACNC,YAAY,EACZC,MAAM,EACNC,QAAQ,EACRC,GAAG,EACHC,OAAO,EACPC,cAAc,EACdC,IAAI,QACC,YAAS;AAEhB,SAASC,YAAY,EAAEC,YAAY,QAAQ,QAAQ","ignoreList":[]}
@@ -3,10 +3,26 @@
3
3
  import { Platform } from 'react-native';
4
4
  import Jieba from './NativeJieba';
5
5
  import JiebaAndroidHelper from "./NativeJiebaAndroidHelper.js";
6
- let initPromise = null;
7
- export function initJieba(_options = {}) {
8
- if (initPromise) return initPromise;
9
- initPromise = (async () => {
6
+ let preparePromise = null;
7
+
8
+ /**
9
+ * Optional on native (iOS/Android): the dictionary is located/extracted
10
+ * automatically on the first segmentation call, so you can call `cut`, `tag`,
11
+ * etc. directly without ever calling `prepareJieba`.
12
+ *
13
+ * Calling it is still useful as a *warm-up*: on Android's very first run the
14
+ * dictionary assets are extracted from the APK, and doing that work here
15
+ * (asynchronously, up front) avoids a one-time blocking pause on the first
16
+ * `cut`/`tag` call.
17
+ *
18
+ * On web it is **required** — it loads and instantiates the `jieba-wasm`
19
+ * binary, which cannot be done synchronously.
20
+ *
21
+ * Idempotent: repeated calls return the same promise.
22
+ */
23
+ export function prepareJieba(_options = {}) {
24
+ if (preparePromise) return preparePromise;
25
+ preparePromise = (async () => {
10
26
  if (Platform.OS === 'android') {
11
27
  if (!JiebaAndroidHelper) {
12
28
  throw new Error('react-native-jieba: JiebaAndroidHelper native module is missing.');
@@ -16,9 +32,20 @@ export function initJieba(_options = {}) {
16
32
  }
17
33
  // iOS: OnLoad.mm already configured the dict path from the bundle.
18
34
  })().catch(err => {
19
- initPromise = null;
35
+ preparePromise = null;
20
36
  throw err;
21
37
  });
22
- return initPromise;
38
+ return preparePromise;
39
+ }
40
+
41
+ /**
42
+ * Whether jieba is ready to segment synchronously.
43
+ *
44
+ * Backed by the native engine state (not by whether `prepareJieba` was called),
45
+ * so it stays correct even when the dictionary is resolved lazily on the first
46
+ * `cut`/`tag` call. On iOS this is `true` from app launch.
47
+ */
48
+ export function isJiebaReady() {
49
+ return Jieba.isReady();
23
50
  }
24
51
  //# sourceMappingURL=init.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["Platform","Jieba","JiebaAndroidHelper","initPromise","initJieba","_options","OS","Error","path","prepareDictDir","setDictPath","catch","err"],"sourceRoot":"../../src","sources":["init.ts"],"mappings":";;AAAA,SAASA,QAAQ,QAAQ,cAAc;AACvC,OAAOC,KAAK,MAAM,eAAe;AACjC,OAAOC,kBAAkB,MAAM,+BAA4B;AAW3D,IAAIC,WAAiC,GAAG,IAAI;AAE5C,OAAO,SAASC,SAASA,CAACC,QAA0B,GAAG,CAAC,CAAC,EAAiB;EACxE,IAAIF,WAAW,EAAE,OAAOA,WAAW;EACnCA,WAAW,GAAG,CAAC,YAAY;IACzB,IAAIH,QAAQ,CAACM,EAAE,KAAK,SAAS,EAAE;MAC7B,IAAI,CAACJ,kBAAkB,EAAE;QACvB,MAAM,IAAIK,KAAK,CACb,kEACF,CAAC;MACH;MACA,MAAMC,IAAI,GAAG,MAAMN,kBAAkB,CAACO,cAAc,CAAC,CAAC;MACtDR,KAAK,CAACS,WAAW,CAACF,IAAI,CAAC;IACzB;IACA;EACF,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBT,WAAW,GAAG,IAAI;IAClB,MAAMS,GAAG;EACX,CAAC,CAAC;EACF,OAAOT,WAAW;AACpB","ignoreList":[]}
1
+ {"version":3,"names":["Platform","Jieba","JiebaAndroidHelper","preparePromise","prepareJieba","_options","OS","Error","path","prepareDictDir","setDictPath","catch","err","isJiebaReady","isReady"],"sourceRoot":"../../src","sources":["init.ts"],"mappings":";;AAAA,SAASA,QAAQ,QAAQ,cAAc;AACvC,OAAOC,KAAK,MAAM,eAAe;AACjC,OAAOC,kBAAkB,MAAM,+BAA4B;AAW3D,IAAIC,cAAoC,GAAG,IAAI;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAC1BC,QAA6B,GAAG,CAAC,CAAC,EACnB;EACf,IAAIF,cAAc,EAAE,OAAOA,cAAc;EACzCA,cAAc,GAAG,CAAC,YAAY;IAC5B,IAAIH,QAAQ,CAACM,EAAE,KAAK,SAAS,EAAE;MAC7B,IAAI,CAACJ,kBAAkB,EAAE;QACvB,MAAM,IAAIK,KAAK,CACb,kEACF,CAAC;MACH;MACA,MAAMC,IAAI,GAAG,MAAMN,kBAAkB,CAACO,cAAc,CAAC,CAAC;MACtDR,KAAK,CAACS,WAAW,CAACF,IAAI,CAAC;IACzB;IACA;EACF,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBT,cAAc,GAAG,IAAI;IACrB,MAAMS,GAAG;EACX,CAAC,CAAC;EACF,OAAOT,cAAc;AACvB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASU,YAAYA,CAAA,EAAY;EACtC,OAAOZ,KAAK,CAACa,OAAO,CAAC,CAAC;AACxB","ignoreList":[]}
@@ -1,10 +1,17 @@
1
1
  "use strict";
2
2
 
3
- import { setJiebaWasm } from "./NativeJieba.web.js";
4
- let initPromise = null;
5
- export function initJieba(options = {}) {
6
- if (initPromise) return initPromise;
7
- initPromise = (async () => {
3
+ import { setJiebaWasm, isJiebaWasmReady } from "./NativeJieba.web.js";
4
+ let preparePromise = null;
5
+
6
+ /**
7
+ * **Required on web**: loads and instantiates the `jieba-wasm` binary (which
8
+ * cannot be done synchronously). Await it once before calling `cut`, `tag`, etc.
9
+ *
10
+ * Idempotent: repeated calls return the same promise.
11
+ */
12
+ export function prepareJieba(options = {}) {
13
+ if (preparePromise) return preparePromise;
14
+ preparePromise = (async () => {
8
15
  // `jieba-wasm` resolves to its `web` build under the `browser`/`import`
9
16
  // conditions, whose default export is an async initializer that fetches
10
17
  // and instantiates the wasm binary.
@@ -20,9 +27,14 @@ export function initJieba(options = {}) {
20
27
  await wasm.default(options.wasmUrl);
21
28
  setJiebaWasm(wasm);
22
29
  })().catch(err => {
23
- initPromise = null;
30
+ preparePromise = null;
24
31
  throw err;
25
32
  });
26
- return initPromise;
33
+ return preparePromise;
34
+ }
35
+
36
+ /** Whether the wasm module has been loaded and jieba can segment. */
37
+ export function isJiebaReady() {
38
+ return isJiebaWasmReady();
27
39
  }
28
40
  //# sourceMappingURL=init.web.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["setJiebaWasm","initPromise","initJieba","options","wasm","default","wasmUrl","catch","err"],"sourceRoot":"../../src","sources":["init.web.ts"],"mappings":";;AAAA,SAASA,YAAY,QAA8B,sBAAmB;AAKtE,IAAIC,WAAiC,GAAG,IAAI;AAE5C,OAAO,SAASC,SAASA,CAACC,OAAyB,GAAG,CAAC,CAAC,EAAiB;EACvE,IAAIF,WAAW,EAAE,OAAOA,WAAW;EACnCA,WAAW,GAAG,CAAC,YAAY;IACzB;IACA;IACA;IACA,MAAMG,IAAI,GAAI,MAAM,MAAM,CAAC,YAAY,CAEpB;;IAEnB;IACA;IACA;IACA;IACA;IACA;IACA;IACA,MAAMA,IAAI,CAACC,OAAO,CAACF,OAAO,CAACG,OAAO,CAAC;IACnCN,YAAY,CAACI,IAAI,CAAC;EACpB,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBP,WAAW,GAAG,IAAI;IAClB,MAAMO,GAAG;EACX,CAAC,CAAC;EACF,OAAOP,WAAW;AACpB","ignoreList":[]}
1
+ {"version":3,"names":["setJiebaWasm","isJiebaWasmReady","preparePromise","prepareJieba","options","wasm","default","wasmUrl","catch","err","isJiebaReady"],"sourceRoot":"../../src","sources":["init.web.ts"],"mappings":";;AAAA,SACEA,YAAY,EACZC,gBAAgB,QAEX,sBAAmB;AAW1B,IAAIC,cAAoC,GAAG,IAAI;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAACC,OAA4B,GAAG,CAAC,CAAC,EAAiB;EAC7E,IAAIF,cAAc,EAAE,OAAOA,cAAc;EACzCA,cAAc,GAAG,CAAC,YAAY;IAC5B;IACA;IACA;IACA,MAAMG,IAAI,GAAI,MAAM,MAAM,CAAC,YAAY,CAEpB;;IAEnB;IACA;IACA;IACA;IACA;IACA;IACA;IACA,MAAMA,IAAI,CAACC,OAAO,CAACF,OAAO,CAACG,OAAO,CAAC;IACnCP,YAAY,CAACK,IAAI,CAAC;EACpB,CAAC,EAAE,CAAC,CAACG,KAAK,CAAEC,GAAG,IAAK;IAClBP,cAAc,GAAG,IAAI;IACrB,MAAMO,GAAG;EACX,CAAC,CAAC;EACF,OAAOP,cAAc;AACvB;;AAEA;AACA,OAAO,SAASQ,YAAYA,CAAA,EAAY;EACtC,OAAOT,gBAAgB,CAAC,CAAC;AAC3B","ignoreList":[]}
@@ -19,6 +19,18 @@ export function cutSmall(sentence, maxWordLen) {
19
19
  export function tag(sentence) {
20
20
  return Jieba.tag(sentence);
21
21
  }
22
+
23
+ /**
24
+ * TF-IDF keyword extraction.
25
+ *
26
+ * Requires the IDF dictionary, which is loaded lazily on the first call (every other API works
27
+ * without it). Throws when that dictionary is unavailable:
28
+ * - on **web**, always — jieba-wasm ships no IDF data;
29
+ * - on **native**, only in builds that opted out of bundling it via `RN_JIEBA_EXCLUDE_IDF_DICT=1`
30
+ * (iOS) or `rnJiebaExcludeIdfDict=true` (Android), which drops ~5MB from the app.
31
+ *
32
+ * Guard it in cross-platform code, or in any app that may be built with the dictionary excluded.
33
+ */
22
34
  export function extract(sentence, topK = 5) {
23
35
  return Jieba.extract(sentence, topK);
24
36
  }
@@ -1 +1 @@
1
- {"version":3,"names":["Jieba","cut","sentence","hmm","cutAll","cutForSearch","cutHMM","cutSmall","maxWordLen","tag","extract","topK","insertUserWord","word","find"],"sourceRoot":"../../src","sources":["jieba.ts"],"mappings":";;AAAA,OAAOA,KAAK,MAAM,eAAe;AAKjC,OAAO,SAASC,GAAGA,CAACC,QAAgB,EAAEC,GAAY,GAAG,IAAI,EAAY;EACnE,OAAOH,KAAK,CAACC,GAAG,CAACC,QAAQ,EAAEC,GAAG,CAAC;AACjC;AAEA,OAAO,SAASC,MAAMA,CAACF,QAAgB,EAAY;EACjD,OAAOF,KAAK,CAACI,MAAM,CAACF,QAAQ,CAAC;AAC/B;AAEA,OAAO,SAASG,YAAYA,CAACH,QAAgB,EAAEC,GAAY,GAAG,IAAI,EAAY;EAC5E,OAAOH,KAAK,CAACK,YAAY,CAACH,QAAQ,EAAEC,GAAG,CAAC;AAC1C;AAEA,OAAO,SAASG,MAAMA,CAACJ,QAAgB,EAAY;EACjD,OAAOF,KAAK,CAACM,MAAM,CAACJ,QAAQ,CAAC;AAC/B;AAEA,OAAO,SAASK,QAAQA,CAACL,QAAgB,EAAEM,UAAkB,EAAY;EACvE,OAAOR,KAAK,CAACO,QAAQ,CAACL,QAAQ,EAAEM,UAAU,CAAC;AAC7C;AAEA,OAAO,SAASC,GAAGA,CAACP,QAAgB,EAAY;EAC9C,OAAOF,KAAK,CAACS,GAAG,CAACP,QAAQ,CAAC;AAC5B;AAEA,OAAO,SAASQ,OAAOA,CAACR,QAAgB,EAAES,IAAY,GAAG,CAAC,EAAa;EACrE,OAAOX,KAAK,CAACU,OAAO,CAACR,QAAQ,EAAES,IAAI,CAAC;AACtC;AAEA,OAAO,SAASC,cAAcA,CAACC,IAAY,EAAEJ,GAAW,GAAG,EAAE,EAAW;EACtE,OAAOT,KAAK,CAACY,cAAc,CAACC,IAAI,EAAEJ,GAAG,CAAC;AACxC;AAEA,OAAO,SAASK,IAAIA,CAACD,IAAY,EAAW;EAC1C,OAAOb,KAAK,CAACc,IAAI,CAACD,IAAI,CAAC;AACzB","ignoreList":[]}
1
+ {"version":3,"names":["Jieba","cut","sentence","hmm","cutAll","cutForSearch","cutHMM","cutSmall","maxWordLen","tag","extract","topK","insertUserWord","word","find"],"sourceRoot":"../../src","sources":["jieba.ts"],"mappings":";;AAAA,OAAOA,KAAK,MAAM,eAAe;AAKjC,OAAO,SAASC,GAAGA,CAACC,QAAgB,EAAEC,GAAY,GAAG,IAAI,EAAY;EACnE,OAAOH,KAAK,CAACC,GAAG,CAACC,QAAQ,EAAEC,GAAG,CAAC;AACjC;AAEA,OAAO,SAASC,MAAMA,CAACF,QAAgB,EAAY;EACjD,OAAOF,KAAK,CAACI,MAAM,CAACF,QAAQ,CAAC;AAC/B;AAEA,OAAO,SAASG,YAAYA,CAACH,QAAgB,EAAEC,GAAY,GAAG,IAAI,EAAY;EAC5E,OAAOH,KAAK,CAACK,YAAY,CAACH,QAAQ,EAAEC,GAAG,CAAC;AAC1C;AAEA,OAAO,SAASG,MAAMA,CAACJ,QAAgB,EAAY;EACjD,OAAOF,KAAK,CAACM,MAAM,CAACJ,QAAQ,CAAC;AAC/B;AAEA,OAAO,SAASK,QAAQA,CAACL,QAAgB,EAAEM,UAAkB,EAAY;EACvE,OAAOR,KAAK,CAACO,QAAQ,CAACL,QAAQ,EAAEM,UAAU,CAAC;AAC7C;AAEA,OAAO,SAASC,GAAGA,CAACP,QAAgB,EAAY;EAC9C,OAAOF,KAAK,CAACS,GAAG,CAACP,QAAQ,CAAC;AAC5B;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASQ,OAAOA,CAACR,QAAgB,EAAES,IAAY,GAAG,CAAC,EAAa;EACrE,OAAOX,KAAK,CAACU,OAAO,CAACR,QAAQ,EAAES,IAAI,CAAC;AACtC;AAEA,OAAO,SAASC,cAAcA,CAACC,IAAY,EAAEJ,GAAW,GAAG,EAAE,EAAW;EACtE,OAAOT,KAAK,CAACY,cAAc,CAACC,IAAI,EAAEJ,GAAG,CAAC;AACxC;AAEA,OAAO,SAASK,IAAIA,CAACD,IAAY,EAAW;EAC1C,OAAOb,KAAK,CAACc,IAAI,CAACD,IAAI,CAAC;AACzB","ignoreList":[]}
@@ -1,5 +1,6 @@
1
1
  import { type TurboModule } from 'react-native';
2
2
  export interface Spec extends TurboModule {
3
+ isReady(): boolean;
3
4
  setDictPath(path: string): void;
4
5
  cut(sentence: string, hmm: boolean): string[];
5
6
  cutAll(sentence: string): string[];
@@ -1 +1 @@
1
- {"version":3,"file":"NativeJieba.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IAC9C,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IACvD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzD,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5D,OAAO,CACL,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACX,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC3C,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC7B;;AAED,wBAA+D"}
1
+ {"version":3,"file":"NativeJieba.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,OAAO,IAAI,OAAO,CAAC;IACnB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IAC9C,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAAC;IACvD,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzD,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5D,OAAO,CACL,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,GACX,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC3C,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAC7B;;AAED,wBAA+D"}
@@ -10,6 +10,7 @@ export type JiebaWasmModule = {
10
10
  add_word(word: string, freq?: number | null, tag?: string | null): number;
11
11
  };
12
12
  export declare function setJiebaWasm(module: JiebaWasmModule): void;
13
+ export declare function isJiebaWasmReady(): boolean;
13
14
  declare const Jieba: Spec;
14
15
  export default Jieba;
15
16
  //# sourceMappingURL=NativeJieba.web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"NativeJieba.web.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAQ1C,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAClD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAChC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAC7D,GAAG,CACD,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GACnB,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC;CAC3E,CAAC;AAIF,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAE1D;AAYD,QAAA,MAAM,KAAK,EAAE,IA4CZ,CAAC;AAEF,eAAe,KAAK,CAAC"}
1
+ {"version":3,"file":"NativeJieba.web.d.ts","sourceRoot":"","sources":["../../../src/NativeJieba.web.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAQ1C,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAClD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAChC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;IAC7D,GAAG,CACD,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI,GACnB,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACxC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC;CAC3E,CAAC;AAIF,wBAAgB,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI,CAE1D;AAED,wBAAgB,gBAAgB,IAAI,OAAO,CAE1C;AAaD,QAAA,MAAM,KAAK,EAAE,IA+CZ,CAAC;AAEF,eAAe,KAAK,CAAC"}
@@ -1,4 +1,5 @@
1
1
  export { cut, cutAll, cutForSearch, cutHMM, cutSmall, tag, extract, insertUserWord, find, } from './jieba';
2
2
  export type { Tagged, Keyword } from './jieba';
3
- export { initJieba } from './init';
3
+ export { prepareJieba, isJiebaReady } from './init';
4
+ export type { PrepareJiebaOptions } from './init';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,MAAM,EACN,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,GAAG,EACH,OAAO,EACP,cAAc,EACd,IAAI,GACL,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,MAAM,EACN,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,GAAG,EACH,OAAO,EACP,cAAc,EACd,IAAI,GACL,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACpD,YAAY,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC"}
@@ -1,4 +1,4 @@
1
- export type InitJiebaOptions = {
1
+ export type PrepareJiebaOptions = {
2
2
  /**
3
3
  * Web only: URL (or fetch input) for the jieba-wasm `.wasm` binary. Most
4
4
  * bundlers resolve it automatically, so this is only needed when the asset
@@ -6,5 +6,28 @@ export type InitJiebaOptions = {
6
6
  */
7
7
  wasmUrl?: string | URL | Request;
8
8
  };
9
- export declare function initJieba(_options?: InitJiebaOptions): Promise<void>;
9
+ /**
10
+ * Optional on native (iOS/Android): the dictionary is located/extracted
11
+ * automatically on the first segmentation call, so you can call `cut`, `tag`,
12
+ * etc. directly without ever calling `prepareJieba`.
13
+ *
14
+ * Calling it is still useful as a *warm-up*: on Android's very first run the
15
+ * dictionary assets are extracted from the APK, and doing that work here
16
+ * (asynchronously, up front) avoids a one-time blocking pause on the first
17
+ * `cut`/`tag` call.
18
+ *
19
+ * On web it is **required** — it loads and instantiates the `jieba-wasm`
20
+ * binary, which cannot be done synchronously.
21
+ *
22
+ * Idempotent: repeated calls return the same promise.
23
+ */
24
+ export declare function prepareJieba(_options?: PrepareJiebaOptions): Promise<void>;
25
+ /**
26
+ * Whether jieba is ready to segment synchronously.
27
+ *
28
+ * Backed by the native engine state (not by whether `prepareJieba` was called),
29
+ * so it stays correct even when the dictionary is resolved lazily on the first
30
+ * `cut`/`tag` call. On iOS this is `true` from app launch.
31
+ */
32
+ export declare function isJiebaReady(): boolean;
10
33
  //# sourceMappingURL=init.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../src/init.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;CAClC,CAAC;AAIF,wBAAgB,SAAS,CAAC,QAAQ,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBxE"}
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../src/init.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;CAClC,CAAC;AAIF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,GAAE,mBAAwB,GACjC,OAAO,CAAC,IAAI,CAAC,CAkBf;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,IAAI,OAAO,CAEtC"}
@@ -1,4 +1,18 @@
1
- import type { InitJiebaOptions } from './init';
2
- export type { InitJiebaOptions } from './init';
3
- export declare function initJieba(options?: InitJiebaOptions): Promise<void>;
1
+ export type PrepareJiebaOptions = {
2
+ /**
3
+ * URL (or fetch input) for the jieba-wasm `.wasm` binary. Most bundlers
4
+ * resolve it automatically, so this is only needed when the asset is served
5
+ * from a custom location.
6
+ */
7
+ wasmUrl?: string | URL | Request;
8
+ };
9
+ /**
10
+ * **Required on web**: loads and instantiates the `jieba-wasm` binary (which
11
+ * cannot be done synchronously). Await it once before calling `cut`, `tag`, etc.
12
+ *
13
+ * Idempotent: repeated calls return the same promise.
14
+ */
15
+ export declare function prepareJieba(options?: PrepareJiebaOptions): Promise<void>;
16
+ /** Whether the wasm module has been loaded and jieba can segment. */
17
+ export declare function isJiebaReady(): boolean;
4
18
  //# sourceMappingURL=init.web.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"init.web.d.ts","sourceRoot":"","sources":["../../../src/init.web.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAE/C,YAAY,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAI/C,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwBvE"}
1
+ {"version":3,"file":"init.web.d.ts","sourceRoot":"","sources":["../../../src/init.web.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC;CAClC,CAAC;AAIF;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwB7E;AAED,qEAAqE;AACrE,wBAAgB,YAAY,IAAI,OAAO,CAEtC"}
@@ -12,6 +12,17 @@ export declare function cutForSearch(sentence: string, hmm?: boolean): string[];
12
12
  export declare function cutHMM(sentence: string): string[];
13
13
  export declare function cutSmall(sentence: string, maxWordLen: number): string[];
14
14
  export declare function tag(sentence: string): Tagged[];
15
+ /**
16
+ * TF-IDF keyword extraction.
17
+ *
18
+ * Requires the IDF dictionary, which is loaded lazily on the first call (every other API works
19
+ * without it). Throws when that dictionary is unavailable:
20
+ * - on **web**, always — jieba-wasm ships no IDF data;
21
+ * - on **native**, only in builds that opted out of bundling it via `RN_JIEBA_EXCLUDE_IDF_DICT=1`
22
+ * (iOS) or `rnJiebaExcludeIdfDict=true` (Android), which drops ~5MB from the app.
23
+ *
24
+ * Guard it in cross-platform code, or in any app that may be built with the dictionary excluded.
25
+ */
15
26
  export declare function extract(sentence: string, topK?: number): Keyword[];
16
27
  export declare function insertUserWord(word: string, tag?: string): boolean;
17
28
  export declare function find(word: string): boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"jieba.d.ts","sourceRoot":"","sources":["../../../src/jieba.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AACnD,MAAM,MAAM,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,wBAAgB,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAE,OAAc,GAAG,MAAM,EAAE,CAEnE;AAED,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAEjD;AAED,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAE,OAAc,GAAG,MAAM,EAAE,CAE5E;AAED,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAEjD;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAEvE;AAED,wBAAgB,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAE9C;AAED,wBAAgB,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,MAAU,GAAG,OAAO,EAAE,CAErE;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,GAAE,MAAW,GAAG,OAAO,CAEtE;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE1C"}
1
+ {"version":3,"file":"jieba.d.ts","sourceRoot":"","sources":["../../../src/jieba.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AACnD,MAAM,MAAM,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,wBAAgB,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAE,OAAc,GAAG,MAAM,EAAE,CAEnE;AAED,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAEjD;AAED,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,GAAE,OAAc,GAAG,MAAM,EAAE,CAE5E;AAED,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAEjD;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAEvE;AAED,wBAAgB,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAE9C;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,MAAU,GAAG,OAAO,EAAE,CAErE;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,GAAE,MAAW,GAAG,OAAO,CAEtE;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE1C"}