-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsaveLanguageSubset.dev.js
108 lines (99 loc) · 3.01 KB
/
saveLanguageSubset.dev.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Copyright 2023 Nito T.M.
License https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
Author Nito T.M. (https://github.com/nitotm)
Package npmjs.com/package/eld
*/
import { isoLanguages } from './isoLanguages.js'
export const saveLanguageSubset = (function () {
/**
* Creates a ngrams database file download, only with the languages included in the langArray subset
*
* @param {Array} langArray
* @param {Object} ngrams
* @param {Object} defaultLanguages
* @param {string} type
*/
function saveSubset (langArray, ngrams, defaultLanguages, type) {
// langArray languages are already validated by dynamicLangSubset()
if (!langArray.length) {
return 'No languages found'
}
let newNgrams = JSON.parse(JSON.stringify(ngrams)) // Deep copy of object
const file = 'ngrams' + type + '-' + langArray.length + '_' + Date.now() + '.js'
for (let ngram in newNgrams) {
for (let id in newNgrams[ngram]) {
if (langArray.indexOf(parseInt(id)) === -1) {
delete newNgrams[ngram][id]
}
}
if (Object.keys(newNgrams[ngram]).length === 0) {
delete newNgrams[ngram]
}
}
download('// Copyright 2023 Nito T.M. [ Apache 2.0 Licence https://www.apache.org/licenses/LICENSE-2.0 ]\n' +
'export const ngramsData = {\n' +
' type: "' + type + '",\n' +
' languages: ' + JSON.stringify(isoLanguages(langArray, defaultLanguages)) + ',\n' +
' isSubset: true,\n' +
' ngrams: ' + ngramExport(newNgrams) + '\n' +
'}', file, 'js')
}
/**
* @param {Object} ngrams
* @returns {string}
*/
function ngramExport (ngrams) {
if (typeof ngrams === 'object' && ngrams) {
let toImplode = []
for (const property in ngrams) {
toImplode.push('\'' + property.replace(/'/g, '\\\'') + '\':' + joinNumbers(ngrams[property]))
}
return '{' + toImplode.join(',') + '}'
}
}
/**
* @param {Object} obj
* @returns {string}
*/
function joinNumbers (obj) {
let toImplode = []
for (const property in obj) {
toImplode.push(property + ':' + obj[property])
}
return '{' + toImplode.join(',') + '}'
}
/**
* Triggers file download at the web browser
*
* @param {string} data
* @param {string} filename
* @param {string} type
*/
function download (data, filename, type) {
const file = new Blob([data], { type: type })
if (typeof window === 'undefined') {
console.log('saveSubset() is only available at the Web Browser')
return
}
if (window.navigator.msSaveOrOpenBlob) {
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename)
} else {
// Others
let a = document.createElement('a')
let url = URL.createObjectURL(file)
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
setTimeout(function () {
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}, 0)
}
}
return {
saveSubset: saveSubset
}
})()