@bsky.app/expo-guess-language 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/.eslintrc.js +5 -0
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/android/build.gradle +23 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/expo/modules/expoguesslanguage/ExpoGuessLanguageModule.kt +60 -0
- package/build/ExpoGuessLanguage.types.d.ts +8 -0
- package/build/ExpoGuessLanguage.types.d.ts.map +1 -0
- package/build/ExpoGuessLanguage.types.js +2 -0
- package/build/ExpoGuessLanguage.types.js.map +1 -0
- package/build/ExpoGuessLanguageModule.d.ts +8 -0
- package/build/ExpoGuessLanguageModule.d.ts.map +1 -0
- package/build/ExpoGuessLanguageModule.js +3 -0
- package/build/ExpoGuessLanguageModule.js.map +1 -0
- package/build/index.android.d.ts +4 -0
- package/build/index.android.d.ts.map +1 -0
- package/build/index.android.js +16 -0
- package/build/index.android.js.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/index.web.d.ts +4 -0
- package/build/index.web.d.ts.map +1 -0
- package/build/index.web.js +5 -0
- package/build/index.web.js.map +1 -0
- package/build/lande-detect.d.ts +3 -0
- package/build/lande-detect.d.ts.map +1 -0
- package/build/lande-detect.js +70 -0
- package/build/lande-detect.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/ExpoGuessLanguage.podspec +29 -0
- package/ios/ExpoGuessLanguageModule.swift +30 -0
- package/package.json +47 -0
- package/src/ExpoGuessLanguage.types.ts +8 -0
- package/src/ExpoGuessLanguageModule.ts +12 -0
- package/src/index.android.ts +23 -0
- package/src/index.ts +15 -0
- package/src/index.web.ts +12 -0
- package/src/lande-detect.ts +77 -0
- package/tsconfig.json +9 -0
package/.eslintrc.js
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bluesky Social PBC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @bsky.app/expo-guess-language
|
|
2
|
+
|
|
3
|
+
An Expo module for language detection using native platform APIs, with a JavaScript fallback.
|
|
4
|
+
|
|
5
|
+
## Platform support
|
|
6
|
+
|
|
7
|
+
| Platform | Backend | Notes |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| iOS | [NaturalLanguage](https://developer.apple.com/documentation/naturallanguage) | Always available (iOS 15.1+) |
|
|
10
|
+
| Android | [ML Kit Language ID](https://developers.google.com/ml-kit/language/identification) | Requires Google Play Services |
|
|
11
|
+
| Android (no Play Services) | [lande](https://github.com/nicklatkovich/lande) | Automatic JS fallback |
|
|
12
|
+
| Web | [lande](https://github.com/nicklatkovich/lande) | JS-only |
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npx expo install @bsky.app/expo-guess-language
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
For bare React Native projects, run `npx pod-install` after installing.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { guessLanguage } from "@bsky.app/expo-guess-language";
|
|
26
|
+
|
|
27
|
+
const results = await guessLanguage("Hello, world!");
|
|
28
|
+
// [{ language: "en", confidence: 0.98 }, ...]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Options
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const results = await guessLanguage("Bonjour le monde!", {
|
|
35
|
+
maxResults: 3, // default: 10
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Types
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
type LanguageResult = {
|
|
43
|
+
language: string; // BCP-47 code ("en", "fr", "ja", ...)
|
|
44
|
+
confidence: number; // 0 to 1
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type GuessLanguageOptions = {
|
|
48
|
+
maxResults?: number; // default 10
|
|
49
|
+
};
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
plugins {
|
|
2
|
+
id 'com.android.library'
|
|
3
|
+
id 'expo-module-gradle-plugin'
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
group = 'expo.modules.expoguesslanguage'
|
|
7
|
+
version = '0.1.0'
|
|
8
|
+
|
|
9
|
+
android {
|
|
10
|
+
namespace "expo.modules.expoguesslanguage"
|
|
11
|
+
defaultConfig {
|
|
12
|
+
versionCode 1
|
|
13
|
+
versionName "0.1.0"
|
|
14
|
+
}
|
|
15
|
+
lintOptions {
|
|
16
|
+
abortOnError false
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
dependencies {
|
|
21
|
+
implementation 'com.google.mlkit:language-id:17.0.6'
|
|
22
|
+
implementation 'com.google.android.gms:play-services-base:18.5.0'
|
|
23
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
package expo.modules.expoguesslanguage
|
|
2
|
+
|
|
3
|
+
import android.os.Bundle
|
|
4
|
+
import com.google.android.gms.common.ConnectionResult
|
|
5
|
+
import com.google.android.gms.common.GoogleApiAvailability
|
|
6
|
+
import com.google.android.gms.tasks.Tasks
|
|
7
|
+
import com.google.mlkit.nl.languageid.LanguageIdentification
|
|
8
|
+
import com.google.mlkit.nl.languageid.LanguageIdentificationOptions
|
|
9
|
+
import expo.modules.kotlin.modules.Module
|
|
10
|
+
import expo.modules.kotlin.modules.ModuleDefinition
|
|
11
|
+
|
|
12
|
+
class ExpoGuessLanguageModule : Module() {
|
|
13
|
+
private val isPlayServicesAvailable: Boolean by lazy {
|
|
14
|
+
val context = appContext.reactContext ?: return@lazy false
|
|
15
|
+
GoogleApiAvailability.getInstance()
|
|
16
|
+
.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private val languageIdentifier by lazy {
|
|
20
|
+
val options = LanguageIdentificationOptions.Builder()
|
|
21
|
+
.setConfidenceThreshold(0.01f)
|
|
22
|
+
.build()
|
|
23
|
+
LanguageIdentification.getClient(options)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
override fun definition() = ModuleDefinition {
|
|
27
|
+
Name("ExpoGuessLanguage")
|
|
28
|
+
|
|
29
|
+
Property("isNativeAvailable") {
|
|
30
|
+
return@Property isPlayServicesAvailable
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
AsyncFunction("guessLanguage") { text: String, maxResults: Int ->
|
|
34
|
+
if (text.isBlank()) return@AsyncFunction emptyList<Bundle>()
|
|
35
|
+
|
|
36
|
+
val task = languageIdentifier.identifyPossibleLanguages(text)
|
|
37
|
+
val results = Tasks.await(task)
|
|
38
|
+
|
|
39
|
+
results
|
|
40
|
+
.filter { it.languageTag != "und" }
|
|
41
|
+
.sortedByDescending { it.confidence }
|
|
42
|
+
.map { result ->
|
|
43
|
+
// Normalize to primary language subtag only (e.g. "ar-Latn" → "ar")
|
|
44
|
+
val lang = result.languageTag.substringBefore('-')
|
|
45
|
+
lang to result.confidence.toDouble()
|
|
46
|
+
}
|
|
47
|
+
// Merge duplicates after normalization (keep highest confidence)
|
|
48
|
+
.groupBy({ it.first }, { it.second })
|
|
49
|
+
.map { (lang, confidences) -> lang to confidences.max() }
|
|
50
|
+
.sortedByDescending { it.second }
|
|
51
|
+
.take(maxResults)
|
|
52
|
+
.map { (lang, confidence) ->
|
|
53
|
+
Bundle().apply {
|
|
54
|
+
putString("language", lang)
|
|
55
|
+
putDouble("confidence", confidence)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoGuessLanguage.types.d.ts","sourceRoot":"","sources":["../src/ExpoGuessLanguage.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoGuessLanguage.types.js","sourceRoot":"","sources":["../src/ExpoGuessLanguage.types.ts"],"names":[],"mappings":"","sourcesContent":["export type LanguageResult = {\n language: string; // BCP-47 code (\"en\", \"fr\", \"ja\")\n confidence: number; // 0 to 1\n};\n\nexport type GuessLanguageOptions = {\n maxResults?: number; // default 10\n};\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { LanguageResult } from "./ExpoGuessLanguage.types";
|
|
2
|
+
declare class ExpoGuessLanguageModuleType {
|
|
3
|
+
isNativeAvailable: boolean;
|
|
4
|
+
guessLanguage(text: string, maxResults: number): Promise<LanguageResult[]>;
|
|
5
|
+
}
|
|
6
|
+
declare const _default: ExpoGuessLanguageModuleType;
|
|
7
|
+
export default _default;
|
|
8
|
+
//# sourceMappingURL=ExpoGuessLanguageModule.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoGuessLanguageModule.d.ts","sourceRoot":"","sources":["../src/ExpoGuessLanguageModule.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEhE,OAAO,OAAO,2BAA2B;IACvC,iBAAiB,EAAE,OAAO,CAAC;IAC3B,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;CAC3E;;AAED,wBAEE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoGuessLanguageModule.js","sourceRoot":"","sources":["../src/ExpoGuessLanguageModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAS3C,eAAe,mBAAmB,CAChC,mBAAmB,CACpB,CAAC","sourcesContent":["import { requireNativeModule } from \"expo\";\n\nimport type { LanguageResult } from \"./ExpoGuessLanguage.types\";\n\ndeclare class ExpoGuessLanguageModuleType {\n isNativeAvailable: boolean;\n guessLanguage(text: string, maxResults: number): Promise<LanguageResult[]>;\n}\n\nexport default requireNativeModule<ExpoGuessLanguageModuleType>(\n \"ExpoGuessLanguage\"\n);\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
2
|
+
export type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
3
|
+
export declare function guessLanguage(text: string, options?: GuessLanguageOptions): Promise<LanguageResult[]>;
|
|
4
|
+
//# sourceMappingURL=index.android.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.android.d.ts","sourceRoot":"","sources":["../src/index.android.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEtF,YAAY,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAStF,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,cAAc,EAAE,CAAC,CAM3B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import ExpoGuessLanguageModule from "./ExpoGuessLanguageModule";
|
|
2
|
+
// Lazy require: lande is in the bundle but never parsed/executed if Play Services is available
|
|
3
|
+
let _landeDetect;
|
|
4
|
+
function getLandeDetect() {
|
|
5
|
+
if (!_landeDetect)
|
|
6
|
+
_landeDetect = require("./lande-detect");
|
|
7
|
+
return _landeDetect;
|
|
8
|
+
}
|
|
9
|
+
export function guessLanguage(text, options) {
|
|
10
|
+
const maxResults = options?.maxResults ?? 10;
|
|
11
|
+
if (ExpoGuessLanguageModule.isNativeAvailable) {
|
|
12
|
+
return ExpoGuessLanguageModule.guessLanguage(text, maxResults);
|
|
13
|
+
}
|
|
14
|
+
return Promise.resolve(getLandeDetect().detectWithLande(text, maxResults));
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=index.android.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.android.js","sourceRoot":"","sources":["../src/index.android.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAMhE,+FAA+F;AAC/F,IAAI,YAAyD,CAAC;AAC9D,SAAS,cAAc;IACrB,IAAI,CAAC,YAAY;QAAE,YAAY,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC5D,OAAO,YAAa,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,OAA8B;IAE9B,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,EAAE,CAAC;IAC7C,IAAI,uBAAuB,CAAC,iBAAiB,EAAE,CAAC;QAC9C,OAAO,uBAAuB,CAAC,aAAa,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IACjE,CAAC;IACD,OAAO,OAAO,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;AAC7E,CAAC","sourcesContent":["import ExpoGuessLanguageModule from \"./ExpoGuessLanguageModule\";\n\nimport type { GuessLanguageOptions, LanguageResult } from \"./ExpoGuessLanguage.types\";\n\nexport type { GuessLanguageOptions, LanguageResult } from \"./ExpoGuessLanguage.types\";\n\n// Lazy require: lande is in the bundle but never parsed/executed if Play Services is available\nlet _landeDetect: typeof import(\"./lande-detect\") | undefined;\nfunction getLandeDetect() {\n if (!_landeDetect) _landeDetect = require(\"./lande-detect\");\n return _landeDetect!;\n}\n\nexport function guessLanguage(\n text: string,\n options?: GuessLanguageOptions\n): Promise<LanguageResult[]> {\n const maxResults = options?.maxResults ?? 10;\n if (ExpoGuessLanguageModule.isNativeAvailable) {\n return ExpoGuessLanguageModule.guessLanguage(text, maxResults);\n }\n return Promise.resolve(getLandeDetect().detectWithLande(text, maxResults));\n}\n"]}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
2
|
+
export type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
3
|
+
export declare function guessLanguage(text: string, options?: GuessLanguageOptions): Promise<LanguageResult[]>;
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEtF,YAAY,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEtF,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,cAAc,EAAE,CAAC,CAK3B"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAMhE,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,OAA8B;IAE9B,OAAO,uBAAuB,CAAC,aAAa,CAC1C,IAAI,EACJ,OAAO,EAAE,UAAU,IAAI,EAAE,CAC1B,CAAC;AACJ,CAAC","sourcesContent":["import ExpoGuessLanguageModule from \"./ExpoGuessLanguageModule\";\n\nimport type { GuessLanguageOptions, LanguageResult } from \"./ExpoGuessLanguage.types\";\n\nexport type { GuessLanguageOptions, LanguageResult } from \"./ExpoGuessLanguage.types\";\n\nexport function guessLanguage(\n text: string,\n options?: GuessLanguageOptions\n): Promise<LanguageResult[]> {\n return ExpoGuessLanguageModule.guessLanguage(\n text,\n options?.maxResults ?? 10\n );\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
2
|
+
export type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
3
|
+
export declare function guessLanguage(text: string, options?: GuessLanguageOptions): Promise<LanguageResult[]>;
|
|
4
|
+
//# sourceMappingURL=index.web.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.web.d.ts","sourceRoot":"","sources":["../src/index.web.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEtF,YAAY,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEtF,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,cAAc,EAAE,CAAC,CAE3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.web.js","sourceRoot":"","sources":["../src/index.web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAMjD,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,OAA8B;IAE9B,OAAO,OAAO,CAAC,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,IAAI,EAAE,CAAC,CAAC,CAAC;AAC3E,CAAC","sourcesContent":["import { detectWithLande } from \"./lande-detect\";\n\nimport type { GuessLanguageOptions, LanguageResult } from \"./ExpoGuessLanguage.types\";\n\nexport type { GuessLanguageOptions, LanguageResult } from \"./ExpoGuessLanguage.types\";\n\nexport function guessLanguage(\n text: string,\n options?: GuessLanguageOptions\n): Promise<LanguageResult[]> {\n return Promise.resolve(detectWithLande(text, options?.maxResults ?? 10));\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lande-detect.d.ts","sourceRoot":"","sources":["../src/lande-detect.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAwDhE,wBAAgB,eAAe,CAC7B,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,cAAc,EAAE,CAelB"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import lande from "lande";
|
|
2
|
+
// ISO 639-3 → BCP-47 mapping for lande's 50 supported languages
|
|
3
|
+
const ISO639_3_TO_BCP47 = {
|
|
4
|
+
afr: "af",
|
|
5
|
+
ara: "ar",
|
|
6
|
+
aze: "az",
|
|
7
|
+
bel: "be",
|
|
8
|
+
ben: "bn",
|
|
9
|
+
bul: "bg",
|
|
10
|
+
cat: "ca",
|
|
11
|
+
ces: "cs",
|
|
12
|
+
ckb: "ckb",
|
|
13
|
+
cmn: "zh",
|
|
14
|
+
dan: "da",
|
|
15
|
+
deu: "de",
|
|
16
|
+
ell: "el",
|
|
17
|
+
eng: "en",
|
|
18
|
+
est: "et",
|
|
19
|
+
eus: "eu",
|
|
20
|
+
fin: "fi",
|
|
21
|
+
fra: "fr",
|
|
22
|
+
hau: "ha",
|
|
23
|
+
heb: "he",
|
|
24
|
+
hin: "hi",
|
|
25
|
+
hrv: "hr",
|
|
26
|
+
hun: "hu",
|
|
27
|
+
hye: "hy",
|
|
28
|
+
ind: "id",
|
|
29
|
+
isl: "is",
|
|
30
|
+
ita: "it",
|
|
31
|
+
jpn: "ja",
|
|
32
|
+
kat: "ka",
|
|
33
|
+
kaz: "kk",
|
|
34
|
+
kor: "ko",
|
|
35
|
+
lit: "lt",
|
|
36
|
+
mar: "mr",
|
|
37
|
+
mkd: "mk",
|
|
38
|
+
nld: "nl",
|
|
39
|
+
nob: "nb",
|
|
40
|
+
pes: "fa",
|
|
41
|
+
pol: "pl",
|
|
42
|
+
por: "pt",
|
|
43
|
+
ron: "ro",
|
|
44
|
+
run: "rn",
|
|
45
|
+
rus: "ru",
|
|
46
|
+
slk: "sk",
|
|
47
|
+
spa: "es",
|
|
48
|
+
srp: "sr",
|
|
49
|
+
swe: "sv",
|
|
50
|
+
tgl: "tl",
|
|
51
|
+
tur: "tr",
|
|
52
|
+
ukr: "uk",
|
|
53
|
+
vie: "vi",
|
|
54
|
+
};
|
|
55
|
+
export function detectWithLande(text, maxResults) {
|
|
56
|
+
if (!text.trim())
|
|
57
|
+
return [];
|
|
58
|
+
const results = lande(text);
|
|
59
|
+
const mapped = [];
|
|
60
|
+
for (const [iso3, confidence] of results) {
|
|
61
|
+
if (mapped.length >= maxResults)
|
|
62
|
+
break;
|
|
63
|
+
const bcp47 = ISO639_3_TO_BCP47[iso3];
|
|
64
|
+
if (bcp47) {
|
|
65
|
+
mapped.push({ language: bcp47, confidence });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return mapped;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=lande-detect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lande-detect.js","sourceRoot":"","sources":["../src/lande-detect.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B,gEAAgE;AAChE,MAAM,iBAAiB,GAA2B;IAChD,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,KAAK;IACV,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,MAAM,UAAU,eAAe,CAC7B,IAAY,EACZ,UAAkB;IAElB,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,CAAC;IAE5B,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5B,MAAM,MAAM,GAAqB,EAAE,CAAC;IAEpC,KAAK,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,OAAO,EAAE,CAAC;QACzC,IAAI,MAAM,CAAC,MAAM,IAAI,UAAU;YAAE,MAAM;QACvC,MAAM,KAAK,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,KAAK,EAAE,CAAC;YACV,MAAM,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import lande from \"lande\";\n\nimport type { LanguageResult } from \"./ExpoGuessLanguage.types\";\n\n// ISO 639-3 → BCP-47 mapping for lande's 50 supported languages\nconst ISO639_3_TO_BCP47: Record<string, string> = {\n afr: \"af\",\n ara: \"ar\",\n aze: \"az\",\n bel: \"be\",\n ben: \"bn\",\n bul: \"bg\",\n cat: \"ca\",\n ces: \"cs\",\n ckb: \"ckb\",\n cmn: \"zh\",\n dan: \"da\",\n deu: \"de\",\n ell: \"el\",\n eng: \"en\",\n est: \"et\",\n eus: \"eu\",\n fin: \"fi\",\n fra: \"fr\",\n hau: \"ha\",\n heb: \"he\",\n hin: \"hi\",\n hrv: \"hr\",\n hun: \"hu\",\n hye: \"hy\",\n ind: \"id\",\n isl: \"is\",\n ita: \"it\",\n jpn: \"ja\",\n kat: \"ka\",\n kaz: \"kk\",\n kor: \"ko\",\n lit: \"lt\",\n mar: \"mr\",\n mkd: \"mk\",\n nld: \"nl\",\n nob: \"nb\",\n pes: \"fa\",\n pol: \"pl\",\n por: \"pt\",\n ron: \"ro\",\n run: \"rn\",\n rus: \"ru\",\n slk: \"sk\",\n spa: \"es\",\n srp: \"sr\",\n swe: \"sv\",\n tgl: \"tl\",\n tur: \"tr\",\n ukr: \"uk\",\n vie: \"vi\",\n};\n\nexport function detectWithLande(\n text: string,\n maxResults: number\n): LanguageResult[] {\n if (!text.trim()) return [];\n\n const results = lande(text);\n const mapped: LanguageResult[] = [];\n\n for (const [iso3, confidence] of results) {\n if (mapped.length >= maxResults) break;\n const bcp47 = ISO639_3_TO_BCP47[iso3];\n if (bcp47) {\n mapped.push({ language: bcp47, confidence });\n }\n }\n\n return mapped;\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
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 = 'ExpoGuessLanguage'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platforms = {
|
|
14
|
+
:ios => '15.1',
|
|
15
|
+
:tvos => '15.1'
|
|
16
|
+
}
|
|
17
|
+
s.swift_version = '5.9'
|
|
18
|
+
s.source = { git: 'https://github.com/bluesky-social/expo-guess-language' }
|
|
19
|
+
s.static_framework = true
|
|
20
|
+
|
|
21
|
+
s.dependency 'ExpoModulesCore'
|
|
22
|
+
|
|
23
|
+
# Swift/Objective-C compatibility
|
|
24
|
+
s.pod_target_xcconfig = {
|
|
25
|
+
'DEFINES_MODULE' => 'YES',
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}"
|
|
29
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import NaturalLanguage
|
|
3
|
+
|
|
4
|
+
public class ExpoGuessLanguageModule: Module {
|
|
5
|
+
public func definition() -> ModuleDefinition {
|
|
6
|
+
Name("ExpoGuessLanguage")
|
|
7
|
+
|
|
8
|
+
Property("isNativeAvailable") {
|
|
9
|
+
return true
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
AsyncFunction("guessLanguage") { (text: String, maxResults: Int) -> [[String: Any]] in
|
|
13
|
+
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
14
|
+
guard !trimmed.isEmpty else { return [] }
|
|
15
|
+
|
|
16
|
+
let recognizer = NLLanguageRecognizer()
|
|
17
|
+
recognizer.processString(text)
|
|
18
|
+
let hypotheses = recognizer.languageHypotheses(withMaximum: maxResults)
|
|
19
|
+
recognizer.reset()
|
|
20
|
+
|
|
21
|
+
let results = hypotheses
|
|
22
|
+
.filter { $0.key != .undetermined }
|
|
23
|
+
.sorted { $0.value > $1.value }
|
|
24
|
+
.prefix(maxResults)
|
|
25
|
+
.map { ["language": $0.key.rawValue, "confidence": $0.value] as [String: Any] }
|
|
26
|
+
|
|
27
|
+
return Array(results)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bsky.app/expo-guess-language",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Guess the language of a text snippet using native APIs",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"types": "build/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "expo-module build",
|
|
9
|
+
"clean": "expo-module clean",
|
|
10
|
+
"lint": "expo-module lint",
|
|
11
|
+
"test": "expo-module test",
|
|
12
|
+
"prepare": "expo-module prepare",
|
|
13
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
14
|
+
"expo-module": "expo-module",
|
|
15
|
+
"open:ios": "xed example/ios",
|
|
16
|
+
"open:android": "open -a \"Android Studio\" example/android"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"expo",
|
|
21
|
+
"language-detection",
|
|
22
|
+
"nlp"
|
|
23
|
+
],
|
|
24
|
+
"repository": "https://github.com/bluesky-social/expo-guess-language",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/bluesky-social/expo-guess-language/issues"
|
|
27
|
+
},
|
|
28
|
+
"author": "Samuel Newman <samuel@blueskyweb.xyz> (https://github.com/mozzius)",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"homepage": "https://github.com/bluesky-social/expo-guess-language#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@expo/npm-proofread": "^1.0.1",
|
|
33
|
+
"@types/react": "~19.1.1",
|
|
34
|
+
"expo": "^54.0.33",
|
|
35
|
+
"expo-module-scripts": "^55.0.2",
|
|
36
|
+
"react-native": "0.81.5"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"lande": "^1.0.10"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"expo": "*",
|
|
43
|
+
"react": "*",
|
|
44
|
+
"react-native": "*"
|
|
45
|
+
},
|
|
46
|
+
"packageManager": "pnpm@10.26.2+sha512.0e308ff2005fc7410366f154f625f6631ab2b16b1d2e70238444dd6ae9d630a8482d92a451144debc492416896ed16f7b114a86ec68b8404b2443869e68ffda6"
|
|
47
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { requireNativeModule } from "expo";
|
|
2
|
+
|
|
3
|
+
import type { LanguageResult } from "./ExpoGuessLanguage.types";
|
|
4
|
+
|
|
5
|
+
declare class ExpoGuessLanguageModuleType {
|
|
6
|
+
isNativeAvailable: boolean;
|
|
7
|
+
guessLanguage(text: string, maxResults: number): Promise<LanguageResult[]>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default requireNativeModule<ExpoGuessLanguageModuleType>(
|
|
11
|
+
"ExpoGuessLanguage"
|
|
12
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ExpoGuessLanguageModule from "./ExpoGuessLanguageModule";
|
|
2
|
+
|
|
3
|
+
import type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
4
|
+
|
|
5
|
+
export type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
6
|
+
|
|
7
|
+
// Lazy require: lande is in the bundle but never parsed/executed if Play Services is available
|
|
8
|
+
let _landeDetect: typeof import("./lande-detect") | undefined;
|
|
9
|
+
function getLandeDetect() {
|
|
10
|
+
if (!_landeDetect) _landeDetect = require("./lande-detect");
|
|
11
|
+
return _landeDetect!;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function guessLanguage(
|
|
15
|
+
text: string,
|
|
16
|
+
options?: GuessLanguageOptions
|
|
17
|
+
): Promise<LanguageResult[]> {
|
|
18
|
+
const maxResults = options?.maxResults ?? 10;
|
|
19
|
+
if (ExpoGuessLanguageModule.isNativeAvailable) {
|
|
20
|
+
return ExpoGuessLanguageModule.guessLanguage(text, maxResults);
|
|
21
|
+
}
|
|
22
|
+
return Promise.resolve(getLandeDetect().detectWithLande(text, maxResults));
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ExpoGuessLanguageModule from "./ExpoGuessLanguageModule";
|
|
2
|
+
|
|
3
|
+
import type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
4
|
+
|
|
5
|
+
export type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
6
|
+
|
|
7
|
+
export function guessLanguage(
|
|
8
|
+
text: string,
|
|
9
|
+
options?: GuessLanguageOptions
|
|
10
|
+
): Promise<LanguageResult[]> {
|
|
11
|
+
return ExpoGuessLanguageModule.guessLanguage(
|
|
12
|
+
text,
|
|
13
|
+
options?.maxResults ?? 10
|
|
14
|
+
);
|
|
15
|
+
}
|
package/src/index.web.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { detectWithLande } from "./lande-detect";
|
|
2
|
+
|
|
3
|
+
import type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
4
|
+
|
|
5
|
+
export type { GuessLanguageOptions, LanguageResult } from "./ExpoGuessLanguage.types";
|
|
6
|
+
|
|
7
|
+
export function guessLanguage(
|
|
8
|
+
text: string,
|
|
9
|
+
options?: GuessLanguageOptions
|
|
10
|
+
): Promise<LanguageResult[]> {
|
|
11
|
+
return Promise.resolve(detectWithLande(text, options?.maxResults ?? 10));
|
|
12
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import lande from "lande";
|
|
2
|
+
|
|
3
|
+
import type { LanguageResult } from "./ExpoGuessLanguage.types";
|
|
4
|
+
|
|
5
|
+
// ISO 639-3 → BCP-47 mapping for lande's 50 supported languages
|
|
6
|
+
const ISO639_3_TO_BCP47: Record<string, string> = {
|
|
7
|
+
afr: "af",
|
|
8
|
+
ara: "ar",
|
|
9
|
+
aze: "az",
|
|
10
|
+
bel: "be",
|
|
11
|
+
ben: "bn",
|
|
12
|
+
bul: "bg",
|
|
13
|
+
cat: "ca",
|
|
14
|
+
ces: "cs",
|
|
15
|
+
ckb: "ckb",
|
|
16
|
+
cmn: "zh",
|
|
17
|
+
dan: "da",
|
|
18
|
+
deu: "de",
|
|
19
|
+
ell: "el",
|
|
20
|
+
eng: "en",
|
|
21
|
+
est: "et",
|
|
22
|
+
eus: "eu",
|
|
23
|
+
fin: "fi",
|
|
24
|
+
fra: "fr",
|
|
25
|
+
hau: "ha",
|
|
26
|
+
heb: "he",
|
|
27
|
+
hin: "hi",
|
|
28
|
+
hrv: "hr",
|
|
29
|
+
hun: "hu",
|
|
30
|
+
hye: "hy",
|
|
31
|
+
ind: "id",
|
|
32
|
+
isl: "is",
|
|
33
|
+
ita: "it",
|
|
34
|
+
jpn: "ja",
|
|
35
|
+
kat: "ka",
|
|
36
|
+
kaz: "kk",
|
|
37
|
+
kor: "ko",
|
|
38
|
+
lit: "lt",
|
|
39
|
+
mar: "mr",
|
|
40
|
+
mkd: "mk",
|
|
41
|
+
nld: "nl",
|
|
42
|
+
nob: "nb",
|
|
43
|
+
pes: "fa",
|
|
44
|
+
pol: "pl",
|
|
45
|
+
por: "pt",
|
|
46
|
+
ron: "ro",
|
|
47
|
+
run: "rn",
|
|
48
|
+
rus: "ru",
|
|
49
|
+
slk: "sk",
|
|
50
|
+
spa: "es",
|
|
51
|
+
srp: "sr",
|
|
52
|
+
swe: "sv",
|
|
53
|
+
tgl: "tl",
|
|
54
|
+
tur: "tr",
|
|
55
|
+
ukr: "uk",
|
|
56
|
+
vie: "vi",
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export function detectWithLande(
|
|
60
|
+
text: string,
|
|
61
|
+
maxResults: number
|
|
62
|
+
): LanguageResult[] {
|
|
63
|
+
if (!text.trim()) return [];
|
|
64
|
+
|
|
65
|
+
const results = lande(text);
|
|
66
|
+
const mapped: LanguageResult[] = [];
|
|
67
|
+
|
|
68
|
+
for (const [iso3, confidence] of results) {
|
|
69
|
+
if (mapped.length >= maxResults) break;
|
|
70
|
+
const bcp47 = ISO639_3_TO_BCP47[iso3];
|
|
71
|
+
if (bcp47) {
|
|
72
|
+
mapped.push({ language: bcp47, confidence });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return mapped;
|
|
77
|
+
}
|
package/tsconfig.json
ADDED