-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup basic component relations in KanjisenseFigure
- Loading branch information
1 parent
d284488
commit 0c332e0
Showing
8 changed files
with
374 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,73 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { KanjisenseFigureRelation, PrismaClient } from "@prisma/client"; | ||
|
||
const RADICAL_ENTRY_REGEX = /radical \(no\.|radical number/; | ||
export async function getFigureMeaningsText( | ||
prisma: PrismaClient, | ||
figureId: string, | ||
figure: KanjisenseFigureRelation, | ||
componentsDictionaryEntry: ComponentMeaning | null, | ||
) { | ||
const unihanDefinition = prisma.unihan15.findUnique({ | ||
const figureId = figure.id; | ||
const unihanDefinitionLookup = prisma.unihan15.findUnique({ | ||
where: { id: figureId }, | ||
select: { kDefinition: true }, | ||
}); | ||
const kanjidicEnglish = prisma.kanjidicEntry.findUnique({ | ||
const kanjidicEnglishLookup = prisma.kanjidicEntry.findUnique({ | ||
where: { id: figureId }, | ||
select: { definitions: true }, | ||
}); | ||
|
||
return { | ||
unihanDefinitionText: | ||
(await unihanDefinition)?.kDefinition?.join("; ") || null, | ||
kanjidicEnglish: | ||
(await kanjidicEnglish)?.definitions?.filter( | ||
(e) => !RADICAL_ENTRY_REGEX.test(e), | ||
) || [], | ||
const mnemonicKeywords = componentsDictionaryEntry; | ||
const historicalKeyword = | ||
mnemonicKeywords?.historical && mnemonicKeywords.historical !== "(various)" | ||
? mnemonicKeywords.historical | ||
: null; | ||
let mnemonicSource = ""; | ||
if (mnemonicKeywords?.reference) | ||
mnemonicSource = ` {{cf. ${mnemonicKeywords.reference}}}`; | ||
else if (mnemonicKeywords?.standin) | ||
mnemonicSource = ` {{via ${mnemonicKeywords.standin}}}`; | ||
|
||
const unihanDefinitionText = | ||
(await unihanDefinitionLookup)?.kDefinition?.join("; ") || null; | ||
const kanjidicEnglish = | ||
(await kanjidicEnglishLookup)?.definitions?.filter( | ||
(e) => !RADICAL_ENTRY_REGEX.test(e), | ||
) || []; | ||
const mnemonicKeyword = mnemonicKeywords?.mnemonic | ||
? [mnemonicKeywords.mnemonic, mnemonicSource].join("") | ||
: null; | ||
const historicalKeywordOrDefinition = | ||
(historicalKeyword || | ||
kanjidicEnglish?.[0] || | ||
unihanDefinitionText?.split("; ")?.[0]) ?? | ||
null; | ||
|
||
const meaning = { | ||
unihanDefinitionText, | ||
kanjidicEnglish, | ||
keyword: historicalKeywordOrDefinition || mnemonicKeyword, | ||
mnemonicKeyword: !historicalKeywordOrDefinition ? mnemonicKeyword : null, | ||
}; | ||
|
||
if ( | ||
!meaning.unihanDefinitionText && | ||
!meaning.kanjidicEnglish.length && | ||
!meaning.keyword && | ||
!meaning.mnemonicKeyword | ||
) | ||
return null; | ||
return meaning; | ||
} | ||
|
||
export interface ComponentMeaning { | ||
/** historical meaning */ | ||
historical?: string; | ||
/** mnemonic keyword, if historical meaning is absent or different */ | ||
mnemonic?: string; | ||
/** for this component's mnemonic keyword, it borrows the meaning of a common kanji containing it. */ | ||
standin?: string; | ||
/** this component derives its mnemonic keyword from a common kanji using it. */ | ||
reference?: string; | ||
/** for grouping components by meaning */ | ||
tag?: string | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.