UNPKG

wn-ts-node

Version:
774 lines (726 loc) 25.6 kB
import { Kysely } from 'kysely'; import { z } from 'zod'; /** * Shared database configuration interfaces for wn-ts ecosystem * * This provides common configuration types that both Node.js and Web implementations * can use, eliminating duplication across packages. */ /** * Base database configuration interface */ declare interface BaseDatabaseConfig { /** * Whether the database is read-only */ readonly?: boolean; /** * Whether to force recreation of the database */ forceRecreate?: boolean; /** * Whether to enable verbose logging */ verbose?: boolean; } declare abstract class BaseKyselyQueryService { protected db: Kysely<Database>; protected defaultStrategy: QueryStrategy; protected defaultOptions: QueryOptions; constructor(db: Kysely<Database>, options?: { strategy?: QueryStrategy; }); private updateDefaultOptions; private getOptionsForStrategy; getLexicons(options?: { ids?: string[]; id?: string; language?: string; version?: string; }): Promise<Lexicon[]>; getLexiconById(id: string): Promise<Lexicon | undefined>; getWords(options?: WordQuery & QueryOptions): Promise<Word[]>; getWordById(id: string): Promise<Word | undefined>; getWordsByFormFast(form: string, options?: { pos?: PartOfSpeech; lexicon?: string; maxResults?: number; }): Promise<Word[]>; getWordsByFormFuzzyFast(form: string, options?: { pos?: PartOfSpeech; lexicon?: string; maxResults?: number; }): Promise<Word[]>; getSynsets(options?: SynsetQuery & QueryOptions): Promise<Synset[]>; getSynsetsV1(options?: SynsetQuery): Promise<Synset[]>; getSynsetsV2(options?: SynsetQuery): Promise<Synset[]>; getSynsetsV3(options?: SynsetQuery): Promise<Synset[]>; getSynsetsV4(options?: SynsetQuery): Promise<Synset[]>; private queryCache; private cacheHits; private cacheMisses; getSynsetsV5(options?: SynsetQuery): Promise<Synset[]>; getSynsetsV6(options?: SynsetQuery): Promise<Synset[]>; getSynsetsFast(options?: SynsetQuery): Promise<Synset[]>; getSynsetById(id: string, options?: QueryOptions): Promise<Synset | undefined>; getSynsetsByFormFast(form: string, options?: { pos?: PartOfSpeech; lexicon?: string; maxResults?: number; } & QueryOptions): Promise<Synset[]>; getSenses(options?: SenseQuery & QueryOptions): Promise<Sense[]>; getSensesV1(options?: SenseQuery & QueryOptions): Promise<Sense[]>; getSensesV5(options?: SenseQuery & QueryOptions): Promise<Sense[]>; getSensesV6(options?: SenseQuery & QueryOptions): Promise<Sense[]>; getSenseById(id: string): Promise<Sense | undefined>; getDefinitionsBySynsetId(synsetId: string): Promise<any[]>; getIliById(id: string): Promise<ILI | undefined>; getIlis(options?: { status?: string; }): Promise<ILI[]>; getStatistics(): Promise<{ totalWords: number; totalSynsets: number; totalSenses: number; totalILIs: number; totalLexicons: number; }>; batchInsert<T extends keyof Database>(tableName: T, data: Database[T][]): Promise<void>; abstract createTables(): Promise<void>; protected transformLexiconRecord(record: any): Lexicon; protected transformWordRecord(record: Database['words']): Promise<Word>; protected transformSynsetRecordV1(record: any): Promise<Synset>; protected transformSynsetRecordV2(record: any): Promise<Synset>; protected transformSynsetRecordFast(record: any): Promise<Synset>; protected transformSynsetRecord(record: any, options?: QueryOptions): Promise<Synset>; protected transformSenseRecord(record: any): Sense; protected transformIliRecord(record: any): ILI; getWordsByLexicon(lexiconId: string): Promise<any[]>; getSensesByWordId(wordId: string): Promise<any[]>; getSynsetsByLexicon(lexiconId: string): Promise<any[]>; getExamplesBySynsetId(synsetId: string): Promise<any[]>; getSensesBySynsetId(synsetId: string): Promise<any[]>; getWordsByIds(wordIds: string[]): Promise<Word[]>; getWordsBySynsetAndLanguage(synsetId: string, language?: string): Promise<Word[]>; getWordsByIliAndLanguage(ili: string, language?: string): Promise<Word[]>; getWordsByIliAndLexiconPrefix(ili: string, lexiconPrefix: string): Promise<Word[]>; getRelationsBySynsetId(synsetId: string): Promise<any[]>; /** * Get forms for a specific word */ getFormsByWordId(wordId: string): Promise<any[]>; } /** * Shared database types for wn-ts ecosystem * * This file defines the common database schema types that both Node.js and Web * implementations can use, eliminating duplication across packages. */ declare interface Database { lexicons: LexiconTable; words: WordTable; synsets: SynsetTable; senses: SenseTable; definitions: DefinitionTable; relations: RelationTable; examples: ExampleTable; ilis: IliTable; forms: FormTable; } declare type Definition = z.infer<typeof DefinitionSchema>; declare const DefinitionSchema: z.ZodObject<{ id: z.ZodString; language: z.ZodString; text: z.ZodString; source: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare interface DefinitionTable { id: string; synset_id: string; language: string; text: string; source?: string; } declare interface ExampleTable { id: string; synset_id?: string; sense_id?: string; language: string; text: string; source?: string; } declare interface FormTable { id: string; word_id: string; written_form: string; script?: string; tag?: string; } declare type ILI = z.infer<typeof ILISchema>; declare const ILISchema: z.ZodObject<{ id: z.ZodString; definition: z.ZodOptional<z.ZodString>; status: z.ZodEnum<{ standard: "standard"; proposed: "proposed"; deprecated: "deprecated"; }>; supersededBy: z.ZodOptional<z.ZodString>; note: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare interface IliTable { id: string; definition?: string; status: string; superseded_by?: string; note?: string; meta?: string; } declare class KyselyQueryService extends BaseKyselyQueryService { constructor(db: Kysely<Database>, options?: { strategy?: QueryStrategy; }); createTables(): Promise<void>; deleteLexicon(lexiconId: string): Promise<void>; deleteWordsByLexicon(lexiconId: string): Promise<void>; deleteSynsetsByLexicon(lexiconId: string): Promise<void>; } declare class KyselyWordnet extends LocalBaseWordnet { private nodeDatabase; private queryService; private strategy; constructor(lexicon?: string | string[], options?: Partial<NodeWordnetConfig>); initialize(): Promise<void>; private configureSQLite; getWord(id: string): Promise<Word | undefined>; getSynset(id: string): Promise<Synset | undefined>; synset(id: string): Promise<Synset | undefined>; getSense(id: string): Promise<Sense | undefined>; getIli(id: string): Promise<ILI | undefined>; words(query?: WordQuery): Promise<Word[]>; synsets(query?: SynsetQuery): Promise<Synset[]>; senses(query?: SenseQuery): Promise<Sense[]>; lexicons(): Promise<Lexicon[]>; getStatistics(): Promise<{ totalWords: number; totalSynsets: number; totalSenses: number; totalILIs: number; totalLexicons: number; }>; ilis(status?: string): Promise<ILI[]>; synsetsByILI(iliId: string): Promise<Synset[]>; getProjects(): Promise<Project[]>; searchWords(query: any): Promise<Word[]>; searchSynsets(query: any): Promise<Synset[]>; wordsByForm(form: string, options?: any): Promise<Word[]>; synsetsByForm(form: string, options?: any): Promise<Synset[]>; getWordForms(wordId: string): Promise<string[]>; getWordLemma(wordId: string): Promise<string>; morphy(form: string, pos?: PartOfSpeech): Promise<Record<PartOfSpeech, Set<string>>>; getDerivedWords(_wordId: string): Promise<Word[]>; getHypernyms(synsetId: string): Promise<Synset[]>; getHyponyms(synsetId: string): Promise<Synset[]>; getRelatedSynsets(synsetId: string, relationType: string): Promise<Synset[]>; getRelatedSenses(_senseId: string, _relationType: string): Promise<Sense[]>; getShortestPath(_synsetId1: string, _synsetId2: string): Promise<Synset[]>; getSynsetDepth(_synsetId: string): Promise<number>; translateWord(_wordId: string, _targetLang: string): Promise<Record<string, Word[]>>; translateSynset(synsetId: string, _targetLang: string): Promise<Synset[]>; translateSense(_senseId: string, _targetLang: string): Promise<Sense[]>; getCrossLingualSynsets(_iliId: string, _targetLangs?: string[]): Promise<Record<string, Synset[]>>; getDefinitions(synsetId: string): Promise<Definition[]>; getExamples(synsetId: string): Promise<string[]>; getSenseExamples(_senseId: string): Promise<string[]>; getSynsetWords(synsetId: string): Promise<Word[]>; getSynsetLemmas(synsetId: string): Promise<string[]>; getSynsetSenses(synsetId: string): Promise<Sense[]>; hasLexicon(lexiconId: string): Promise<boolean>; getSupportedLanguages(): Promise<string[]>; getLexiconDependencies(_lexiconId: string): Promise<string[]>; getRawDatabase(): Promise<Kysely<Database>>; executeRawQuery<T = any>(_sqlString: string, _params?: any[]): Promise<T[]>; executeRawTransaction<T>(callback: (db: Kysely<Database>) => Promise<T>): Promise<T>; batchInsertWords(words: any[]): Promise<void>; batchInsertSynsets(synsets: any[]): Promise<void>; batchInsertSenses(senses: any[]): Promise<void>; batchInsertForms(forms: any[]): Promise<void>; batchInsertDefinitions(definitions: any[]): Promise<void>; batchInsertExamples(examples: any[]): Promise<void>; batchInsertRelations(relations: any[]): Promise<void>; batchInsertILIs(ilis: any[]): Promise<void>; batchInsertLexicons(lexicons: any[]): Promise<void>; close(): Promise<void>; /** * Get the query service for direct database operations */ getQueryService(): KyselyQueryService; /** * Get words by ILI and language using the query service */ getWordsByIliAndLanguage(ili: string, language?: string): Promise<Word[]>; } declare type Lexicon = z.infer<typeof LexiconSchema>; declare const LexiconSchema: z.ZodObject<{ id: z.ZodString; label: z.ZodString; language: z.ZodString; email: z.ZodOptional<z.ZodString>; license: z.ZodOptional<z.ZodString>; version: z.ZodOptional<z.ZodString>; url: z.ZodOptional<z.ZodString>; citation: z.ZodOptional<z.ZodString>; logo: z.ZodOptional<z.ZodString>; requires: z.ZodOptional<z.ZodArray<z.ZodString>>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; }, z.core.$strip>; declare interface LexiconTable { id: string; label: string; language: string; email?: string; license?: string; version?: string; url?: string; citation?: string; logo?: string; metadata?: string; } declare abstract class LocalBaseWordnet { protected database: NodeKyselyDatabase; protected initialized: boolean; protected lexicon: string[]; protected expand: string[]; protected normalizer?: (form: string) => string; constructor(lexicon?: string | string[], options?: any); abstract initialize(): Promise<void>; protected getDb(): Kysely<Database>; abstract getWord(id: string): Promise<Word | undefined>; abstract getSynset(id: string): Promise<Synset | undefined>; abstract getSense(id: string): Promise<Sense | undefined>; abstract getIli(id: string): Promise<ILI | undefined>; protected getSynsetOrUndefined(id: string): Promise<Synset | undefined>; abstract words(query?: any): Promise<Word[]>; abstract synsets(query?: any): Promise<Synset[]>; abstract senses(query?: any): Promise<Sense[]>; abstract lexicons(): Promise<Lexicon[]>; normalizeForm(form: string): Promise<string>; } /** * Node.js specific database configuration */ declare interface NodeDatabaseConfig extends BaseDatabaseConfig { /** * Database file path */ filename: string; /** * Whether the file must exist */ fileMustExist?: boolean; /** * Database timeout in milliseconds */ timeout?: number; } declare class NodeKyselyDatabase implements NodeKyselyDatabaseInterface { private db; private sqliteDb; private config; constructor(config: NodeDatabaseConfig); initialize(): Promise<void>; getDatabase(): Kysely<Database>; close(): Promise<void>; /** * Get the underlying better-sqlite3 database instance * This is useful for operations that need direct SQLite access */ getSqliteDatabase(): any; /** * Execute a raw SQL query (useful for complex operations) */ executeRaw(sql: string, params?: any[]): Promise<any[]>; /** * Execute a raw SQL query that returns a single row */ executeRawSingle(sql: string, params?: any[]): Promise<any>; /** * Begin a transaction */ beginTransaction(): Promise<void>; /** * Commit a transaction */ commitTransaction(): Promise<void>; /** * Rollback a transaction */ rollbackTransaction(): Promise<void>; /** * Check if the database is in a transaction */ isInTransaction(): boolean; /** * Get database statistics */ getDatabaseStats(): Promise<{ pageCount: number; pageSize: number; pageCountBytes: number; freelistCount: number; freelistCountBytes: number; cacheSize: number; cacheSizeBytes: number; cacheUsed: number; cacheUsedBytes: number; }>; /** * Optimize the database */ optimize(): Promise<void>; } declare interface NodeKyselyDatabaseInterface { getDatabase(): Kysely<Database>; initialize(): Promise<void>; close(): Promise<void>; } declare interface NodeWordnetConfig extends NodeDatabaseConfig { journalMode?: 'DELETE' | 'WAL' | 'MEMORY' | 'OFF'; synchronous?: 'OFF' | 'NORMAL' | 'FULL' | 'EXTRA'; cacheSize?: number; tempStore?: 'DEFAULT' | 'FILE' | 'MEMORY'; mmapSize?: number; foreignKeys?: boolean; recursiveTriggers?: boolean; forceRecreate?: boolean; strategy?: QueryStrategy; } /** * Node.js implementation of WordNetCore interface * Bridges the new kernel architecture with existing KyselyWordnet */ export declare class NodeWordNetCore implements WordNetCore { private kyselyWordnet; constructor(lexicon?: string | string[], options?: Partial<NodeWordnetConfig>); query(_sql: string, _params?: unknown[]): Promise<unknown[]>; words(query?: WordQuery): Promise<Word[]>; word(wordId: string): Promise<Word>; synsets(query?: SynsetQuery): Promise<Synset[]>; synset(synsetId: string): Promise<Synset>; senses(query?: SenseQuery): Promise<Sense[]>; sense(senseId: string): Promise<Sense>; ili(iliId: string): Promise<ILI>; ilis(status?: string): Promise<ILI[]>; lexicons(): Promise<any[]>; synsetsByILI(iliId: string): Promise<Synset[]>; getWord(form: string): Promise<Word[]>; getSynset(id: string): Promise<any | null>; getSenses(wordId: string): Promise<Sense[]>; getDefinitions(synsetId: string): Promise<any[]>; getRelations(synsetId: string, type?: string): Promise<any[]>; initialize(): Promise<void>; close(): Promise<void>; getKyselyWordnet(): KyselyWordnet; } declare type PartOfSpeech = 'n' | 'v' | 'a' | 'r' | 's' | 'c' | 'p' | 'i' | 'x' | 'u'; declare type Project = z.infer<typeof ProjectSchema>; declare const ProjectSchema: z.ZodObject<{ id: z.ZodString; label: z.ZodString; description: z.ZodOptional<z.ZodString>; url: z.ZodOptional<z.ZodString>; license: z.ZodOptional<z.ZodString>; citation: z.ZodOptional<z.ZodString>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>; }, z.core.$strip>; declare interface QueryOptions { strategy?: QueryStrategy; includeDefinitions?: boolean; includeExamples?: boolean; includeRelations?: boolean; includeSenses?: boolean; } /** * Query optimization strategies for different performance requirements * Strategy names are flexible and can be query-specific (e.g., 'v1', 'v2', 'optimized', 'fast', etc.) */ declare type QueryStrategy = string; declare type Relation = z.infer<typeof RelationSchema>; declare const RelationSchema: z.ZodObject<{ id: z.ZodString; type: z.ZodString; target: z.ZodString; source: z.ZodOptional<z.ZodString>; dcType: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare interface RelationTable { id: string; source_id: string; target_id: string; type: string; source?: string; } declare type Sense = z.infer<typeof SenseSchema>; declare type SenseQuery = z.infer<typeof SenseQuerySchema>; declare const SenseQuerySchema: z.ZodObject<{ wordIdOrForm: z.ZodOptional<z.ZodString>; pos: z.ZodOptional<z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>>; lexicon: z.ZodOptional<z.ZodString>; strategy: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare const SenseSchema: z.ZodObject<{ id: z.ZodString; wordId: z.ZodString; synsetId: z.ZodString; examples: z.ZodArray<z.ZodObject<{ id: z.ZodString; language: z.ZodString; text: z.ZodString; source: z.ZodOptional<z.ZodString>; }, z.core.$strip>>; counts: z.ZodArray<z.ZodObject<{ id: z.ZodString; value: z.ZodNumber; writtenForm: z.ZodString; pos: z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>; }, z.core.$strip>>; tags: z.ZodArray<z.ZodObject<{ id: z.ZodString; category: z.ZodString; value: z.ZodString; }, z.core.$strip>>; relations: z.ZodOptional<z.ZodArray<z.ZodObject<{ id: z.ZodString; type: z.ZodString; target: z.ZodString; source: z.ZodOptional<z.ZodString>; dcType: z.ZodOptional<z.ZodString>; }, z.core.$strip>>>; source: z.ZodOptional<z.ZodString>; sensekey: z.ZodOptional<z.ZodString>; adjposition: z.ZodOptional<z.ZodString>; subcategory: z.ZodOptional<z.ZodString>; domain: z.ZodOptional<z.ZodString>; register: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare interface SenseTable { id: string; word_id: string; synset_id: string; source?: string; sensekey?: string; adjposition?: string; subcategory?: string; domain?: string; register?: string; } declare type Synset = z.infer<typeof SynsetSchema>; declare type SynsetQuery = z.infer<typeof SynsetQuerySchema>; declare const SynsetQuerySchema: z.ZodObject<{ form: z.ZodOptional<z.ZodString>; pos: z.ZodOptional<z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>>; ili: z.ZodOptional<z.ZodString>; lexicon: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>; language: z.ZodOptional<z.ZodString>; searchAllForms: z.ZodOptional<z.ZodBoolean>; fuzzy: z.ZodOptional<z.ZodBoolean>; maxResults: z.ZodOptional<z.ZodNumber>; includeDefinitions: z.ZodOptional<z.ZodBoolean>; includeExamples: z.ZodOptional<z.ZodBoolean>; includeRelations: z.ZodOptional<z.ZodBoolean>; strategy: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare const SynsetSchema: z.ZodObject<{ id: z.ZodString; ili: z.ZodOptional<z.ZodString>; pos: z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>; definitions: z.ZodArray<z.ZodObject<{ id: z.ZodString; language: z.ZodString; text: z.ZodString; source: z.ZodOptional<z.ZodString>; }, z.core.$strip>>; examples: z.ZodArray<z.ZodObject<{ id: z.ZodString; language: z.ZodString; text: z.ZodString; source: z.ZodOptional<z.ZodString>; }, z.core.$strip>>; relations: z.ZodArray<z.ZodObject<{ id: z.ZodString; type: z.ZodString; target: z.ZodString; source: z.ZodOptional<z.ZodString>; dcType: z.ZodOptional<z.ZodString>; }, z.core.$strip>>; iliDefinitions: z.ZodOptional<z.ZodArray<z.ZodObject<{ id: z.ZodString; language: z.ZodString; text: z.ZodString; source: z.ZodOptional<z.ZodString>; }, z.core.$strip>>>; language: z.ZodString; lexicon: z.ZodString; memberIds: z.ZodArray<z.ZodString>; senseIds: z.ZodArray<z.ZodString>; }, z.core.$strip>; declare interface SynsetTable { id: string; ili?: string; pos: string; language: string; lexicon: string; } declare type Word = z.infer<typeof WordSchema>; declare interface WordNetCore { query(sql: string, params?: unknown[]): Promise<unknown[]>; words(query?: WordQuery): Promise<Word[]>; word(wordId: string): Promise<Word>; synsets(query?: SynsetQuery): Promise<Synset[]>; synset(synsetId: string): Promise<Synset>; senses(query?: SenseQuery): Promise<Sense[]>; sense(senseId: string): Promise<Sense>; ili(iliId: string): Promise<ILI>; ilis(status?: string): Promise<ILI[]>; synsetsByILI(iliId: string): Promise<Synset[]>; lexicons(): Promise<Lexicon[]>; getWord(form: string): Promise<Word[]>; getSynset(id: string): Promise<Synset | null>; getSenses(wordId: string): Promise<Sense[]>; getDefinitions(synsetId: string): Promise<Definition[]>; getRelations(synsetId: string, type?: string): Promise<Relation[]>; } declare type WordQuery = z.infer<typeof WordQuerySchema>; declare const WordQuerySchema: z.ZodObject<{ form: z.ZodOptional<z.ZodString>; pos: z.ZodOptional<z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>>; lexicon: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>; language: z.ZodOptional<z.ZodString>; searchAllForms: z.ZodOptional<z.ZodBoolean>; fuzzy: z.ZodOptional<z.ZodBoolean>; maxResults: z.ZodOptional<z.ZodNumber>; includeInflected: z.ZodOptional<z.ZodBoolean>; strategy: z.ZodOptional<z.ZodString>; }, z.core.$strip>; declare const WordSchema: z.ZodObject<{ id: z.ZodString; lemma: z.ZodString; pos: z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>; forms: z.ZodArray<z.ZodObject<{ id: z.ZodString; writtenForm: z.ZodString; script: z.ZodOptional<z.ZodString>; tag: z.ZodOptional<z.ZodString>; }, z.core.$strip>>; pronunciations: z.ZodArray<z.ZodObject<{ id: z.ZodString; value: z.ZodString; variety: z.ZodOptional<z.ZodString>; notation: z.ZodOptional<z.ZodString>; geographic: z.ZodOptional<z.ZodString>; }, z.core.$strip>>; tags: z.ZodArray<z.ZodObject<{ id: z.ZodString; category: z.ZodString; value: z.ZodString; }, z.core.$strip>>; counts: z.ZodArray<z.ZodObject<{ id: z.ZodString; value: z.ZodNumber; writtenForm: z.ZodString; pos: z.ZodEnum<{ n: "n"; v: "v"; a: "a"; r: "r"; s: "s"; c: "c"; p: "p"; i: "i"; x: "x"; u: "u"; }>; }, z.core.$strip>>; syntacticBehaviours: z.ZodOptional<z.ZodArray<z.ZodObject<{ id: z.ZodString; subcategorizationFrame: z.ZodString; source: z.ZodOptional<z.ZodString>; senseIds: z.ZodArray<z.ZodString>; }, z.core.$strip>>>; language: z.ZodString; lexicon: z.ZodString; }, z.core.$strip>; declare interface WordTable { id: string; lemma: string; pos: string; language: string; lexicon: string; } export { }