### Multi-Profile Setup Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/sort-dictionaries.md Example demonstrating how to configure multiple profiles for different use cases like Japanese, Chinese, and Cantonese. ```javascript const profiles = { /* Profile 0: Japanese */ 0: { groupOrder: ['jafreq', 'ja', 'zhfreq', 'zh', 'yuefreq', 'yue'], enabledGroups: ['jafreq', 'ja'], // Only enable Japanese }, /* Profile 1: Chinese */ 1: { groupOrder: ['zhfreq', 'zh', 'yuefreq', 'yue', 'jafreq', 'ja'], enabledGroups: ['zhfreq', 'zh'], // Only enable Chinese }, /* Profile 2: Cantonese */ 2: { groupOrder: ['yuefreq', 'yue', 'zhfreq', 'zh', 'jafreq', 'ja'], enabledGroups: ['yuefreq', 'yue'], // Only enable Cantonese }, }; ``` -------------------------------- ### Complete Configuration Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md A comprehensive example combining both `profiles` and `groups` objects for a multi-language setup. This configuration defines dictionary organization and matching rules. ```javascript const profiles = { 0: { groupOrder: ['jafreq', 'ja', 'zhfreq', 'zh', 'yuefreq', 'yue'], enabledGroups: ['jafreq', 'ja', 'zhfreq', 'zh'], }, 1: { groupOrder: ['zhfreq', 'zh', 'jafreq', 'ja'], enabledGroups: ['zhfreq', 'zh'], }, }; const groups = { jafreq: [ /^JPDB$/, /^Innocent Ranked$/, /^Novels$/, /^Youtube$/, /^BCCWJ-LUW$/, /^CC100$/, ], ja: [ /^NHK$/, /^大辞泉$/, /^新明解.*版$/, /^大辞林.*版$/, /^Jitendex.*$/, /^JMnedict$/, ], zhfreq: [ /^HSK$/, /^SUBTLEX-CH$/, /^BLCUlit$/, ], zh: [ /^CC-CEDICT.*$/, /^中日大辞典.*版?$/, /^白水社中国語辞典$/, ], yuefreq: [ /^Words\.hk Frequency$/, /^Cifu Spoken$/, ], yue: [ /^Words\.hk 粵典.*$/, /^CantoDict$/, /^Canto CEDICT$/, ], }; ``` -------------------------------- ### Simple Single-Profile Setup Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/sort-dictionaries.md Sets up a basic profile with two groups, 'jafreq' and 'ja', defining their order and enabled status. ```javascript const profiles = { 0: { groupOrder: ['jafreq', 'ja'], enabledGroups: ['jafreq', 'ja'], }, }; const groups = { jafreq: [ /^JPDB$/, /^Innocent Ranked$/, ], ja: [ /^NHK$/, /^大辞泉$/, /^Jitendex.*$/, ], }; ``` -------------------------------- ### Environment Setup Commands Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/README.md Provides essential commands for setting up the project environment, including creating an output directory, installing dependencies, and placing necessary files. ```bash mkdir -p ./dl # Output directory npm install # Install dependencies # Place util/jmdict_english.zip # If using getJMDictInfo ``` -------------------------------- ### Install Dependencies Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Command to install all project dependencies using npm. ```bash npm install ``` -------------------------------- ### Minimal Configuration Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md An example of a minimal configuration for Yomitan profiles and groups. It defines group order, enabled groups, and regex patterns for each group. ```javascript const profiles = { 0: { groupOrder: ['freq', 'defs'], enabledGroups: ['freq', 'defs'], }, }; const groups = { freq: [ /^JPDB$/, /^Innocent.*$/, ], defs: [ /^NHK$/, /^.*大辞.*$/, /^Jitendex.*$/, ], }; ``` -------------------------------- ### Project Dependencies Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/README.md Lists the npm packages required for the project, including versions. Install all dependencies using 'npm install'. ```json { "axios": "^0.27.2", "jsdom": "^20.0.0", "jszip": "^3.10.1", "cli-progress": "^3.12.0", "csv-parser": "^3.0.0", "csvtojson": "^2.0.10", "qs": "^6.11.1", "word-wrap": "^1.2.3", "yomichan-dict-builder": "^2.0.0", "yomichan-dict-reader": "^1.2.2" } ``` -------------------------------- ### Example: Get Deinflector Info Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/get-jmdict-info.md Demonstrates how to use the `getDeinflectors` function to retrieve conjugation information for a verb. Includes a safe call example that returns an empty string for unknown entries. ```javascript const { getDeinflectors } = require('./util/getJMDictInfo'); // Get deinflector info for a verb const deinflectors = await getDeinflectors('食べる', 'たべる'); console.log(deinflectors); // Deinflector information for the verb // Safe call - returns empty string if entry not found const unknown = await getDeinflectors('zzzzz', 'zzzz'); console.log(unknown); // '' ``` -------------------------------- ### Console Output Examples Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/sort-dictionaries.md The script logs its progress and any issues encountered during dictionary sorting to the console. ```text Sorting dictionaries for profile 0... Updated dictionaries order! Failed to match dictionaries: [Set of unmatched regex patterns] Unknown dictionaries found: [Set of dictionary names not in sort order] ``` -------------------------------- ### Example JMDict Entry Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Illustrates a concrete example of a JMDict entry object, showing typical values for its fields. ```javascript const entry = { reading: 'たべる', tags: 'v1 trans', deinflectors: 'v1 v1-s v4u', popularity: 42, sequence: 1000001, bigTags: 'common', }; ``` -------------------------------- ### Profiles Configuration Type Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md An example of the profiles configuration type, defining per-profile dictionary organization settings including group order and enabled groups. ```javascript const profiles = { 0: { groupOrder: ['jafreq', 'ja'], enabledGroups: ['jafreq', 'ja'], }, 1: { groupOrder: ['zhfreq', 'zh'], enabledGroups: ['zhfreq', 'zh'], }, }; ``` -------------------------------- ### Individual Imports Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Examples of importing individual utility functions and modules. ```javascript // Individual imports const saveDict = require('./util/saveDict'); const { getURL, getJSON } = require('./util/scrape'); const { getDeinflectors } = require('./util/getJMDictInfo'); const japUtils = require('./util/japaneseUtils'); const writeJson = require('./util/writeJson'); ``` -------------------------------- ### Usage Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/save-dict.md Demonstrates how to import and use the saveDict function with dictionary data and a desired filename. ```javascript const saveDict = require('./util/saveDict'); const dictData = { /* ... */ }; saveDict(dictData, 'Dictionary Name.zip'); ``` -------------------------------- ### Node.js Environment Variables Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Examples of setting environment variables for Node.js scripts. Used for controlling script behavior, such as setting the environment to production or enabling debug logging. ```bash NODE_ENV=production npm run build DEBUG=* npm start ``` -------------------------------- ### Multi-Language Profiles Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/sort-dictionaries.md Illustrates setting up multiple profiles for different languages (Japanese, Chinese) and a study mode profile that includes all languages. ```javascript const profiles = { 0: { // Japanese groupOrder: ['jafreq', 'ja'], enabledGroups: ['jafreq', 'ja'], }, 1: { // Chinese groupOrder: ['zhfreq', 'zh'], enabledGroups: ['zhfreq', 'zh'], }, 2: { // Study mode - all languages groupOrder: ['jafreq', 'ja', 'zhfreq', 'zh'], enabledGroups: ['jafreq', 'ja', 'zhfreq', 'zh'], }, }; const groups = { jafreq: [/^JPDB$/, /^Innocent Ranked$/], ja: [/^NHK$/, /^大辞泉$/], zhfreq: [/^HSK$/, /^SUBTLEX-CH$/], zh: [/^CC-CEDICT.*$/, /^漢語大詞典$/], }; ``` -------------------------------- ### Group Configuration Type Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md An example of the group configuration type, mapping group identifiers to arrays of regular expression patterns for dictionary matching. ```javascript const groups = { jafreq: [ /^JPDB$/, /^Innocent Ranked$/, /^Novels$/, ], ja: [ /^NHK$/, /^大辞泉$/, /^Jitendex.*$/, ], }; ``` -------------------------------- ### Example: Cache Loading Times Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/get-jmdict-info.md Compares the loading times for the first call to `getJMDictData` (which loads from disk) versus subsequent calls (which use the cache). ```javascript const { getJMDictData } = require('./util/getJMDictInfo'); console.time('First load'); await getJMDictData(); console.timeEnd('First load'); // First load: 2345.67ms console.time('Second load (cached)'); await getJMDictData(); console.timeEnd('Second load (cached)'); // Second load (cached): 0.12ms ``` -------------------------------- ### Example Fix for Unknown Dictionaries Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md This example demonstrates how to fix the 'Unknown dictionaries found' warning by adding a regex pattern for a new dictionary to the 'groups' object and ensuring it's included in 'profiles'. ```javascript const groups = { ja: [ /^NHK$/, // Add new pattern for unknown dictionary /^My Custom Dictionary$/, ], }; const profiles = { 0: { groupOrder: ['ja'], // Already includes our group enabledGroups: ['ja'], }, }; ``` -------------------------------- ### Build Term Dictionary Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/save-dict.md This example shows how to create and save a term dictionary, splitting entries into multiple term banks if necessary. It includes a basic index file and term data. ```javascript const saveDict = require('./util/saveDict'); // Term dictionary data const termBanks = []; const terms = [ ['単語', 'たんご', 'word; term', [], 0, [], []], ['単語帳', 'たんごちょう', 'word list; vocabulary list', [], 1, [], []], ]; // Split into chunks (e.g., 1000 entries per bank) for (let i = 0; i < terms.length; i += 1000) { termBanks.push(terms.slice(i, i + 1000)); } const data = { 'index.json': { title: 'My Term Dictionary', revision: 'terms1', format: 3, author: 'Me', }, }; // Add term banks for (let i = 0; i < termBanks.length; i++) { data[`term_bank_${i + 1}.json`] = termBanks[i]; } saveDict(data, 'Term Dictionary.zip'); ``` -------------------------------- ### Example Enabled Yomitan Dictionary Option Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Illustrates a typical configuration for an enabled dictionary, showing its name, enabled status, and priority. ```javascript { name: 'JPDB', enabled: true, priority: 10000, } ``` -------------------------------- ### Custom Group Organization Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/sort-dictionaries.md Demonstrates organizing dictionaries into custom groups like 'pronunciation', 'definition', and 'rare', and configuring a profile to use these groups. ```javascript const groups = { pronunciation: [ /^NHK$/, /^大辞泉$/, ], definition: [ /^広辞苑.*$/, /^デジタル大辞泉$/, ], rare: [ /^漢字林$/, /^全訳漢辞海$/, ], }; const profiles = { 0: { groupOrder: ['pronunciation', 'definition', 'rare'], enabledGroups: ['pronunciation', 'definition'], }, }; ``` -------------------------------- ### Build Kanji Frequency Dictionary Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/save-dict.md This example demonstrates how to construct and save a kanji frequency dictionary. It generates placeholder kanji data and formats it according to the Yomichan/Yomitan specification. ```javascript const saveDict = require('./util/saveDict'); // Kanji frequency data const kanjiFreqData = []; for (let i = 0; i < 2136; i++) { const kanji = '漢'; // Placeholder const frequency = i + 1; kanjiFreqData.push([ kanji, 'freq', { value: frequency, displayValue: `${frequency} (occurrences)`, }, ]); } const freqDict = { 'index.json': { title: 'Kanji Frequency', revision: 'kanjifreq1', format: 3, frequencyMode: 'rank-based', author: 'Dictionary Creator', }, 'kanji_meta_bank_1.json': kanjiFreqData, }; saveDict(freqDict, '[Frequency] Kanji.zip'); ``` -------------------------------- ### Example Frequency Yomitan Dictionary Option Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Demonstrates the configuration for a frequency dictionary, including its priority which influences lookup order. ```javascript { name: 'JPDB Frequency', enabled: true, priority: 10010, } ``` -------------------------------- ### Yomitan Dictionary Priority Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Example of how dictionary priorities are set in Yomitan's configuration. Higher priority values mean the dictionary is searched earlier. The script modifies these values to organize dictionary search order. ```javascript dictionaries[0].priority = 10010 // Group 1, position 0 dictionaries[1].priority = 10000 // Group 1, position 1 dictionaries[2].priority = 9990 // Group 1, position 2 dictionaries[3].priority = 100 // Group 2, position 0 dictionaries[4].priority = 90 // Group 2, position 1 ``` -------------------------------- ### Multi-Language Profile Configuration Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Example demonstrating how to configure multiple profiles for different languages, such as Japanese, Chinese, and Cantonese. Each profile defines its own group order and enabled groups. ```javascript const profiles = { // Profile 0: Japanese focus 0: { groupOrder: ['jafreq', 'ja', 'zhfreq', 'zh', 'yuefreq', 'yue'], enabledGroups: ['jafreq', 'ja'], }, // Profile 1: Chinese focus 1: { groupOrder: ['zhfreq', 'zh', 'yuefreq', 'yue', 'jafreq', 'ja'], enabledGroups: ['zhfreq', 'zh'], }, // Profile 2: Cantonese focus 2: { groupOrder: ['yuefreq', 'yue', 'zhfreq', 'zh', 'jafreq', 'ja'], enabledGroups: ['yuefreq', 'yue'], }, }; ``` -------------------------------- ### Kanji Data Type Example Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md An example of the internal Kanji Data Type used during dictionary-building, showing fields like frequency, readings, types, and composition. ```javascript { '漢': { frequency: 1, readings: [['かん'], ['から']], types: ['Shinjitai'], kanken: 'N1', vocab: ['漢字', '漢語', '漢文'], composedOf: ['水', '田'], }, '字': { frequency: 2, readings: [['じ'], ['あざ']], kanken: 'N1', }, } ``` -------------------------------- ### Limit Example Sentences in NEW Saitou Dictionary Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/readme.md Apply this CSS to the NEW Saitou Japanese-English Dictionary to limit the number of displayed example sentences. Adjust the 'n + 5' to change the limit. ```css [data-dictionary='NEW斎藤和英大辞典'] ul.gloss-sc-ul > li:nth-child(n + 5) { display: none; } ``` -------------------------------- ### Example Yomichan Bilingual Dictionary Index Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Demonstrates the `index.json` structure for a bilingual dictionary, including fields like URL and attribution. ```javascript { title: 'Jitendex', revision: 'jitendex_4.3', format: 3, url: 'https://github.com/stephenmk/Jitendex', description: 'Free Japanese-English dictionary', author: 'Stephen Kraus', attribution: 'Jitendex', } ``` -------------------------------- ### JavaScript Logging Output Examples Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Standard console output for dictionary sorting operations. This can be captured or filtered in the browser console. ```text Sorting dictionaries for profile {index}... Updated dictionaries order! Failed to match dictionaries: [Set of patterns] Unknown dictionaries found: [Set of names] ``` -------------------------------- ### Example: Build Verb Conjugation Information Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/get-jmdict-info.md Shows how to process deinflector data to build conjugation information for a verb. Logs the possible conjugations or a message indicating no recorded conjugations. ```javascript const { getDeinflectors } = require('./util/getJMDictInfo'); async function getVerbConjugations(term, reading) { const deinflectors = await getDeinflectors(term, reading); if (deinflectors) { // Process deinflector data const forms = deinflectors.split(' '); console.log(`${term} can be conjugated to: ${forms.join(', ')}`); } else { console.log(`${term} has no recorded conjugations`); } } await getVerbConjugations('食べる', 'たべる'); ``` -------------------------------- ### Regex Pattern Examples Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Illustrates various regex patterns for matching dictionary names, including exact matches, prefix/suffix matches, and patterns with optional characters. ```regex /^JPDB$/ // Exact match ``` ```regex /^Jitendex.*$/ // Starts with "Jitendex" ``` ```regex /^.*Wikipedia.*$/ // Contains "Wikipedia" ``` ```regex /^新明解.*版$/ // Japanese: starts with 新明解, ends with 版 ``` ```regex /^中日大辞典.*版?$/ // Optional suffix ``` -------------------------------- ### Destructured Imports Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Examples of importing specific functions from utility modules using destructuring. ```javascript // Destructured imports const { hiraganaToKatakana, normalizeReading } = require('./util/japaneseUtils'); const { getURL, wait } = require('./util/scrape'); ``` -------------------------------- ### Example Yomichan Kanji Dictionary Index Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Illustrates the `index.json` structure for a Kanji dictionary, including optional fields like URL and attribution. ```javascript { title: 'JPDB Kanji', revision: 'jpdb_kanji_2024-01-15T10:30:00Z', format: 3, url: 'https://jpdb.io', description: 'Kanji information from JPDB including readings, meanings, and example vocabulary', author: 'JPDB, Marv', attribution: 'JPDB', } ``` -------------------------------- ### Example Yomichan Frequency Dictionary Index Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Shows the `index.json` structure for a frequency dictionary, highlighting the `frequencyMode` field. ```javascript { title: 'JPDB Kanji Frequency', revision: 'jpdb_kanji_freq_2024-01-15', format: 3, frequencyMode: 'rank-based', description: 'Kanji frequency ranking from JPDB corpus', author: 'JPDB', } ``` -------------------------------- ### Example Disabled Yomitan Dictionary Option Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/types.md Shows the configuration for a disabled dictionary, where the `enabled` field is set to `false`. ```javascript { name: 'Rare Dictionary', enabled: false, priority: 100, } ``` -------------------------------- ### Check Node.js Version Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Command to check the installed Node.js version. Requires Node.js 14.0.0 or higher. ```bash node --version ``` -------------------------------- ### Standard JSON File Output Format Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/write-json.md This example illustrates the expected format of JSON files generated by the `writeJson` utility, showing two-space indentation and proper escaping of values. ```json { "key1": "value1", "key2": { "nested": "value2" }, "array": [ "item1", "item2" ] } ``` -------------------------------- ### Success Logging in saveDict.js Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Example of the success logging message for the saveDict.js script, indicating that a dictionary ZIP file has been successfully written. ```javascript Wrote {outputZipName} ``` -------------------------------- ### Import Japanese Utility Functions Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/README.md Import necessary functions for Japanese text processing from the japaneseUtils module. This setup is required before using any of these utility functions. ```javascript const { hiraganaToKatakana, katakanaToHiragana, containsKanji, isHiragana, isKatakana, normalizeReading } = require('./util/japaneseUtils'); ``` -------------------------------- ### Example Regex Patterns for Dictionary Matching Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/sort-dictionaries.md Demonstrates various regex patterns used to match dictionary names. These patterns can be exact, partial, or include Japanese characters. ```javascript /^JPDB$/ // Exact name match /^Jitendex.*$/ // Starts with "Jitendex" /^JA Wikipedia.*$/ // Starts with "JA Wikipedia" /^漢検漢字辞典.*版$/ // Japanese kanji patterns ``` -------------------------------- ### Success Logging in writeJson.js Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Example of the success logging message for the writeJson.js script, indicating that a JSON file has been successfully written. ```javascript Wrote {filename} ``` -------------------------------- ### Manage Dictionary Order in Browser Console Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Configuration for managing dictionary profiles and groups within the Yomitan options page console. This example sets up profile '0' with specific group orders and enabled groups. ```javascript // Paste into Yomitan options page console: const profiles = { 0: { groupOrder: ['freq', 'ja', 'zh'], enabledGroups: ['freq', 'ja'], }, }; const groups = { freq: [/^JPDB$/, /^Innocent Ranked$/], ja: [/^NHK$/, /^大辞泉$/, /^Jitendex.*$/], zh: [/^CC-CEDICT.*$/, /^HSK$/], }; // Then copy rest of sort-dictionaries.js and run ``` -------------------------------- ### Handle Path-Related Write Errors Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/write-json.md This example demonstrates how to catch and handle specific file system errors like permission denied (`EACCES`) or directory not found (`ENOENT`) when using `writeJson`. ```javascript const writeJson = require('./util/writeJson'); async function safeWrite(object, filename) { try { await writeJson(object, filename); } catch (error) { if (error.code === 'EACCES') { console.error(`Permission denied: ${filename}`); } else if (error.code === 'ENOENT') { console.error(`Directory not found: ${filename}`); } else { console.error(`Write failed: ${error.message}`); } } } ``` -------------------------------- ### Custom Timeout Wrapper for getJSON Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/scrape.md This example demonstrates how to implement a custom timeout for the `getJSON` function using `Promise.race` and a `wait` utility. It ensures that the request does not hang indefinitely. ```javascript const { getJSON, wait } = require('./util/scrape'); // Implement a custom timeout wrapper async function getJSONWithTimeout(url, timeoutMs = 30000) { let resolved = false; const promise = getJSON(url); const timeoutPromise = wait(timeoutMs).then(() => { if (!resolved) throw new Error('Timeout'); return null; }); try { const result = await Promise.race([promise, timeoutPromise]); resolved = true; return result; } catch (error) { console.error(`Failed to fetch ${url}: ${error}`); throw error; } } ``` -------------------------------- ### Iterative JSON Saves During Processing Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/write-json.md This example demonstrates saving intermediate results to JSON files periodically during a large dataset processing task. It includes saving checkpoints every 100 items and a final save. ```javascript const writeJson = require('./util/writeJson'); async function processLargeDataset(items) { const results = {}; for (let i = 0; i < items.length; i++) { const processed = await processItem(items[i]); results[items[i].id] = processed; // Save every 100 items if ((i + 1) % 100 === 0) { await writeJson(results, `./results-checkpoint-${i + 1}.json`); console.log(`Processed ${i + 1} items`); } } // Final save await writeJson(results, './results-final.json'); } ``` -------------------------------- ### Dictionary Building Script Configuration Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Configuration constants for dictionary building scripts, such as setting limits for example vocabulary per kanji and the wait time in milliseconds between network requests. These are hardcoded and may require modification for different paths or values. ```javascript const VOCAB_LIMIT = 25; // Max example vocabulary per kanji const WAIT_MS = 1500; // Milliseconds to wait between requests ``` ```javascript const folderPath = './jpdb/'; const outputZipName = '[Kanji] JPDB Kanji.zip'; ``` -------------------------------- ### Importing Individual Utility Modules (CommonJS) Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/api-overview.md Demonstrates how to import specific utility modules using CommonJS syntax in Node.js. ```javascript const saveDict = require('./util/saveDict'); const { getURL, getJSON, wait } = require('./util/scrape'); const { getDeinflectors } = require('./util/getJMDictInfo'); const japUtils = require('./util/japaneseUtils'); const writeJson = require('./util/writeJson'); ``` -------------------------------- ### Create Download Directory Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/api-overview.md Ensure the './dl/' directory exists before calling saveDict() to prevent file system errors. ```bash mkdir -p ./dl/ ``` -------------------------------- ### Error Handling Log Output Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/scrape.md Provides an example of the log output generated when errors occur during scraping operations. ```text Error getting https://example.com: Error details here ``` -------------------------------- ### Create Output Directory Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/save-dict.md Ensure the './dl/' directory exists for saving ZIP files. This command creates the directory if it does not already exist. ```bash mkdir -p ./dl ``` -------------------------------- ### Process and Build Dictionary Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/README.md This pattern processes a list of entries to build a dictionary structure, normalizing readings and saving the output as a zip file. It relies on utility functions for normalization and saving. ```javascript const { normalizeReading } = require('./util/japaneseUtils'); const saveDict = require('./util/saveDict'); async function buildDict(entries) { const data = { 'index.json': { title: 'Dict', revision: '1', format: 3 }, 'term_bank_1.json': entries.map(e => [ e.term, normalizeReading(e.term, e.reading), e.definition, [], 0, [], [] ]), }; saveDict(data, 'Dictionary.zip'); } ``` -------------------------------- ### Placeholder: Get Kanji Readings Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/get-jmdict-info.md A placeholder function for future implementation to retrieve on and kun readings of a kanji from KANJIDIC data. ```javascript function getKanjiReadings(kanji: string): void ``` -------------------------------- ### Build Yomitan Dictionary Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/README.md This script demonstrates the complete process of building a Yomitan dictionary. It fetches data, processes entries by filtering for kanji and normalizing readings, creates the dictionary structure, and saves it as a ZIP file. ```javascript const { getJSON } = require('./util/scrape'); const { normalizeReading, containsKanji } = require('./util/japaneseUtils'); const saveDict = require('./util/saveDict'); async function buildDictionary() { // 1. Fetch data const entries = await getJSON('https://api.example.com/terms'); // 2. Process entries const processed = entries .filter(e => containsKanji(e.term)) .map(e => [ e.term, normalizeReading(e.term, e.reading), e.definition, [], 0, [], [], ]); // 3. Create dictionary const dictionaryData = { 'index.json': { title: 'My Dictionary', revision: '1.0', format: 3, author: 'Me', description: 'Custom dictionary', }, 'term_bank_1.json': processed, }; // 4. Save to ZIP saveDict(dictionaryData, 'My Dictionary.zip'); // → Saved to ./dl/My Dictionary.zip } buildDictionary().catch(console.error); ``` -------------------------------- ### Dependencies Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/scrape.md Lists the project's dependencies, including `axios` for HTTP requests and `jsdom` for DOM manipulation. ```json { "axios": "^0.27.2", "jsdom": "^20.0.0" } ``` -------------------------------- ### Sort Yomitan Dictionaries Automatically Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/readme.md Use this JavaScript snippet in the Yomitan console to automatically sort your installed dictionaries. Paste the script and press enter. ```javascript function sortDictionaries() { const order = [ "JMnedict", "JMdict", "Jitendex", "NEW 斎藤和英大辞典", "小学館例解学習国語 第十二版", "大辞泉 第二版", "実用日本語表現辞典", "PixivLight", "使い方の分かる 類語例解辞典", "漢検漢字辞典 第二版", "dojg-consolidated-v1_01", "JPDB_v2.1_kana_display_only", "jiten_freq_global", "Freq_CC100", "BCCWJ-LUW", "KANJIDIC_english", "JPDB Kanji", "NHK2016" ]; const dictionaryList = document.querySelectorAll("#dictionaries-list .dictionary-entry"); const dictionaries = []; dictionaryList.forEach(entry => { const name = entry.querySelector(".dictionary-name").innerText; const id = entry.dataset.dictionaryId; dictionaries.push({ name, id, element: entry }); }); dictionaries.sort((a, b) => { const indexA = order.indexOf(a.name); const indexB = order.indexOf(b.name); if (indexA === -1 && indexB === -1) return 0; if (indexA === -1) return 1; if (indexB === -1) return -1; return indexA - indexB; }); const parent = dictionaryList[0].parentNode; dictionaries.forEach(dict => { parent.appendChild(dict.element); }); console.log("Dictionaries sorted."); } sortDictionaries(); ``` -------------------------------- ### Error Logging in scrape.js Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/configuration.md Example of the error logging format used in the scrape.js script. It logs the URL and the specific error message encountered during fetching. ```javascript let waitTime = 10000; // Initial wait: 10 seconds waitTime *= 2; // Double on each retry ``` -------------------------------- ### Importing Japanese Utility Functions Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/japanese-utils.md Demonstrates how to import all Japanese utility functions using CommonJS module exports or destructure individual functions for use in your project. ```javascript module.exports = { hiraganaToKatakana, katakanaToHiragana, containsKanji, isHiragana, isKatakana, normalizeReading, }; ``` ```javascript const japUtils = require('./util/japaneseUtils'); // Or destructure individual functions const { containsKanji, isKatakana, normalizeReading } = require('./util/japaneseUtils'); if (containsKanji(term)) { const normalized = normalizeReading(term, reading); // ... } ``` -------------------------------- ### Error Handling: File Not Found Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/get-jmdict-info.md Demonstrates how to handle potential errors when loading JMDict data, specifically a 'file not found' scenario for `jmdict_english.zip`. ```javascript const { getJMDictData } = require('./util/getJMDictInfo'); try { const data = await getJMDictData(); } catch (error) { console.error('Failed to load JMDict:', error.message); // Handle missing jmdict_english.zip } ``` -------------------------------- ### Save Dictionary Data to ZIP Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/save-dict.md Use this function to package dictionary data into a ZIP archive and save it to the './dl/' directory. Ensure the output directory exists before calling. ```javascript const saveDict = require('./util/saveDict'); // Prepare dictionary data const dictionaryData = { 'index.json': { title: 'My Dictionary', revision: 'mydict1', format: 3, url: 'https://example.com', description: 'A sample dictionary', author: 'Me', }, 'term_bank_1.json': [ ['単語', 'たんご', 'word', [], 0, [], []], ['漢字', 'かんじ', 'kanji character', [], 1, [], []], ], 'kanji_bank_1.json': [ ['漢', 'reading', { value: 1, displayValue: '1 (common)' }], ], }; // Save the dictionary saveDict(dictionaryData, 'My Dictionary.zip'); // Console output: Wrote My Dictionary.zip ``` -------------------------------- ### Write Kanji Data to JSON Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/write-json.md This example demonstrates writing a more complex nested object, such as kanji data, to a JSON file. It ensures that nested structures are correctly serialized. ```javascript const writeJson = require('./util/writeJson'); const kanjiData = { '漢': { readings: ['かん'], meanings: ['Sino-'], frequency: 1, jlpt: 2, }, '字': { readings: ['じ'], meanings: ['character'], frequency: 2, jlpt: 1, }, }; await writeJson(kanjiData, './kanji.json'); ``` -------------------------------- ### Validation Before Operation: Directory Creation and JSON Serialization Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md This pattern ensures operations proceed only after necessary preconditions are met. It creates a directory if it doesn't exist and validates that data is JSON-serializable before saving. ```javascript const fs = require('fs'); const saveDict = require('./util/saveDict'); function saveWithValidation(data, filename) { // Create output directory if missing if (!fs.existsSync('./dl')) { fs.mkdirSync('./dl', { recursive: true }); } // Validate data is JSON-serializable try { JSON.stringify(data); } catch (error) { throw new Error(`Data not JSON-serializable: ${error.message}`); } // Save saveDict(data, filename); } ``` -------------------------------- ### Get Deinflectors for a Term-Reading Pair Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/get-jmdict-info.md Retrieves deinflector information for a given term and its reading. Returns an empty string if not found or on error. Catches and logs errors internally. ```javascript async function getDeinflectors(term: string, reading: string): Promise ``` -------------------------------- ### Handle Undefined Values in JSON Serialization Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md Address errors arising from 'undefined' values in JSON serialization. This example shows how to create a cleaned object excluding undefined properties. ```javascript // undefined saveDict({ 'file.json': { key: undefined } }, 'out.zip'); // Error! // Replace undefined const cleanObj = Object.keys(obj) .reduce((acc, key) => { if (obj[key] !== undefined) { acc[key] = obj[key]; } return acc; }, {}); ``` -------------------------------- ### Build Dictionary Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Utility function to save dictionary data in a zip archive. Requires a data object with index and term bank information. Import from './util/saveDict'. ```javascript const saveDict = require('./util/saveDict'); const data = { 'index.json': { title: 'My Dictionary', revision: '1', format: 3 }, 'term_bank_1.json': [['term', 'reading', 'def', [], 0, [], []]], }; saveDict(data, 'My Dictionary.zip'); // Saves to ./dl/ ``` -------------------------------- ### Check Available Yomitan Profiles Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md Diagnose 'Failed to get options for profile' errors by verifying Yomitan profile availability. This script snippet logs the number of available profiles. ```javascript // Run in Yomitan options console to check available profiles const { Application } = await import('./js/application.js'); const app = await Application.main(); console.log('Available profiles:', app.settings.profiles.length); ``` -------------------------------- ### Usage Pattern for writeJson Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/write-json.md Illustrates the typical usage pattern for the `writeJson` function, emphasizing that it must be called with `await` within an `async` function and includes basic error handling for the overall process. ```javascript const writeJson = require('./util/writeJson'); // Must be used with await in an async function async function main() { const data = { /* ... */ }; await writeJson(data, './output.json'); } main().catch(console.error); ``` -------------------------------- ### Rate Limiting with Wait Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/scrape.md Shows how to fetch multiple URLs with rate limiting by introducing a delay between each request using the `wait` function. ```javascript const { getJSON, wait } = require('./util/scrape'); // Fetch multiple URLs with rate limiting async function fetchMultipleWithRateLimit(urls, delayMs = 2000) { const results = []; for (const url of urls) { const data = await getJSON(url); results.push(data); await wait(delayMs); // Wait between requests } return results; } ``` -------------------------------- ### Handle Circular References in JSON Serialization Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md Avoid 'Converting circular structure to JSON' errors by manually cleaning objects before serialization. This example shows how to copy an object without its circular references. ```javascript const obj = { a: 1 }; obj.self = obj; saveDict({ 'file.json': obj }, 'out.zip'); // Error! // Remove circular references const cleanObj = { a: obj.a }; // Copy without circular ref ``` -------------------------------- ### Ensure Output Directory Exists Before Saving Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md Verifies the existence of the output directory and creates it if it doesn't exist before saving a file. Prevents silent write failures. ```javascript const fs = require('fs'); const saveDict = require('./util/saveDict'); const outputDir = './dl/'; if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } saveDict(data, 'Dictionary.zip'); ``` -------------------------------- ### Test Regex Patterns in Browser Console Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/errors.md Use this snippet in the Yomitan options page console to test if your regex patterns correctly match installed dictionary names. It iterates through dictionaries and logs their names along with the test result. ```javascript const dicts = await settingsController.getOptions(); for (const dict of dicts.dictionaries) { console.log(dict.name); console.log(/^pattern$/.test(dict.name)); } ``` -------------------------------- ### Build Dictionary from JMDict Entries Source: https://github.com/marvnc/yomitan-dictionaries/blob/master/_autodocs/quick-reference.md Builds a dictionary by fetching JMDict entries for a list of terms. Includes error handling for terms that cannot be processed and saves the resulting dictionary. ```javascript const saveDict = require('./util/saveDict'); const { getJMDictEntry } = require('./util/getJMDictInfo'); async function buildDictionary(terms) { const termData = []; for (const term of terms) { try { const entry = await getJMDictEntry(term[0], term[1]); termData.push([ term[0], term[1], entry.tags, [], 0, [], [], ]); } catch (error) { console.warn(`Skipping: ${term[0]}`); } } saveDict({ 'index.json': { title: 'My Dictionary', revision: 'v1', format: 3, author: 'Me', }, 'term_bank_1.json': termData, }, 'My Dictionary.zip'); } ```