| 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 |
const path = require('path'); |
| 30 |
const fs = require('fs'); |
| 31 |
const packageRoot = path.join(__dirname, '..'); |
| 32 |
|
| 33 |
function requireOptionalPackage(packageName) { |
| 34 |
try { |
| 35 |
return require(packageName); |
| 36 |
} catch (error) { |
| 37 |
if (error && error.code === 'MODULE_NOT_FOUND') { |
| 38 |
return null; |
| 39 |
} |
| 40 |
throw error; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
function resolveBindingPath() { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
const candidates = [ |
| 51 |
path.join(packageRoot, 'build', 'Release', 'opencc.node'), |
| 52 |
path.join( |
| 53 |
packageRoot, 'prebuilds', `${process.platform}-${process.arch}`, 'opencc.node' |
| 54 |
), |
| 55 |
]; |
| 56 |
for (const candidate of candidates) { |
| 57 |
if (fs.existsSync(candidate)) { |
| 58 |
return candidate; |
| 59 |
} |
| 60 |
} |
| 61 |
const scopedBinaryPackage = requireOptionalPackage( |
| 62 |
`@opencc/opencc-${process.platform}-${process.arch}` |
| 63 |
); |
| 64 |
if (scopedBinaryPackage && scopedBinaryPackage.binaryPath) { |
| 65 |
return scopedBinaryPackage.binaryPath; |
| 66 |
} |
| 67 |
throw new Error( |
| 68 |
'Could not locate the opencc native addon (looked in build/Release, ' + |
| 69 |
'prebuilds/, and @opencc/opencc-* scoped packages)' |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
const bindingPath = resolveBindingPath(); |
| 74 |
const binding = require(bindingPath); |
| 75 |
|
| 76 |
const getAssetsPath = function (bindingPath) { |
| 77 |
const packageAssetsPath = path.join(packageRoot, 'prebuilds', 'assets'); |
| 78 |
if (fs.existsSync(packageAssetsPath)) { |
| 79 |
return packageAssetsPath; |
| 80 |
} |
| 81 |
|
| 82 |
const bindingDir = path.dirname(bindingPath); |
| 83 |
const prebuildsDir = path.dirname(bindingDir); |
| 84 |
if (path.basename(prebuildsDir) === 'prebuilds') { |
| 85 |
const sharedAssetsPath = path.join(prebuildsDir, 'assets'); |
| 86 |
if (fs.existsSync(sharedAssetsPath)) { |
| 87 |
return sharedAssetsPath; |
| 88 |
} |
| 89 |
} |
| 90 |
return bindingDir; |
| 91 |
}; |
| 92 |
|
| 93 |
function requireOptionalPeer(packageName) { |
| 94 |
const searchPaths = [ |
| 95 |
process.cwd(), |
| 96 |
path.join(__dirname, '..'), |
| 97 |
__dirname, |
| 98 |
]; |
| 99 |
for (const searchPath of searchPaths) { |
| 100 |
try { |
| 101 |
return require(require.resolve(packageName, { paths: [searchPath] })); |
| 102 |
} catch (e) { |
| 103 |
|
| 104 |
} |
| 105 |
} |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
const jiebaInfo = requireOptionalPeer('opencc-jieba'); |
| 112 |
|
| 113 |
const assetsPath = getAssetsPath(bindingPath); |
| 114 |
const isAbsolutePath = function (filePath) { |
| 115 |
return path.isAbsolute(filePath) || /^[A-Za-z]:[\\/]/.test(filePath); |
| 116 |
}; |
| 117 |
|
| 118 |
const getConfigPath = function (config) { |
| 119 |
let configPath = config; |
| 120 |
if (!isAbsolutePath(config)) { |
| 121 |
|
| 122 |
configPath = path.join(assetsPath, config); |
| 123 |
} |
| 124 |
return configPath; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
function patchDictPaths(dict, baseDir) { |
| 134 |
if (!dict) return; |
| 135 |
if (dict.type === 'group' && Array.isArray(dict.dicts)) { |
| 136 |
for (const d of dict.dicts) { |
| 137 |
patchDictPaths(d, baseDir); |
| 138 |
} |
| 139 |
} else if (dict.file && !path.isAbsolute(dict.file)) { |
| 140 |
dict.file = path.join(baseDir, dict.file); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
function filterTofuRiskDicts(dict, includeTofuRiskDictionaries) { |
| 145 |
if (!dict) return null; |
| 146 |
if (dict.type === 'inline') { |
| 147 |
return dict; |
| 148 |
} |
| 149 |
if (dict.may_output_tofu && !includeTofuRiskDictionaries) { |
| 150 |
return null; |
| 151 |
} |
| 152 |
if (dict.type === 'group' && Array.isArray(dict.dicts)) { |
| 153 |
dict.dicts = dict.dicts |
| 154 |
.map((d) => filterTofuRiskDicts(d, includeTofuRiskDictionaries)) |
| 155 |
.filter(Boolean); |
| 156 |
if (dict.dicts.length === 0) { |
| 157 |
return null; |
| 158 |
} |
| 159 |
} |
| 160 |
return dict; |
| 161 |
} |
| 162 |
|
| 163 |
function filterTofuRiskConversionChain(config, includeTofuRiskDictionaries) { |
| 164 |
if (!Array.isArray(config.conversion_chain)) return; |
| 165 |
config.conversion_chain = config.conversion_chain |
| 166 |
.map((step) => { |
| 167 |
if (!step || !step.dict) return step; |
| 168 |
step.dict = filterTofuRiskDicts(step.dict, includeTofuRiskDictionaries); |
| 169 |
return step.dict ? step : null; |
| 170 |
}) |
| 171 |
.filter(Boolean); |
| 172 |
} |
| 173 |
|
| 174 |
function isConfigObject(config) { |
| 175 |
return config && typeof config === 'object' && !Array.isArray(config); |
| 176 |
} |
| 177 |
|
| 178 |
function cloneConfigObject(config) { |
| 179 |
return JSON.parse(JSON.stringify(config)); |
| 180 |
} |
| 181 |
|
| 182 |
function createFromConfigObject(config, options) { |
| 183 |
if (!isConfigObject(config)) { |
| 184 |
throw new TypeError('OpenCC config must be an object'); |
| 185 |
} |
| 186 |
if (options.resourceZip) { |
| 187 |
throw new TypeError('resourceZip is only supported with config file names'); |
| 188 |
} |
| 189 |
|
| 190 |
const raw = cloneConfigObject(config); |
| 191 |
const includeTofuRiskDictionaries = |
| 192 |
options.includeTofuRiskDictionaries !== false; |
| 193 |
filterTofuRiskConversionChain(raw, includeTofuRiskDictionaries); |
| 194 |
|
| 195 |
const configDirectory = options.configDirectory || assetsPath; |
| 196 |
return new binding.Opencc( |
| 197 |
JSON.stringify(raw), |
| 198 |
configDirectory + (/[\\/]$/.test(configDirectory) ? '' : path.sep) |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
ge |
| 206 |
ts |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
function patchConfigPaths(config, jieba, mainAssetsDir) { |
| 213 |
|
| 214 |
if (jieba && config.segmentation) { |
| 215 |
config.segmentation.__plugin_library = jieba.pluginLibrary; |
| 216 |
|
| 217 |
if (config.segmentation.resources) { |
| 218 |
const res = config.segmentation.resources; |
| 219 |
for (const key of Object.keys(res)) { |
| 220 |
if (!path.isAbsolute(res[key])) { |
| 221 |
res[key] = path.join(jieba.dataDir, res[key]); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (Array.isArray(config.normalization)) { |
| 228 |
for (const step of config.normalization) { |
| 229 |
patchDictPaths(step.dict, mainAssetsDir); |
| 230 |
} |
| 231 |
} |
| 232 |
if (Array.isArray(config.conversion_chain)) { |
| 233 |
for (const step of config.conversion_chain) { |
| 234 |
patchDictPaths(step.dict, mainAssetsDir); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
function resolveJiebaConfigPath(config, jieba) { |
| 240 |
if (!jieba || isAbsolutePath(config)) { |
| 241 |
return null; |
| 242 |
} |
| 243 |
if (typeof jieba.resolveConfigPath === 'function') { |
| 244 |
return jieba.resolveConfigPath(config); |
| 245 |
} |
| 246 |
const candidates = [config]; |
| 247 |
if (!config.endsWith('.json')) { |
| 248 |
candidates.push(config + '.json'); |
| 249 |
} |
| 250 |
for (const candidate of candidates) { |
| 251 |
if (candidate.includes('/') || candidate.includes('\\')) { |
| 252 |
continue; |
| 253 |
} |
| 254 |
const configPath = path.join(jieba.dataDir, candidate); |
| 255 |
if (fs.existsSync(configPath)) { |
| 256 |
return configPath; |
| 257 |
} |
| 258 |
} |
| 259 |
return null; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
const OpenCC = module.exports = function (config, options) { |
| 270 |
if (!config) { |
| 271 |
config = 's2t.json'; |
| 272 |
} |
| 273 |
if (!options) { |
| 274 |
options = {}; |
| 275 |
} |
| 276 |
|
| 277 |
if (isConfigObject(config)) { |
| 278 |
this.handler = createFromConfigObject(config, options); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
const includeTofuRiskDictionaries = |
| 283 |
options.includeTofuRiskDictionaries !== false; |
| 284 |
|
| 285 |
if (options.resourceZip) { |
| 286 |
this.handler = new binding.Opencc( |
| 287 |
config, |
| 288 |
options.resourceZip, |
| 289 |
includeTofuRiskDictionaries |
| 290 |
); |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
const jiebaConfigPath = resolveJiebaConfigPath(config, jiebaInfo); |
| 298 |
if (jiebaConfigPath) { |
| 299 |
const raw = parseJSON(fs.readFileSync(jiebaConfigPath, 'utf-8')); |
| 300 |
filterTofuRiskConversionChain(raw, includeTofuRiskDictionaries); |
| 301 |
patchConfigPaths(raw, jiebaInfo, assetsPath); |
| 302 |
this.handler = new binding.Opencc( |
| 303 |
JSON.stringify(raw), |
| 304 |
jiebaInfo.dataDir + '/' |
| 305 |
); |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
config = getConfigPath(config); |
| 310 |
if (!includeTofuRiskDictionaries) { |
| 311 |
const raw = parseJSON(fs.readFileSync(config, 'utf-8')); |
| 312 |
filterTofuRiskConversionChain(raw, includeTofuRiskDictionaries); |
| 313 |
const configDir = path.dirname(config); |
| 314 |
if (raw.segmentation && raw.segmentation.dict) { |
| 315 |
patchDictPaths(raw.segmentation.dict, configDir); |
| 316 |
} |
| 317 |
if (Array.isArray(raw.conversion_chain)) { |
| 318 |
for (const step of raw.conversion_chain) { |
| 319 |
patchDictPaths(step.dict, configDir); |
| 320 |
} |
| 321 |
} |
| 322 |
this.handler = new binding.Opencc( |
| 323 |
JSON.stringify(raw), |
| 324 |
configDir + path.sep |
| 325 |
); |
| 326 |
return; |
| 327 |
} |
| 328 |
this.handler = new binding.Opencc(config); |
| 329 |
}; |
| 330 |
|
| 331 |
function parseJSON(str) { |
| 332 |
const cleanStr = str.replace(/"(?:[^"\\]|\\.)*"|(\/\/.*|\/\*[\s\S]*?\*\/)|(,\s*(?=[\]}]))/g, (m, g1, g2) => (g1 || g2) ? "" : m); |
| 333 |
return JSON.parse(cleanStr); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
OpenCC.OpenCC = OpenCC; |
| 338 |
OpenCC._parseJSON = parseJSON; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
OpenCC._bindingPath = bindingPath; |
| 344 |
OpenCC._assetsPath = assetsPath; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
OpenCC.version = binding.Opencc.version(); |
| 354 |
|
| 355 |
OpenCC.fromConfig = function (config, options) { |
| 356 |
return new OpenCC(config, options); |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
OpenCC.generateDict = function (inputFileName, outputFileName, |
| 371 |
formatFrom, formatTo) { |
| 372 |
return binding.Opencc.generateDict(inputFileName, outputFileName, |
| 373 |
formatFrom, formatTo); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
OpenCC.prototype.convert = function (input, callback) { |
| 386 |
return this.handler.convert(input.toString(), callback); |
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
OpenCC.prototype.convertSync = function (input) { |
| 399 |
return this.handler.convertSync(input.toString()); |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
OpenCC.prototype.convertPromise = function (input) { |
| 412 |
const self = this; |
| 413 |
return new Promise(function (resolve, reject) { |
| 414 |
self.handler.convert(input.toString(), function (err, text) { |
| 415 |
if (err) reject(err); |
| 416 |
else resolve(text); |
| 417 |
}); |
| 418 |
}); |
| 419 |
}; |
| 420 |
|
| 421 |
OpenCC.prototype._createConverterStream = function () { |
| 422 |
return new binding.OpenccStream(this.handler); |
| 423 |
}; |