@isdk/detect-text-language
Advanced tools
Comparing version
@@ -507,2 +507,7 @@ declare const CountryCodes: { | ||
interface IDetectLanguageOptions { | ||
isoCode?: boolean; | ||
langSubset?: string[]; | ||
threshold?: number; | ||
} | ||
/** | ||
@@ -526,5 +531,3 @@ * detects the language of the given text. | ||
*/ | ||
declare function detectTextLanguage(text: string, options?: { | ||
isoCode?: boolean; | ||
}): any; | ||
declare function detectTextLanguage(text: string, options?: IDetectLanguageOptions): string | undefined; | ||
/** | ||
@@ -537,7 +540,10 @@ * Detects the language of the provided text and returns the language code along with additional information. | ||
*/ | ||
declare function detectTextLangEx(text: string): { | ||
declare function detectTextLangEx(text: string, options?: IDetectLanguageOptions): { | ||
iso6391: string; | ||
iso3166?: string | undefined; | ||
name?: string | undefined; | ||
country?: string | undefined; | ||
iso3166?: string; | ||
name?: string; | ||
country?: string; | ||
scores?: { | ||
[langCode: string]: number; | ||
}; | ||
} | undefined; | ||
@@ -547,2 +553,2 @@ declare function getCountryCodeFromLang(iso6391: string): string | undefined; | ||
export { CountryCodes, CountryNames, detectTextLangEx, detectTextLanguage, getCountryCodeFromLang, getLanguageFromIso6391 }; | ||
export { CountryCodes, CountryNames, type IDetectLanguageOptions, detectTextLangEx, detectTextLanguage, getCountryCodeFromLang, getLanguageFromIso6391 }; |
@@ -543,28 +543,48 @@ "use strict"; | ||
var _CountryCodes = Object.fromEntries(Object.entries(CountryCodes).map(([key, value]) => [key, value.split(",")[0]])); | ||
var isSubset = false; | ||
function dynamicLangSubset(langSubset) { | ||
if (langSubset?.length) { | ||
isSubset = true; | ||
import_eld.eld.dynamicLangSubset(langSubset); | ||
} else if (isSubset) { | ||
isSubset = false; | ||
import_eld.eld.dynamicLangSubset(false); | ||
} | ||
} | ||
function detectTextLanguage(text, options = {}) { | ||
dynamicLangSubset(options.langSubset); | ||
const result = import_eld.eld.detect(text); | ||
if (result.isReliable()) { | ||
const threshold = options.threshold ?? 0.1; | ||
const scores = result.getScores(); | ||
const lang = result.language; | ||
if (!options.isoCode) { | ||
return getLanguageFromIso6391(lang); | ||
if (scores[lang] >= threshold) { | ||
if (!options.isoCode) { | ||
return getLanguageFromIso6391(lang); | ||
} | ||
return lang; | ||
} | ||
return lang; | ||
} | ||
} | ||
function detectTextLangEx(text) { | ||
function detectTextLangEx(text, options) { | ||
dynamicLangSubset(options?.langSubset); | ||
let result; | ||
const langInfo = import_eld.eld.detect(text); | ||
if (langInfo.isReliable()) { | ||
const threshold = options?.threshold ?? 0.1; | ||
const scores = langInfo.getScores(); | ||
const iso6391 = langInfo.language; | ||
result = { iso6391 }; | ||
const countryCode = getCountryCodeFromLang(iso6391); | ||
if (countryCode) { | ||
result.iso3166 = countryCode; | ||
const countryName = CountryNames[countryCode]; | ||
result.country = countryName; | ||
if (scores[iso6391] >= threshold) { | ||
result = { iso6391, scores: langInfo.getScores() }; | ||
const countryCode = getCountryCodeFromLang(iso6391); | ||
if (countryCode) { | ||
result.iso3166 = countryCode; | ||
const countryName = CountryNames[countryCode]; | ||
result.country = countryName; | ||
} | ||
const info = import_iso_639_3.iso6393.find((i) => i.iso6391 === iso6391); | ||
if (info?.name) { | ||
result.name = info.name; | ||
} | ||
} | ||
const info = import_iso_639_3.iso6393.find((i) => i.iso6391 === iso6391); | ||
if (info?.name) { | ||
result.name = info.name; | ||
} | ||
} | ||
@@ -571,0 +591,0 @@ return result; |
@@ -1,5 +0,7 @@ | ||
@isdk/detect-text-language / [Exports](modules.md) | ||
**@isdk/detect-text-language** • [**Docs**](globals.md) | ||
*** | ||
## ai-tool-llm | ||
LLM(Large Language Model) |
{ | ||
"name": "@isdk/detect-text-language", | ||
"description": "Detect the text language", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"author": "Riceball LEE <snowyu.lee@gmail.com>", | ||
@@ -25,15 +25,15 @@ "homepage": "https://github.com/isdk/detect-text-language.js", | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^2.20.0", | ||
"@types/jest": "^29.5.12", | ||
"@typescript-eslint/eslint-plugin": "^7.12.0", | ||
"eslint": "^8.57.0", | ||
"@antfu/eslint-config": "^3.6.2", | ||
"@types/jest": "^29.5.13", | ||
"@typescript-eslint/eslint-plugin": "^8.6.0", | ||
"eslint": "^9.10.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-tsdoc": "^0.2.17", | ||
"prettier": "^3.3.1", | ||
"tsup": "^8.1.0", | ||
"typedoc": "^0.25.13", | ||
"typedoc-plugin-markdown": "^3.17.1", | ||
"typescript": "^5.4.5", | ||
"vite": "^5.2.12", | ||
"vitest": "^1.6.0" | ||
"eslint-plugin-tsdoc": "^0.3.0", | ||
"prettier": "^3.3.3", | ||
"tsup": "^8.2.4", | ||
"typedoc": "^0.26.7", | ||
"typedoc-plugin-markdown": "^4.2.7", | ||
"typescript": "^5.6.2", | ||
"vite": "^5.4.6", | ||
"vitest": "^2.1.1" | ||
}, | ||
@@ -40,0 +40,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
80621
25.18%15
66.67%1734
2.73%