Skip to content

Commit

Permalink
Add instant statistics via s2
Browse files Browse the repository at this point in the history
  • Loading branch information
tansongchen committed Jun 30, 2024
1 parent 27fe368 commit f4de326
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 34 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chai",
"private": true,
"version": "0.1.11",
"version": "0.1.12",
"type": "module",
"scripts": {
"start": "vite --mode CF",
Expand Down Expand Up @@ -43,6 +43,7 @@
"jotai-optics": "^0.4.0",
"js-md5": "^0.8.3",
"js-yaml": "4.1.0",
"libchai": "^0.1.12",
"lodash-es": "^4.17.21",
"lz-string": "^1.5.0",
"mathjs": "^13.0.1",
Expand Down
4 changes: 3 additions & 1 deletion src/atoms/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
keyboardAtom,
mappingAtom,
meaningfulObjectiveAtom,
priorityShortCodesAtom,
repertoireAtom,
} from ".";
import { assetsAtom, customElementsAtom } from "./assets";
Expand Down Expand Up @@ -78,7 +79,8 @@ export const assemblyResultAtom = atom(async (get) => {
const dictionary = await get(dictionaryAtom);
const analysisResult = await get(analysisResultAtom);
const customElements = get(customElementsAtom);
const config = { algebra, encoder, keyboard };
const priority = get(priorityShortCodesAtom);
const config = { algebra, encoder, keyboard, priority };
return await thread.spawn<AssemblyResult>("assembly", [
repertoire,
config,
Expand Down
25 changes: 7 additions & 18 deletions src/atoms/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Config, EncodeResult } from "~/lib";
import type { Config } from "~/lib";
import { isValidCJKChar, stringifySequence } from "~/lib";
import useTitle from "ahooks/es/useTitle";
import init, { validate } from "libchai";
Expand All @@ -12,36 +12,25 @@ import type { WorkerOutput } from "~/worker";
import { atomEffect } from "jotai-effect";
import { configAtom } from "./config";
import { assetsAtom } from "./assets";
import { assemblyResultAtom, encodeResultAtom } from "./cache";
import { atom } from "jotai";
import { meaningfulObjectiveAtom } from "./encoder";
import { assemblyResultAtom } from "./cache";

export const syncConfig = atomEffect((get, set) => {
export const syncConfig = atomEffect((get) => {
const value = get(configAtom);
thread
.spawn("sync", ["config", value])
.then(() => set(dummyAtom, (x) => x + 1));
thread.spawn("sync", ["config", value]);
});

export const syncAssets = atomEffect((get, set) => {
export const syncAssets = atomEffect((get) => {
get(assetsAtom).then(async (value) => {
await thread.spawn("sync", ["assets", value]);
set(dummyAtom, (x) => x + 1);
});
});

export const syncInfo = atomEffect((get, set) => {
export const syncInfo = atomEffect((get) => {
get(assemblyResultAtom).then(async (value) => {
await thread.spawn("sync", [
"info",
stringifySequence(value, get(configAtom)),
]);
set(dummyAtom, (x) => x + 1);
await thread.spawn("sync", ["info", stringifySequence(value)]);
});
});

const dummyAtom = atom(0);

export const RemoteContext = createContext(true);

export async function validateConfig(config: Config) {
Expand Down
14 changes: 8 additions & 6 deletions src/lib/assembly.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
Algebra,
Condition,
Config,
EncoderConfig,
Grouping,
Keyboard,
Expand Down Expand Up @@ -217,6 +216,7 @@ interface AssembleConfig {
encoder: EncoderConfig;
keyboard: Keyboard;
algebra: Algebra;
priority: [string, string, number][];
}

/**
Expand Down Expand Up @@ -334,17 +334,19 @@ export const assemble = (
});
knownWords.add(hash);
}
return result;
const priorityMap = getPriorityMap(config.priority);
return result.map((x) => {
const hash = `${x.name}-${x.pinyin_list.join(",")}`;
const level = priorityMap.get(hash) ?? -1;
return { ...x, level };
});
};

export const stringifySequence = (result: AssemblyResult, config: Config) => {
const priorityMap = getPriorityMap(config.encoder.priority_short_codes ?? []);
export const stringifySequence = (result: AssemblyResult) => {
return result.map((x) => {
const hash = `${x.name}-${x.pinyin_list.join(",")}`;
return {
...x,
sequence: summarize(x.sequence),
level: priorityMap.get(hash) ?? -1,
};
});
};
Expand Down
23 changes: 15 additions & 8 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import init, { WebInterface } from "libchai";
import { analysis } from "./lib/repertoire";
import { assemble } from "./lib/assembly";
import { defaultConfig } from "./lib";
import type { Assets } from "./atoms";

export interface WorkerInput {
type: "sync" | "encode" | "evaluate" | "optimize" | "analysis" | "assembly";
data: any;
Expand Down Expand Up @@ -38,11 +35,21 @@ export type WorkerOutput =
};

await init();
const webInterface = WebInterface.new(self.postMessage, defaultConfig, [], {
frequency: {},
key_distribution: {},
pair_equivalence: {},
} satisfies Assets);
const webInterface = WebInterface.new(
self.postMessage,
{
info: { name: "" },
source: null,
form: { alphabet: "", mapping: {} },
encoder: { max_length: 0, sources: {}, conditions: {} },
},
[],
{
frequency: {},
key_distribution: {},
pair_equivalence: {},
},
);

self.onmessage = async (event: MessageEvent<WorkerInput>) => {
const channel = event.ports[0]!;
Expand Down
8 changes: 8 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export default defineConfig(({ mode }) => {
emptyOutDir: true,
reportCompressedSize: false,
},
esbuild: {
supported: {
"top-level-await": true,
},
},
define: {
APP_VERSION: JSON.stringify(process.env.npm_package_version),
},
Expand Down Expand Up @@ -74,6 +79,9 @@ export default defineConfig(({ mode }) => {
reporter: ["text", "html"],
},
},
worker: {
format: "es",
},
};

switch (mode) {
Expand Down

0 comments on commit f4de326

Please sign in to comment.