Skip to content

Commit

Permalink
Support updatable dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jul 24, 2024
1 parent 3b3a326 commit ed3ece7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/daily-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ jobs:
- name: Download Latest CSVs
run: npm run download

- name: Build Dictionaries
run: |
npm run buildTermDict
npm run buildHonziDict
- name: Get Current Date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"

- name: Build Dictionaries
run: |
npm run buildTermDict ${{ steps.date.outputs.date }}
npm run buildHonziDict ${{ steps.date.outputs.date }}
- name: Create and Publish Release
uses: softprops/action-gh-release@v2
with:
Expand Down
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,6 @@ export {
};
export const dataFolder = './csvs';
export const exportDirectory = './dist';

export const TERM_INDEX_FILE = 'term_index.json';
export const HONZI_INDEX_FILE = 'honzi_index.json';
25 changes: 21 additions & 4 deletions src/convertToHonziDictionary.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import fs from 'fs/promises';
import { Dictionary, DictionaryIndex, KanjiEntry } from 'yomichan-dict-builder';
import { getVersion } from './util/getVersion.js';
import { dataFolder, exportDirectory } from './constants.js';
import { dataFolder, exportDirectory, HONZI_INDEX_FILE } from './constants.js';
import { readAndParseCSVs } from './util/readAndParseCSVs.js';
import { isSingleCJKHanzi } from 'is-cjk-hanzi';

(async () => {
const tagName = process.argv[2] ?? 'latest';

const { dictionaryEntries, dateString } = await readAndParseCSVs(dataFolder);

/** @type {`${string}.zip`} */
const honziDictionaryFilename = `Words.hk Honzi ${dateString}.zip`;
const dictionary = new Dictionary({
fileName: `Words.hk Honzi ${dateString}.zip`,
fileName: honziDictionaryFilename,
});

const dictionaryIndex = new DictionaryIndex()
Expand All @@ -23,12 +28,24 @@ import { isSingleCJKHanzi } from 'is-cjk-hanzi';
Converted using https://github.com/MarvNC/yomichan-dict-builder`
)
.setTitle(`Words.hk 粵典 漢字 [${dateString}]`)
.setRevision(`${getVersion()}`);
.setRevision(`${getVersion()}`)
.setIsUpdatable(true)
.setIndexUrl(
`https://github.com/MarvNC/wordshk-yomitan/releases/download/latest/${HONZI_INDEX_FILE}`
)
.setDownloadUrl(
`https://github.com/MarvNC/wordshk-yomitan/releases/download/${tagName}/${honziDictionaryFilename}`
);
await dictionary.setIndex(dictionaryIndex.build());

// save index file to exportDirectory
await fs.writeFile(
`${exportDirectory}/${HONZI_INDEX_FILE}`,
JSON.stringify(dictionaryIndex.build())
);

for (const entry of dictionaryEntries) {
addHonziEntry(dictionary, entry);
// const kanjiEntry = new KanjiEntry(entry.)
}
console.log(`Finished adding entries to dictionary.`);

Expand Down
23 changes: 21 additions & 2 deletions src/convertToTermDictionary.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs/promises';
import { Dictionary, DictionaryIndex } from 'yomichan-dict-builder';

import { convertEntryToYomitanTerms } from './util/yomitan/convertEntryToYomitanTerms.js';
Expand All @@ -10,13 +11,16 @@ import {
IMAGE_FOLDER,
COMPRESSED_IMAGES_FOLDER,
IMAGE_RESIZE_WIDTH,
TERM_INDEX_FILE,
} from './constants.js';
import { compressImages } from './util/imageHandler/compressImages.js';
import { dataFolder, exportDirectory } from './constants.js';
import { getVersion } from './util/getVersion.js';
import { readAndParseCSVs } from './util/readAndParseCSVs.js';

(async () => {
const tagName = process.argv[2] ?? 'latest';

const { dictionaryEntries, dateString } = await readAndParseCSVs(dataFolder);

const uniqueLabels = findLabelValues(dictionaryEntries);
Expand All @@ -31,8 +35,10 @@ import { readAndParseCSVs } from './util/readAndParseCSVs.js';
IMAGE_RESIZE_WIDTH
);

/** @type {`${string}.zip`} */
const termDictionaryFileName = `Words.hk.${dateString}.zip`;
const dictionary = new Dictionary({
fileName: `Words.hk ${dateString}.zip`,
fileName: termDictionaryFileName,
});

const dictionaryIndex = new DictionaryIndex()
Expand All @@ -48,9 +54,22 @@ import { readAndParseCSVs } from './util/readAndParseCSVs.js';
Converted using https://github.com/MarvNC/yomichan-dict-builder`
)
.setTitle(`Words.hk 粵典 [${dateString}]`)
.setRevision(`${getVersion()}`);
.setRevision(`${getVersion()}`)
.setIsUpdatable(true)
.setIndexUrl(
`https://github.com/MarvNC/wordshk-yomitan/releases/download/latest/${TERM_INDEX_FILE}`
)
.setDownloadUrl(
`https://github.com/MarvNC/wordshk-yomitan/releases/download/${tagName}/${termDictionaryFileName}`
);
await dictionary.setIndex(dictionaryIndex.build());

// save index file to exportDirectory
await fs.writeFile(
`${exportDirectory}/${TERM_INDEX_FILE}`,
JSON.stringify(dictionaryIndex.build())
);

for (const entry of dictionaryEntries) {
const terms = convertEntryToYomitanTerms(entry);
for (const term of terms) {
Expand Down

0 comments on commit ed3ece7

Please sign in to comment.