react-native-lingua 0.1.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/LICENSE +20 -0
- package/Lingua.podspec +21 -0
- package/README.md +254 -0
- package/android/CMakeLists.txt +55 -0
- package/android/build.gradle +94 -0
- package/android/cpp-adapter.cpp +36 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/lingua/JNIOnLoad.kt +20 -0
- package/android/src/main/java/com/lingua/LinguaModule.kt +51 -0
- package/android/src/main/java/com/lingua/LinguaPackage.kt +33 -0
- package/android/src/main/jni/include/liblingua.h +65 -0
- package/cpp/lingua.cpp +184 -0
- package/cpp/lingua.h +12 -0
- package/cpp/macros.h +15 -0
- package/ios/Lingua.h +5 -0
- package/ios/Lingua.mm +40 -0
- package/lib/module/NativeLingua.js +5 -0
- package/lib/module/NativeLingua.js.map +1 -0
- package/lib/module/index.js +82 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeLingua.d.ts +7 -0
- package/lib/typescript/src/NativeLingua.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +50 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +164 -0
- package/src/NativeLingua.ts +7 -0
- package/src/index.tsx +121 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 hej
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/Lingua.podspec
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "Lingua"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/Hkmu/react-native-lingua/.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm}", "cpp/**/*.{hpp,cpp,c,h}"
|
|
17
|
+
s.private_header_files = "ios/**/*.h"
|
|
18
|
+
s.vendored_frameworks = "ios/*.xcframework"
|
|
19
|
+
|
|
20
|
+
install_modules_dependencies(s)
|
|
21
|
+
end
|
package/README.md
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# react-native-lingua
|
|
2
|
+
|
|
3
|
+
An accurate natural language detection library for React Native, powered by [lingua-rs](https://github.com/pemistahl/lingua-rs).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Highly Accurate**: Uses advanced n-gram models (1-5) for superior detection accuracy
|
|
8
|
+
- 📦 **75 Languages**: Supports detection of 75 languages
|
|
9
|
+
- ⚡ **Fast**: Built with Rust and JSI for optimal performance
|
|
10
|
+
- 🔒 **Type-Safe**: Full TypeScript support
|
|
11
|
+
- 📱 **Cross-Platform**: Works on iOS and Android
|
|
12
|
+
- 🌐 **Offline**: No internet connection required
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
### Prerequisites
|
|
17
|
+
|
|
18
|
+
The native libraries are built automatically during installation. You'll need:
|
|
19
|
+
|
|
20
|
+
1. **Rust toolchain**:
|
|
21
|
+
```sh
|
|
22
|
+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
2. **cargo-ndk** (for Android):
|
|
26
|
+
```sh
|
|
27
|
+
cargo install cargo-ndk
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
3. **Android NDK** (for Android):
|
|
31
|
+
- Recommended version: 26.1.10909125
|
|
32
|
+
- Set environment variables:
|
|
33
|
+
```sh
|
|
34
|
+
export ANDROID_SDK_ROOT=$HOME/Library/Android/sdk
|
|
35
|
+
export ANDROID_HOME=$HOME/Library/Android/sdk
|
|
36
|
+
export ANDROID_NDK_HOME=$ANDROID_HOME/ndk/26.1.10909125
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
4. **Xcode** (for iOS on macOS)
|
|
40
|
+
|
|
41
|
+
### Install the Package
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
pnpm add react-native-lingua
|
|
45
|
+
# or
|
|
46
|
+
npm install react-native-lingua
|
|
47
|
+
# or
|
|
48
|
+
yarn add react-native-lingua
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The native libraries will be built automatically during `postinstall`.
|
|
52
|
+
|
|
53
|
+
### iOS Setup
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
cd ios && pod install
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Android Setup
|
|
60
|
+
|
|
61
|
+
No additional steps required.
|
|
62
|
+
|
|
63
|
+
## Manual Build (Optional)
|
|
64
|
+
|
|
65
|
+
If you need to rebuild the native libraries manually:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
cd node_modules/react-native-lingua
|
|
69
|
+
npm run build:rust
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Usage
|
|
73
|
+
|
|
74
|
+
### Basic Language Detection
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import {
|
|
78
|
+
createDetectorForAllLanguages,
|
|
79
|
+
createDetectorForLanguages,
|
|
80
|
+
detectLanguage,
|
|
81
|
+
} from 'react-native-lingua';
|
|
82
|
+
|
|
83
|
+
// Create a detector for all supported languages
|
|
84
|
+
const detector = createDetectorForAllLanguages();
|
|
85
|
+
|
|
86
|
+
// Detect language
|
|
87
|
+
const language = detectLanguage(detector, 'Hello, how are you?');
|
|
88
|
+
console.log(language); // 'en'
|
|
89
|
+
|
|
90
|
+
// Or create a detector for specific languages (faster and more accurate)
|
|
91
|
+
const specificDetector = createDetectorForLanguages(['en', 'fr', 'de', 'es']);
|
|
92
|
+
const lang = detectLanguage(specificDetector, 'Bonjour le monde');
|
|
93
|
+
console.log(lang); // 'fr'
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Language Confidence
|
|
97
|
+
|
|
98
|
+
Get confidence scores to understand how certain the detection is:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import {
|
|
102
|
+
createDetectorForLanguages,
|
|
103
|
+
computeLanguageConfidence,
|
|
104
|
+
computeLanguageConfidenceValues,
|
|
105
|
+
} from 'react-native-lingua';
|
|
106
|
+
|
|
107
|
+
const detector = createDetectorForLanguages(['en', 'fr', 'de', 'es']);
|
|
108
|
+
|
|
109
|
+
// Get confidence for a specific language
|
|
110
|
+
const confidence = computeLanguageConfidence(detector, 'Hello world', 'en');
|
|
111
|
+
console.log(confidence); // 0.95
|
|
112
|
+
|
|
113
|
+
// Get confidence values for all languages
|
|
114
|
+
const confidences = computeLanguageConfidenceValues(detector, 'Hello world');
|
|
115
|
+
|
|
116
|
+
console.log(confidences);
|
|
117
|
+
// [
|
|
118
|
+
// { language: 'en', confidence: 0.95 },
|
|
119
|
+
// { language: 'fr', confidence: 0.03 },
|
|
120
|
+
// { language: 'de', confidence: 0.01 },
|
|
121
|
+
// { language: 'es', confidence: 0.01 }
|
|
122
|
+
// ]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Supported Languages
|
|
126
|
+
|
|
127
|
+
The library supports 75 languages using ISO 639-1 codes. Some examples:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const commonLanguages = [
|
|
131
|
+
'en', // English
|
|
132
|
+
'es', // Spanish
|
|
133
|
+
'fr', // French
|
|
134
|
+
'de', // German
|
|
135
|
+
'it', // Italian
|
|
136
|
+
'pt', // Portuguese
|
|
137
|
+
'ru', // Russian
|
|
138
|
+
'ja', // Japanese
|
|
139
|
+
'zh', // Chinese
|
|
140
|
+
'ko', // Korean
|
|
141
|
+
'ar', // Arabic
|
|
142
|
+
'hi', // Hindi
|
|
143
|
+
// ... and 63 more
|
|
144
|
+
];
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For a complete list of supported languages, see the [lingua-rs documentation](https://docs.rs/lingua/latest/lingua/enum.Language.html).
|
|
148
|
+
|
|
149
|
+
## API Reference
|
|
150
|
+
|
|
151
|
+
### `createDetectorForAllLanguages()`
|
|
152
|
+
|
|
153
|
+
Creates a language detector that can detect any of the 75 supported languages.
|
|
154
|
+
|
|
155
|
+
**Returns:** `LanguageDetector`
|
|
156
|
+
|
|
157
|
+
**Note:** Loading all languages consumes significant memory (~1 GB). Consider using `createDetectorForLanguages()` if you know which languages to expect.
|
|
158
|
+
|
|
159
|
+
### `createDetectorForLanguages(languages: string[])`
|
|
160
|
+
|
|
161
|
+
Creates a language detector for specific languages.
|
|
162
|
+
|
|
163
|
+
**Parameters:**
|
|
164
|
+
|
|
165
|
+
- `languages: string[]` - Array of ISO 639-1 language codes
|
|
166
|
+
|
|
167
|
+
**Returns:** `LanguageDetector`
|
|
168
|
+
|
|
169
|
+
**Example:**
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
const detector = createDetectorForLanguages(['en', 'fr', 'de']);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### `detectLanguage(detector: LanguageDetector, text: string)`
|
|
176
|
+
|
|
177
|
+
Detects the language of the given text.
|
|
178
|
+
|
|
179
|
+
**Parameters:**
|
|
180
|
+
|
|
181
|
+
- `detector: LanguageDetector` - Language detector instance
|
|
182
|
+
- `text: string` - Text to analyze
|
|
183
|
+
|
|
184
|
+
**Returns:** `string | null` - ISO 639-1 language code or null if detection failed
|
|
185
|
+
|
|
186
|
+
**Example:**
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const lang = detectLanguage(detector, 'Hello world'); // 'en'
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `computeLanguageConfidence(detector: LanguageDetector, text: string, languageCode: string)`
|
|
193
|
+
|
|
194
|
+
Computes the confidence value for a specific language.
|
|
195
|
+
|
|
196
|
+
**Parameters:**
|
|
197
|
+
|
|
198
|
+
- `detector: LanguageDetector` - Language detector instance
|
|
199
|
+
- `text: string` - Text to analyze
|
|
200
|
+
- `languageCode: string` - ISO 639-1 language code
|
|
201
|
+
|
|
202
|
+
**Returns:** `number` - Confidence value between 0.0 and 1.0
|
|
203
|
+
|
|
204
|
+
**Example:**
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const confidence = computeLanguageConfidence(detector, 'Hello world', 'en'); // 0.95
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### `computeLanguageConfidenceValues(detector: LanguageDetector, text: string)`
|
|
211
|
+
|
|
212
|
+
Computes confidence values for all languages in the detector.
|
|
213
|
+
|
|
214
|
+
**Parameters:**
|
|
215
|
+
|
|
216
|
+
- `detector: LanguageDetector` - Language detector instance
|
|
217
|
+
- `text: string` - Text to analyze
|
|
218
|
+
|
|
219
|
+
**Returns:** `LanguageConfidence[]` - Array of `{ language: string, confidence: number }` sorted by confidence (descending)
|
|
220
|
+
|
|
221
|
+
**Example:**
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const confidences = computeLanguageConfidenceValues(detector, 'Hello');
|
|
225
|
+
// [{ language: 'en', confidence: 0.95 }, ...]
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Performance Tips
|
|
229
|
+
|
|
230
|
+
1. **Use specific languages**: Create detectors with only the languages you need for better performance and accuracy
|
|
231
|
+
2. **Reuse detectors**: Create detector instances once and reuse them
|
|
232
|
+
3. **Memory usage**: The detector with all languages uses ~1 GB of memory. Use specific language sets to reduce memory footprint
|
|
233
|
+
|
|
234
|
+
## How It Works
|
|
235
|
+
|
|
236
|
+
This library wraps the Rust [lingua-rs](https://github.com/pemistahl/lingua-rs) library using:
|
|
237
|
+
|
|
238
|
+
1. **Rust FFI**: C-compatible FFI bindings for the lingua library
|
|
239
|
+
2. **C++ JSI**: JavaScript Interface for direct communication with JavaScript
|
|
240
|
+
3. **React Native TurboModule**: Modern architecture for optimal performance
|
|
241
|
+
|
|
242
|
+
The detection uses both rule-based and statistical Naive Bayes methods with n-grams of sizes 1-5, providing high accuracy even for short text snippets.
|
|
243
|
+
|
|
244
|
+
## Contributing
|
|
245
|
+
|
|
246
|
+
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
2
|
+
project(lingua)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
5
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
6
|
+
|
|
7
|
+
# Find all C++ files
|
|
8
|
+
file(GLOB_RECURSE cpp_files RELATIVE ${CMAKE_SOURCE_DIR}
|
|
9
|
+
"../cpp/**.cpp"
|
|
10
|
+
"../cpp/**.h"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
add_library(${CMAKE_PROJECT_NAME} SHARED
|
|
14
|
+
${cpp_files}
|
|
15
|
+
cpp-adapter.cpp
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# Specifies a path to native header files
|
|
19
|
+
include_directories(
|
|
20
|
+
../cpp
|
|
21
|
+
../rust/generated/include
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# Link the lingua_native static library
|
|
25
|
+
cmake_path(SET LINGUA_LIB_PATH ${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/liblingua_native.a NORMALIZE)
|
|
26
|
+
add_library(lingua_native STATIC IMPORTED)
|
|
27
|
+
set_target_properties(lingua_native PROPERTIES IMPORTED_LOCATION ${LINGUA_LIB_PATH})
|
|
28
|
+
|
|
29
|
+
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jni/include)
|
|
30
|
+
|
|
31
|
+
find_library(LOG_LIB log)
|
|
32
|
+
find_package(ReactAndroid REQUIRED CONFIG)
|
|
33
|
+
find_package(fbjni REQUIRED CONFIG)
|
|
34
|
+
|
|
35
|
+
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
|
|
36
|
+
target_link_libraries(
|
|
37
|
+
${CMAKE_PROJECT_NAME}
|
|
38
|
+
lingua_native
|
|
39
|
+
${LOG_LIB}
|
|
40
|
+
ReactAndroid::reactnative
|
|
41
|
+
ReactAndroid::jsi
|
|
42
|
+
fbjni::fbjni
|
|
43
|
+
)
|
|
44
|
+
else()
|
|
45
|
+
target_link_libraries(
|
|
46
|
+
${CMAKE_PROJECT_NAME}
|
|
47
|
+
${LOG_LIB}
|
|
48
|
+
lingua_native
|
|
49
|
+
fbjni::fbjni
|
|
50
|
+
ReactAndroid::jsi
|
|
51
|
+
ReactAndroid::turbomodulejsijni
|
|
52
|
+
ReactAndroid::react_nativemodule_core
|
|
53
|
+
android
|
|
54
|
+
)
|
|
55
|
+
endif()
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['Lingua_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
apply plugin: "com.android.library"
|
|
20
|
+
apply plugin: "kotlin-android"
|
|
21
|
+
|
|
22
|
+
apply plugin: "com.facebook.react"
|
|
23
|
+
|
|
24
|
+
def getExtOrIntegerDefault(name) {
|
|
25
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Lingua_" + name]).toInteger()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
android {
|
|
29
|
+
namespace "com.lingua"
|
|
30
|
+
|
|
31
|
+
ndkVersion = "26.1.10909125"
|
|
32
|
+
|
|
33
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
34
|
+
|
|
35
|
+
defaultConfig {
|
|
36
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
37
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
38
|
+
|
|
39
|
+
externalNativeBuild {
|
|
40
|
+
cmake {
|
|
41
|
+
cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
|
|
42
|
+
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
|
|
43
|
+
arguments '-DANDROID_STL=c++_shared'
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
externalNativeBuild {
|
|
49
|
+
cmake {
|
|
50
|
+
path "CMakeLists.txt"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
buildFeatures {
|
|
55
|
+
buildConfig true
|
|
56
|
+
prefab true
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
buildTypes {
|
|
60
|
+
release {
|
|
61
|
+
minifyEnabled false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
lintOptions {
|
|
66
|
+
disable "GradleCompatible"
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
compileOptions {
|
|
70
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
71
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
sourceSets {
|
|
75
|
+
main {
|
|
76
|
+
java.srcDirs += [
|
|
77
|
+
"generated/java",
|
|
78
|
+
"generated/jni"
|
|
79
|
+
]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
repositories {
|
|
85
|
+
mavenCentral()
|
|
86
|
+
google()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
90
|
+
|
|
91
|
+
dependencies {
|
|
92
|
+
implementation "com.facebook.react:react-android"
|
|
93
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
94
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#include "lingua.h"
|
|
2
|
+
#include <ReactCommon/CallInvokerHolder.h>
|
|
3
|
+
#include <fbjni/fbjni.h>
|
|
4
|
+
#include <jni.h>
|
|
5
|
+
#include <jsi/jsi.h>
|
|
6
|
+
#include <typeinfo>
|
|
7
|
+
|
|
8
|
+
namespace jsi = facebook::jsi;
|
|
9
|
+
namespace react = facebook::react;
|
|
10
|
+
namespace jni = facebook::jni;
|
|
11
|
+
|
|
12
|
+
// This file uses fbjni for cleaner JNI bindings
|
|
13
|
+
struct LinguaModule : jni::JavaClass<LinguaModule> {
|
|
14
|
+
static constexpr auto kJavaDescriptor = "Lcom/lingua/LinguaModule;";
|
|
15
|
+
|
|
16
|
+
static void registerNatives() {
|
|
17
|
+
javaClassStatic()->registerNatives({
|
|
18
|
+
makeNativeMethod("installNativeJsi", LinguaModule::installNativeJsi),
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private:
|
|
23
|
+
static void
|
|
24
|
+
installNativeJsi(jni::alias_ref<jni::JObject> thiz, jlong jsiRuntimePtr,
|
|
25
|
+
jni::alias_ref<react::CallInvokerHolder::javaobject>
|
|
26
|
+
jsCallInvokerHolder) {
|
|
27
|
+
auto jsiRuntime = reinterpret_cast<jsi::Runtime *>(jsiRuntimePtr);
|
|
28
|
+
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
|
|
29
|
+
|
|
30
|
+
lingua::install(*jsiRuntime, jsCallInvoker);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
|
|
35
|
+
return jni::initialize(vm, [] { LinguaModule::registerNatives(); });
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package com.lingua
|
|
2
|
+
|
|
3
|
+
import android.util.Log
|
|
4
|
+
|
|
5
|
+
object JNIOnLoad {
|
|
6
|
+
private const val TAG = "Lingua"
|
|
7
|
+
private var isInitialized = false
|
|
8
|
+
|
|
9
|
+
@Synchronized
|
|
10
|
+
fun initializeNative() {
|
|
11
|
+
if (isInitialized) return
|
|
12
|
+
try {
|
|
13
|
+
System.loadLibrary("lingua")
|
|
14
|
+
isInitialized = true
|
|
15
|
+
} catch (e: Throwable) {
|
|
16
|
+
Log.e(TAG, "Failed to load lingua native library.", e)
|
|
17
|
+
throw e
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package com.lingua
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.Keep
|
|
4
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.bridge.ReactMethod
|
|
7
|
+
import com.facebook.react.common.annotations.FrameworkAPI
|
|
8
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
9
|
+
import com.facebook.react.turbomodule.core.CallInvokerHolderImpl
|
|
10
|
+
|
|
11
|
+
@DoNotStrip
|
|
12
|
+
@Keep
|
|
13
|
+
@OptIn(FrameworkAPI::class)
|
|
14
|
+
@Suppress("KotlinJniMissingFunction")
|
|
15
|
+
@ReactModule(name = LinguaModule.NAME)
|
|
16
|
+
class LinguaModule(reactContext: ReactApplicationContext) :
|
|
17
|
+
NativeLinguaSpec(reactContext) {
|
|
18
|
+
|
|
19
|
+
override fun getName(): String {
|
|
20
|
+
return NAME
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
24
|
+
fun install(): String? {
|
|
25
|
+
try {
|
|
26
|
+
// Get jsi::Runtime pointer
|
|
27
|
+
val jsContext = reactApplicationContext.javaScriptContextHolder
|
|
28
|
+
?: return "ReactApplicationContext.javaScriptContextHolder is null!"
|
|
29
|
+
|
|
30
|
+
// Get CallInvokerHolder
|
|
31
|
+
val callInvokerHolder = reactApplicationContext.jsCallInvokerHolder as? CallInvokerHolderImpl
|
|
32
|
+
?: return "ReactApplicationContext.jsCallInvokerHolder is null!"
|
|
33
|
+
|
|
34
|
+
installNativeJsi(jsContext.get(), callInvokerHolder)
|
|
35
|
+
|
|
36
|
+
return null
|
|
37
|
+
} catch (e: Throwable) {
|
|
38
|
+
return e.message
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private external fun installNativeJsi(jsRuntimePointer: Long, callInvokerHolder: CallInvokerHolderImpl)
|
|
43
|
+
|
|
44
|
+
companion object {
|
|
45
|
+
const val NAME = "Lingua"
|
|
46
|
+
|
|
47
|
+
init {
|
|
48
|
+
JNIOnLoad.initializeNative()
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package com.lingua
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class LinguaPackage : BaseReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == LinguaModule.NAME) {
|
|
13
|
+
LinguaModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
20
|
+
return ReactModuleInfoProvider {
|
|
21
|
+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
|
|
22
|
+
moduleInfos[LinguaModule.NAME] = ReactModuleInfo(
|
|
23
|
+
LinguaModule.NAME,
|
|
24
|
+
LinguaModule.NAME,
|
|
25
|
+
false, // canOverrideExistingModule
|
|
26
|
+
false, // needsEagerInit
|
|
27
|
+
false, // isCxxModule
|
|
28
|
+
true // isTurboModule
|
|
29
|
+
)
|
|
30
|
+
moduleInfos
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#ifndef lingua_h
|
|
2
|
+
#define lingua_h
|
|
3
|
+
|
|
4
|
+
/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */
|
|
5
|
+
|
|
6
|
+
#include <cstdarg>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <cstdlib>
|
|
9
|
+
#include <ostream>
|
|
10
|
+
#include <new>
|
|
11
|
+
|
|
12
|
+
namespace lingua {
|
|
13
|
+
|
|
14
|
+
struct LinguaDetector;
|
|
15
|
+
|
|
16
|
+
/// Result structure for confidence values
|
|
17
|
+
struct ConfidenceValue {
|
|
18
|
+
char *language_code;
|
|
19
|
+
double confidence;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
extern "C" {
|
|
23
|
+
|
|
24
|
+
void set_error(const char **err, const str *error_message);
|
|
25
|
+
|
|
26
|
+
const char *get_error();
|
|
27
|
+
|
|
28
|
+
/// Creates a language detector with all languages
|
|
29
|
+
LinguaDetector *lingua_detector_create_all();
|
|
30
|
+
|
|
31
|
+
/// Creates a language detector with specific languages
|
|
32
|
+
/// languages: comma-separated ISO 639-1 codes (e.g., "en,fr,de,es")
|
|
33
|
+
LinguaDetector *lingua_detector_create_from_languages(const char *languages, const char **error);
|
|
34
|
+
|
|
35
|
+
/// Detects the language of the given text
|
|
36
|
+
/// Returns the ISO 639-1 code (e.g., "en") or null if detection failed
|
|
37
|
+
char *lingua_detect_language(const LinguaDetector *detector, const char *text, const char **error);
|
|
38
|
+
|
|
39
|
+
/// Computes the confidence value for a specific language
|
|
40
|
+
double lingua_compute_language_confidence(const LinguaDetector *detector,
|
|
41
|
+
const char *text,
|
|
42
|
+
const char *language_code,
|
|
43
|
+
const char **error);
|
|
44
|
+
|
|
45
|
+
/// Computes confidence values for all languages
|
|
46
|
+
/// Returns an array of ConfidenceValue structs and sets the count
|
|
47
|
+
ConfidenceValue *lingua_compute_language_confidence_values(const LinguaDetector *detector,
|
|
48
|
+
const char *text,
|
|
49
|
+
int *count,
|
|
50
|
+
const char **error);
|
|
51
|
+
|
|
52
|
+
/// Frees a string allocated by lingua
|
|
53
|
+
void lingua_free_string(char *s);
|
|
54
|
+
|
|
55
|
+
/// Frees the confidence values array
|
|
56
|
+
void lingua_free_confidence_values(ConfidenceValue *values, int count);
|
|
57
|
+
|
|
58
|
+
/// Destroys the detector and frees memory
|
|
59
|
+
void lingua_detector_destroy(LinguaDetector *detector);
|
|
60
|
+
|
|
61
|
+
} // extern "C"
|
|
62
|
+
|
|
63
|
+
} // namespace lingua
|
|
64
|
+
|
|
65
|
+
#endif // lingua_h
|