Skip to content

Commit

Permalink
Add basic synonym antonyms list
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Feb 4, 2024
1 parent 15eb168 commit 23821e8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/util/yomitan/convertEntryToDetailedDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { convertHeadwordsToSC } from './convertHeadwordsToSC.js';
import { convertSenseToLiSC } from './convertSenseToSC.js';
import { createEntryAttribution } from './createEntryAttribution.js';
import { createEntryImageSC } from './createEntryImageSC.js';
import { convertEntryToSynAntsSC } from './convertEntryToSynAntsSC.js';

/**
* Converts a dictionary entry to a detailed definition.
Expand All @@ -15,6 +16,7 @@ function convertEntryToDetailedDefinition(entry) {
const SCArray = [];
// Headword
SCArray.push(convertHeadwordsToSC(entry.headwords));

// Senses with explanation/examples
SCArray.push({
tag: 'div',
Expand All @@ -30,6 +32,11 @@ function convertEntryToDetailedDefinition(entry) {
content: entry.senses.map(convertSenseToLiSC),
},
});

// Synonyms/antonyms
const synAntsSC = convertEntryToSynAntsSC(entry);
SCArray.push(...synAntsSC);

// Image
let imageURLs = [];
if (entry.tags.some((tag) => tag.name === 'img')) {
Expand All @@ -39,6 +46,7 @@ function convertEntryToDetailedDefinition(entry) {
}
imageURLs.push(...validImageURLs);
}

// Attribution
SCArray.push(createEntryAttribution(entry, imageURLs));
return {
Expand Down
60 changes: 60 additions & 0 deletions src/util/yomitan/convertEntryToSynAntsSC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Converts an entry to a ul list of the element's synonyms and antonyms.
* @param {DictionaryEntry} entry
* @returns {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]}
*/
function convertEntryToSynAntsSC(entry) {
let exists = false;
/**
* @type {import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent[]}
*/
const SCArray = [];
/**
* @type {('sim'|'ant')[]}
*/
const tagTypes = ['sim', 'ant'];
for (const type of tagTypes) {
const { SC, exists: typeExists } = convertEntryToSCType(entry, type);
if (typeExists) {
SCArray.push(SC);
}
}

return SCArray;
}

/**
*
* @param {DictionaryEntry} entry
* @param {'sim'|'ant'} type
* @returns {{ SC: import('yomichan-dict-builder/dist/types/yomitan/termbank').StructuredContent, exists: boolean}}
*/
function convertEntryToSCType(entry, type) {
const typeTags = entry.tags.filter((tag) => tag.name === type);
if (typeTags.length === 0) {
return {
SC: [],
exists: false,
};
}
let tagString = typeTags.map((tag) => tag.value).join('・');
return {
SC: {
tag: 'ul',
data: {
wordshk: `${type}-list`,
},
lang: 'yue',
content: {
tag: 'li',
data: {
wordshk: type,
},
content: tagString,
},
},
exists: true,
};
}

export { convertEntryToSynAntsSC };

0 comments on commit 23821e8

Please sign in to comment.