Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support more sections in the dictionary adapters. #378

Merged
merged 19 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion source/constants/dictionaries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import type { LearningLanguage } from "logos:constants/languages/learning";

const sections = [
"partOfSpeech",
"definitions",
"translations",
"relations",
"syllables",
"pronunciation",
"rhymes",
"audio",
"expressions",
"examples",
"frequency",
"inflection",
"etymology",
"notes",
] as const;
type DictionarySection = (typeof sections)[number];

type Dictionary = "dexonline" | "dicolink" | "wiktionary" | "wordnik" | "words-api";

const dictionariesByLanguage = Object.freeze({
Expand All @@ -25,4 +43,4 @@ const dictionariesByLanguage = Object.freeze({
} satisfies Record<LearningLanguage, Dictionary[]> as Record<LearningLanguage, Dictionary[]>);

export default Object.freeze({ languages: dictionariesByLanguage });
export type { Dictionary };
export type { Dictionary, DictionarySection };
7 changes: 6 additions & 1 deletion source/constants/emojis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ export default Object.freeze({
word: {
word: "📜",
definitions: "📚",
translations: "🌐",
relations: "🌳",
pronunciation: "🗣️",
expressions: "💐",
etymology: "🌐",
examples: "🏷️",
etymology: "🌱",
notes: "📝",
},
music: {
song: "🎵",
Expand Down
19 changes: 10 additions & 9 deletions source/constants/parts-of-speech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,35 @@ function isUnknownPartOfSpeech(partOfSpeech: PartOfSpeech): partOfSpeech is "unk
return partOfSpeech === "unknown";
}

interface PartOfSpeechDetection {
readonly detected: PartOfSpeech;
readonly original: string;
}
function getPartOfSpeech({
terms,
learningLanguage,
}: { terms: { exact: string; approximate?: string }; learningLanguage: LearningLanguage }): [
detected: PartOfSpeech,
original: string,
] {
}: { terms: { exact: string; approximate?: string }; learningLanguage: LearningLanguage }): PartOfSpeechDetection {
if (isPartOfSpeech(terms.exact)) {
return [terms.exact, terms.exact];
return { detected: terms.exact, original: terms.exact };
}

if (!(learningLanguage in partsOfSpeechByLanguage)) {
return ["unknown", terms.exact];
return { detected: "unknown", original: terms.exact };
}

const partsOfSpeechLocalised = partsOfSpeechByLanguage[
learningLanguage as keyof typeof partsOfSpeechByLanguage
] as Record<string, PartOfSpeech>;

if (terms.exact in partsOfSpeechLocalised) {
return [partsOfSpeechLocalised[terms.exact]!, terms.exact];
return { detected: partsOfSpeechLocalised[terms.exact]!, original: terms.exact };
}

if (terms.approximate !== undefined && terms.approximate in partsOfSpeechLocalised) {
return [partsOfSpeechLocalised[terms.approximate]!, terms.approximate];
return { detected: partsOfSpeechLocalised[terms.approximate]!, original: terms.approximate };
}

return ["unknown", terms.exact];
return { detected: "unknown", original: terms.exact };
}

export { getPartOfSpeech, isUnknownPartOfSpeech };
Expand Down
77 changes: 5 additions & 72 deletions source/library/adapters/dictionaries/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,14 @@
import type { DictionarySection } from "logos:constants/dictionaries.ts";
import type { LearningLanguage } from "logos:constants/languages/learning";
import type { Licence } from "logos:constants/licences";
import type { PartOfSpeech } from "logos:constants/parts-of-speech";
import type { DictionaryEntry } from "logos/adapters/dictionaries/dictionary-entry";
import type { Client } from "logos/client";
import { Logger } from "logos/logger";

type DictionaryProvisions =
/** Provides definitions of a lemma. */
| "definitions"
/** Provides a lemma's etymology. */
| "etymology"
/** Provides relations between a lemma and other words. */
| "relations"
/** Provides words that rhyme with a given lemma. */
| "rhymes";

interface TaggedValue<T> {
tags?: string[];
value: T;
}

interface Expression extends TaggedValue<string> {}

interface Definition extends TaggedValue<string> {
definitions?: Definition[];
expressions?: Expression[];
relations?: Relations;
}

interface Relations {
synonyms?: string[];
antonyms?: string[];
diminutives?: string[];
augmentatives?: string[];
}

interface Rhymes extends TaggedValue<string> {}

interface Etymology extends TaggedValue<string | undefined> {}

type InflectionTable = { title: string; fields: Discord.CamelizedDiscordEmbedField[] }[];

interface DictionaryEntry {
/** The topic word of an entry. */
lemma: string;

/** The part of speech of the lemma. */
partOfSpeech: [detected: PartOfSpeech, text: string];

/** The definitions for the lemma in its native language. */
nativeDefinitions?: Definition[];

/** The definitions for the lemma. */
definitions?: Definition[];

/** Relations between the lemma and other words. */
relations?: Relations;

/** Rhythmic composition of the lemma. */
rhymes?: Rhymes;

/** The expressions for the lemma. */
expressions?: Expression[];

/** The etymologies for the lemma. */
etymologies?: Etymology[];

/** The inflection of the lemma. */
inflectionTable?: InflectionTable;

sources: [link: string, licence: Licence][];
}

abstract class DictionaryAdapter<DataType = unknown> {
readonly log: Logger;
readonly client: Client;
readonly identifier: string;
readonly provides: DictionaryProvisions[];
readonly provides: DictionarySection[];
readonly supports: LearningLanguage[];
readonly isFallback: boolean;

Expand All @@ -86,7 +19,7 @@ abstract class DictionaryAdapter<DataType = unknown> {
provides,
supports,
isFallback = false,
}: { identifier: string; provides: DictionaryProvisions[]; supports: LearningLanguage[]; isFallback?: boolean },
}: { identifier: string; provides: DictionarySection[]; supports: LearningLanguage[]; isFallback?: boolean },
) {
this.log = Logger.create({ identifier, isDebug: client.environment.isDebug });
this.client = client;
Expand Down Expand Up @@ -137,4 +70,4 @@ abstract class DictionaryAdapter<DataType = unknown> {
}

export { DictionaryAdapter };
export type { Definition, DictionaryEntry, Relations, Rhymes, Expression, Etymology, DictionaryProvisions };
export type { DictionaryEntry };
Loading