-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdemo.html
More file actions
81 lines (69 loc) · 3.92 KB
/
demo.html
File metadata and controls
81 lines (69 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Demo</title>
</head>
<body>
<main>
<h1>Demo</h1>
<div id="demo">See results at the console.</div>
</main>
<!-- <script src="minified/eld.xs.min.js" charset="utf-8"></script> -->
<script type="module">
import { eld } from './src/entries/dynamic.js' // import { eld } from "eld";
// import { eld } from './src/entries/static.large.js' // import { eld } from "eld/large";
// Initialize database, select file: 'extrasmall', 'small', 'medium', 'large' or subsets
await eld.load('large') // Not necessary/available at static sizes { eld/large } imports and bundles
// detect() expects a UTF-8 string, returns an object, with a 'language' variable : ISO 639-1 code or empty string
console.log('Hola, cómo te llamas?', eld.detect('Hola, cómo te llamas?'))
// { language: 'es', getScores(): {'es': 0.5, 'et': 0.2}, isReliable(): true }
// { language: string, getScores(): Object, isReliable(): boolean }
// isReliable(threshold = 0.75) can take a threshold ratio 0-1. A higher threshold will make check more strict.
console.log('Hola, cómo te llamas?', eld.detect('Hola, cómo te llamas?').language)
// 'es'
console.log('getScores', eld.detect('Hola, cómo te llamas?').getScores())
console.log('isReliable', eld.detect('Hola, cómo te llamas?').isReliable())
/*
* To reduce the languages to be detected, there are 2 options, they only need to be executed once.
* This is the complete list on languages for ELD v1, using ISO 639-1 codes:
* ['am', 'ar', 'az', 'be', 'bg', 'bn', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fr',
* 'gu', 'he', 'hi', 'hr', 'hu', 'hy', 'is', 'it', 'ja', 'ka', 'kn', 'ko', 'ku', 'lo', 'lt', 'lv', 'ml', 'mr',
* 'ms', 'nl', 'no', 'or', 'pa', 'pl', 'pt', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sv', 'ta', 'te', 'th', 'tl',
* 'tr', 'uk', 'ur', 'vi', 'yo', 'zh']
*/
let languagesSubset = ['en', 'es', 'fr', 'it', 'nl', 'de']
// Option 1. With dynamicLangSubset, detector executes normally, but at the end will filter the excluded languages
console.log(eld.setLanguageSubset(languagesSubset)) // Returns the validated languages of the subset
// { 9: "de", 11: "en", 12: "es", 17: "fr", 25: "it", 37: "nl" }
// to remove the subset
eld.setLanguageSubset(false)
/*
Option 2. The optimal way to regularly use the same subset, will be using saveSubset()
eld.saveSubset() will download a new database of Ngrams with only the subset languages. (Only at a web browser)
It is not included in the minified version.
saveSubset() is also NOT included for Node.js, you would need to use a browser to download the Ngrams file
*/
// eld.saveSubset(languagesSubset)
// load() Is not available at static sizes { eld/large } imports and bundles
// We can dynamically load any other database saved at src/ngrams/, including subsets. Returns true if success
await eld.load('medium') // Will not remove any active dynamic subset
// eld.load('medium').then((loaded) => { if (loaded) { /* code */ } })
// Finally, we can get the current status of eld: languages, database type and subset
console.log(eld.info())
/*
* Build and minify example with esbuild + terser
* With package installed:
* npx esbuild --bundle --format=esm eld/large --outfile=eld.large.js
* Using folder path:
* npx esbuild --bundle --format=esm src/entries/static.large.js > eld.large.js
* terser eld.large.js --compress --mangle --output eld.large.min.js
*
* For iife browser scripts
* npx esbuild --bundle --format=iife --global-name=__eld_module src/entries/static.extrasmall.js > eld.xs.js --footer:js="globalThis.eld = __eld_module.default;"
*/
</script>
</body>
</html>