UNPKG

node-darts

Version:

Node.js Native Addon for Darts (Double-ARray Trie System)

177 lines 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const native_1 = require("./native"); const errors_1 = require("./errors"); /** * Darts Dictionary class * Provides dictionary search using Double-Array Trie */ class Dictionary { /** * Constructor * @param handle Dictionary handle (optional) * @param words Array of words (optional) */ constructor(handle, words) { this.handle = handle !== undefined ? handle : native_1.dartsNative.createDictionary(); this.isDisposed = false; this.words = words || []; } /** * Gets the native handle * @returns The native handle */ getHandle() { this.ensureNotDisposed(); return this.handle; } /** * Performs an exact match search * @param key search key * @returns the corresponding value if found, -1 otherwise * @throws {DartsError} if the search fails */ exactMatchSearch(key) { this.ensureNotDisposed(); // Return -1 if the dictionary is empty if (this.size() === 0) { return -1; } return native_1.dartsNative.exactMatchSearch(this.handle, key); } /** * Performs a common prefix search * @param key search key * @returns array of found values * @throws {DartsError} if the search fails */ commonPrefixSearch(key) { this.ensureNotDisposed(); // Return an empty array if the dictionary is empty if (this.size() === 0) { return []; } return native_1.dartsNative.commonPrefixSearch(this.handle, key); } /** * Traverses the trie * @param key search key * @param callback callback function * @throws {DartsError} if the traversal fails */ traverse(key, callback) { this.ensureNotDisposed(); native_1.dartsNative.traverse(this.handle, key, callback); } /** * Loads a dictionary file asynchronously * @param filePath path to the dictionary file * @returns true if successful, false otherwise * @throws {FileNotFoundError} if the file is not found * @throws {InvalidDictionaryError} if the dictionary file is invalid */ async load(filePath) { return new Promise((resolve, reject) => { try { const result = this.loadSync(filePath); resolve(result); } catch (error) { reject(error); } }); } /** * Loads a dictionary file synchronously * @param filePath path to the dictionary file * @returns true if successful, false otherwise * @throws {FileNotFoundError} if the file is not found * @throws {InvalidDictionaryError} if the dictionary file is invalid */ loadSync(filePath) { this.ensureNotDisposed(); return native_1.dartsNative.loadDictionary(this.handle, filePath); } /** * Gets the size of the dictionary * @returns size of the dictionary * @throws {DartsError} if getting the size fails */ size() { this.ensureNotDisposed(); return native_1.dartsNative.size(this.handle); } /** * Searches for dictionary words in a text and replaces them * @param text The text to search in * @param replacer The replacement method (function or object) * @returns The text after replacement */ replaceWords(text, replacer) { this.ensureNotDisposed(); let result = ''; let position = 0; while (position < text.length) { let matchFound = false; // Try to match words at the current position for (let len = Math.min(50, text.length - position); len > 0; len -= 1) { const word = text.substring(position, position + len); const value = this.exactMatchSearch(word); if (value !== -1) { // Word found in dictionary let replacement; if (typeof replacer === 'function') { // If it's a callback function replacement = replacer(word); } else { // If it's a replacement map (object) replacement = replacer[word] || word; } result += replacement; position += len; matchFound = true; break; } } if (!matchFound) { // No match found, advance by 1 character result += text[position]; position += 1; } } return result; } /** * Gets a word by its value * @param value The value to look up * @returns The corresponding word or undefined if not found */ getWordByValue(value) { if (this.words.length > value && value >= 0) { return this.words[value]; } return undefined; } /** * Releases resources * After calling this method, this object can no longer be used */ dispose() { if (!this.isDisposed && this.handle !== undefined) { native_1.dartsNative.destroyDictionary(this.handle); this.isDisposed = true; } } /** * Ensures the object has not been disposed * @throws {DartsError} if the object has been disposed */ ensureNotDisposed() { if (this.isDisposed) { throw new errors_1.DartsError('Dictionary object has been disposed'); } } } exports.default = Dictionary; //# sourceMappingURL=dictionary.js.map