Skip to content

Commit

Permalink
Add interface UserDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jan 14, 2024
1 parent a91a1f9 commit 7c8122f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
68 changes: 48 additions & 20 deletions denops/skkeleton/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JpNum } from "./deps/japanese_numeral.ts";
import { RomanNum } from "./deps/roman.ts";
import { zip } from "./deps/std/collections.ts";
import type { CompletionData, RankData } from "./types.ts";
import { UserDictionary } from "./sources/user_dictionary.ts";
import { UserDictionarySource } from "./sources/user_dictionary.ts";
import { SkkDictionarySource } from "./sources/skk_dictionary.ts";
import { DenoKvSource } from "./sources/deno_kv.ts";
import { SkkServerSource } from "./sources/skk_server.ts";
Expand Down Expand Up @@ -113,8 +113,24 @@ export interface Dictionary {
getCompletionResult(prefix: string, feed: string): Promise<CompletionData>;
}

export type UserDictionaryPath = {
path?: string;
rankPath?: string;
};

export interface UserDictionary extends Dictionary {
getHenkanResult(type: HenkanType, word: string): Promise<string[]>;
getCompletionResult(prefix: string, feed: string): Promise<CompletionData>;
getRanks(prefix: string): RankData;
purgeCandidate(type: HenkanType, word: string, candidate: string): Promise<void>;
registerHenkanResult(type: HenkanType, word: string, candidate: string): Promise<void>;
load({ path, rankPath }: UserDictionaryPath): Promise<void>;
save(): Promise<void>;
}

export interface Source {
getDictionaries(): Promise<Dictionary[]>;
getUserDictionary?(): Promise<UserDictionary>;
}

export class NumberConvertWrapper implements Dictionary {
Expand Down Expand Up @@ -175,16 +191,20 @@ function gatherCandidates(

export class Library {
#dictionaries: Dictionary[];
#userDictionary: UserDictionary;
#userDictionary: UserDictionary | undefined;

constructor(
dictionaries?: Dictionary[],
userDictionary?: UserDictionary,
) {
this.#userDictionary = userDictionary ?? new UserDictionary();
this.#dictionaries = [wrapDictionary(this.#userDictionary)].concat(
dictionaries ?? [],
);
this.#userDictionary = userDictionary ?? undefined;
this.#dictionaries = [];
if (userDictionary) {
this.#dictionaries = [wrapDictionary(userDictionary)];
}
if (dictionaries) {
this.#dictionaries = this.#dictionaries.concat(dictionaries);
}
}

async getHenkanResult(type: HenkanType, word: string): Promise<string[]> {
Expand Down Expand Up @@ -230,6 +250,10 @@ export class Library {
}

getRanks(prefix: string): RankData {
if (!this.#userDictionary) {
return [];
}

return this.#userDictionary.getRanks(prefix);
}

Expand All @@ -238,42 +262,46 @@ export class Library {
word: string,
candidate: string,
) {
if (!this.#userDictionary) {
return;
}

this.#userDictionary.registerHenkanResult(type, word, candidate);
if (config.immediatelyDictionaryRW) {
await this.#userDictionary.save();
}
}

async purgeCandidate(type: HenkanType, word: string, candidate: string) {
if (!this.#userDictionary) {
return;
}

this.#userDictionary.purgeCandidate(type, word, candidate);
if (config.immediatelyDictionaryRW) {
await this.#userDictionary.save();
}
}

async load() {
await this.#userDictionary.load();
if (!this.#userDictionary) {
return;
}

await this.#userDictionary.load({});
}

async save() {
if (!this.#userDictionary) {
return;
}

await this.#userDictionary.save();
}
}

export async function load(): Promise<Library> {
const userDictionary = new UserDictionary();
try {
await userDictionary.load({
path: config.userDictionary,
rankPath: config.completionRankFile,
});
} catch (e) {
if (config.debug) {
console.log("userDictionary loading failed");
console.log(e);
}
// do nothing
}
const userDictionary = await (new UserDictionarySource()).getUserDictionary();

let dictionaries: Dictionary[] = [];
for (const source of config.sources) {
Expand Down
38 changes: 32 additions & 6 deletions denops/skkeleton/sources/user_dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,40 @@ import type { CompletionData, RankData } from "../types.ts";
import {
Dictionary,
HenkanType,
UserDictionary,
UserDictionaryPath,
Source,
okuriAriMarker,
okuriNasiMarker,
} from "../dictionary.ts";
import { wrap } from "../deps/iterator_helpers.ts";
import { assert, is } from "../deps/unknownutil.ts";

export type UserDictionaryPath = {
path?: string;
rankPath?: string;
};
export class UserDictionarySource implements Source {
async getDictionaries(): Promise<Dictionary[]>{
return [await this.getUserDictionary()];
}

async getUserDictionary(): Promise<UserDictionary> {
const userDictionary = new UserDictionaryDictionary();
try {
await userDictionary.load({
path: config.userDictionary,
rankPath: config.completionRankFile,
});
} catch (e) {
if (config.debug) {
console.log("userDictionary loading failed");
console.log(e);
}
// do nothing
}

return userDictionary;
}
}

export class UserDictionary implements Dictionary {
export class UserDictionaryDictionary implements UserDictionary {
#okuriAri: Map<string, string[]>;
#okuriNasi: Map<string, string[]>;
#rank: Map<string, number>;
Expand Down Expand Up @@ -91,7 +113,7 @@ export class UserDictionary implements Dictionary {

registerHenkanResult(type: HenkanType, word: string, candidate: string) {
if (candidate === "") {
return;
return Promise.resolve();
}
const target = type === "okuriari" ? this.#okuriAri : this.#okuriNasi;
const oldCandidate = target.get(word) ?? [];
Expand All @@ -101,6 +123,8 @@ export class UserDictionary implements Dictionary {
);
this.#rank.set(candidate, Date.now());
this.#cachedPrefix = "";

return Promise.resolve();
}

purgeCandidate(type: HenkanType, word: string, candidate: string) {
Expand All @@ -111,6 +135,8 @@ export class UserDictionary implements Dictionary {
} else {
target.delete(word);
}

return Promise.resolve();
}

private async readFile(path: string, rankPath: string) {
Expand Down

0 comments on commit 7c8122f

Please sign in to comment.