Skip to content

Commit

Permalink
Improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
tansongchen committed Jun 26, 2024
1 parent 55c5b50 commit b6db7a2
Show file tree
Hide file tree
Showing 68 changed files with 868 additions and 720 deletions.
1 change: 0 additions & 1 deletion examples/yustar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ analysis:
no_cross: false
selector:
- 根少优先
- 全符笔顺
- 连续笔顺
- 结构完整
- 能连不交
Expand Down
27 changes: 18 additions & 9 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"deep-object-diff": "^1.1.9",
"immer": "^10.1.1",
"jotai": "^2.8.0",
"jotai-location": "^0.5.5",
"jotai-minidb": "^0.0.8",
"jotai-optics": "^0.3.2",
"js-md5": "^0.8.3",
Expand Down
1 change: 0 additions & 1 deletion scripts/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ const analyze = (
const estdup1 = [...map1.values()]
.filter((x) => x.length > 1)
.reduce((a, b) => a + estimate(b.length, alphabet), 0);
// console.log([...map2].sort((a, b) => b[1].length - a[1].length).slice(0, 10))
const estdup2 = [...map2.values()]
.filter((x) => x.length > 1)
.reduce((a, b) => a + estimate(b.length, alphabet * alphabet), 0);
Expand Down
3 changes: 2 additions & 1 deletion scripts/recursive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync, writeFileSync } from "fs";
import { Character, Compound, PrimitiveCharacter } from "~/lib";
import type { Compound, PrimitiveCharacter } from "~/lib";
import { Character } from "~/lib";

const repertoire = JSON.parse(
readFileSync("public/cache/repertoire.json", "utf-8"),
Expand Down
3 changes: 2 additions & 1 deletion spec/affine.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { affineMerge, Compound, SVGGlyph } from "~/lib";
import type { Compound, SVGGlyph } from "~/lib";
import { affineMerge } from "~/lib";
import { describe, expect, it } from "vitest";

describe("affine transformations", () => {
Expand Down
12 changes: 8 additions & 4 deletions spec/analysis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import type { SVGGlyph } from "~/lib";
import {
SVGGlyph,
analysis,
assemble,
classifier,
Expand All @@ -9,15 +9,15 @@ import {
recursiveRenderComponent,
recursiveRenderCompound,
} from "~/lib";
import { primitiveRepertoire, repertoire } from "./mock";
import { focusAnalysis, primitiveRepertoire, repertoire } from "./mock";
import { readFileSync } from "fs";

describe("e2e test", () => {
it("checks database integrity", () => {
const config = examples["mswb"];
const analysisResult = analysis(
repertoire,
config,
focusAnalysis(config, repertoire),
Object.keys(repertoire).filter(isValidCJKChar),
);
const { componentError, compoundError } = analysisResult;
Expand All @@ -27,7 +27,11 @@ describe("e2e test", () => {

const assemblyResult = assemble(
repertoire,
config,
{
algebra: config.algebra,
encoder: config.encoder,
keyboard: config.form,
},
characters,
[],
analysisResult,
Expand Down
23 changes: 18 additions & 5 deletions spec/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import {
renderRootList,
} from "~/lib";
import type { DerivedComponent, PrimitiveRepertoire } from "~/lib";
import { primitiveRepertoire, repertoire, computedComponents } from "./mock";
import {
primitiveRepertoire,
repertoire,
computedComponents,
focusAnalysis,
} from "./mock";

describe("pruning", () => {
const strokes = 4;
Expand Down Expand Up @@ -44,13 +49,21 @@ describe("get component scheme", () => {
name: "",
data: "国标五分类",
keyboard: "米十五笔",
encoder: "形音码",
encoder: "形音码(米十五笔)",
});

const rootList = renderRootList(repertoire, config);
const analysisConfig = focusAnalysis(config, repertoire);
const rootList = renderRootList(repertoire, [
...analysisConfig.primaryRoots,
...analysisConfig.secondaryRoots,
]);
it("can get component scheme", () => {
const = computedComponents.!;
const scheme = getComponentScheme(, rootList, config, classifier);
const scheme = getComponentScheme(
,
[...rootList.values()],
analysisConfig,
classifier,
);
expect(scheme).toHaveProperty("sequence");
});
});
37 changes: 20 additions & 17 deletions spec/degenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
binaryToIndices,
defaultDegenerator,
generateSliceBinaries,
defaultConfig,
} from "~/lib";
import { describe, it, expect } from "vitest";
import { create, all } from "mathjs";
import { computedGlyphs2 as renderedGlyphs, computedComponents } from "./mock";
import { RenderedGlyph } from "~/lib";
import type { RenderedGlyph } from "~/lib";

const { randomInt } = create(all!, {
randomSeed: "a",
Expand Down Expand Up @@ -46,39 +45,43 @@ describe("bi-directional conversion", () => {
describe("generate slice binaries", () => {
it("should find multiple occurence of a root", () => {
const {,} = computedComponents;
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([9, 5, 3]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([
9, 5, 3,
]);
});

it("should be able to distinguish 土 and 士", () => {
const {,,,} = computedComponents;
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([7]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([7]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([7]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([7]);
});

it("should be able to distinguish 未 and 末", () => {
const {,,,} = computedComponents;
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([31]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([47, 31]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([31]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([
47, 31,
]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([]);
});

it("should be able to distinguish 口 and 囗", () => {
const {,,,} = computedComponents;
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([14]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultConfig, !, !)).toEqual([13]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([14]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, !)).toEqual([13]);
});

it("should be able to distinguish 木无十 and 全字头", () => {
const 木无十 = computedComponents["\ue087"]!;
const 全字头 = computedComponents["\ue43d"]!;
const {} = computedComponents;
expect(generateSliceBinaries(defaultConfig, !, 木无十)).toEqual([3]);
expect(generateSliceBinaries(defaultConfig, !, 全字头)).toEqual([]);
expect(generateSliceBinaries(defaultDegenerator, !, 木无十)).toEqual([3]);
expect(generateSliceBinaries(defaultDegenerator, !, 全字头)).toEqual([]);
});
});

Expand Down
21 changes: 20 additions & 1 deletion spec/mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import rawrepertoire from "../public/cache/repertoire.json";
import type { BasicComponent, PrimitiveCharacter } from "~/lib";
import type {
AnalysisConfig,
BasicComponent,
Config,
PrimitiveCharacter,
Repertoire,
} from "~/lib";
import { listToObject, determine, computeComponent } from "~/lib";

export const primitiveRepertoire = Object.fromEntries(
Expand All @@ -22,3 +28,16 @@ export const computedGlyphs2 = Object.fromEntries(
return [k, v.glyph];
}),
);

export const focusAnalysis = (config: Config, repertoire: Repertoire) => {
const result: AnalysisConfig = {
analysis: config.analysis ?? {},
primaryRoots: new Set(
Object.keys(config.form.mapping).filter((x) => repertoire[x]),
),
secondaryRoots: new Set(
Object.keys(config.form.grouping ?? []).filter((x) => repertoire[x]),
),
};
return result;
};
27 changes: 17 additions & 10 deletions spec/selector.spec.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
import { describe, expect, it } from "vitest";
import { length, bias, order, crossing, attaching, Scheme } from "~/lib";
import type { Environment, Scheme } from "~/lib";
import { length, bias, order, crossing, attaching, analysis } from "~/lib";
import { select } from "~/lib";
import { computedComponents, repertoire } from "./mock";
import { computedComponents, focusAnalysis, repertoire } from "./mock";
import { defaultConfig } from "~/lib";
import { Config } from "~/lib";

const analysisConfig = focusAnalysis(defaultConfig, repertoire);
const {} = computedComponents as any;
const env: Environment = {
component: ,
rootMap: new Map(),
...analysisConfig,
};

const rootMap = new Map<number, string>();

describe("length", () => {
it("should measure the length of scheme", () => {
expect(length.key([8, 7], , defaultConfig, rootMap)).toBe(2);
expect(length.key([8, 7], env)).toBe(2);
});
});

describe("bias", () => {
it("should measure the bias of scheme", () => {
expect(bias.key([8, 7], , defaultConfig, rootMap)).toEqual([-1, -3]);
expect(bias.key([8, 7], env)).toEqual([-1, -3]);
});
});

describe("order", () => {
it("should measure the order of scheme", () => {
expect(order.key([8, 7], , defaultConfig, rootMap)).toEqual(0);
expect(order.key([8, 7], env)).toEqual(0);
});
});

describe("crossing", () => {
it("should measure the crossing of scheme", () => {
expect(crossing.key([8, 7], , defaultConfig, rootMap)).toBe(0);
expect(crossing.key([12, 3], , defaultConfig, rootMap)).toBe(1);
expect(crossing.key([8, 7], env)).toBe(0);
expect(crossing.key([12, 3], env)).toBe(1);
});
});

describe("attaching", () => {
it("should measure the attaching of scheme", () => {
expect(attaching.key([8, 7], , defaultConfig, rootMap)).toBe(1);
expect(attaching.key([12, 3], , defaultConfig, rootMap)).toBe(0);
expect(attaching.key([8, 7], env)).toBe(1);
expect(attaching.key([12, 3], env)).toBe(0);
});
});

Expand All @@ -46,7 +53,7 @@ describe("select", () => {
expect(
(
select(
defaultConfig,
analysisConfig,
,
[
[12, 3],
Expand Down
4 changes: 2 additions & 2 deletions spec/topology.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, describe, it } from "vitest";
import type { StrokeRelation } from "~/lib";
import type { StrokeRelation, CubicCurve, LinearCurve } from "~/lib";
import { findTopology, curveRelation, renderSVGGlyph } from "~/lib";
import { CubicCurve, LinearCurve, area, render } from "~/lib";
import { area, render } from "~/lib";
import type { Draw, Point } from "~/lib";
import { computedGlyphs2 as computedGlyphs } from "./mock";
import { getIntervalPosition, makeCurve } from "~/lib";
Expand Down
10 changes: 8 additions & 2 deletions src/atoms/analysis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { Analysis, Degenerator, Selector, SieveName } from "~/lib";
import type {
Analysis,
Degenerator,
Selector,
SieveName,
Feature,
} from "~/lib";
import { focusAtom } from "jotai-optics";
import { analysisAtom } from "./config";
import { Feature, mergeClassifier } from "~/lib";
import { mergeClassifier } from "~/lib";
import { atom } from "jotai";

export const degeneratorAtom = focusAtom(analysisAtom, (o) =>
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { atomWithStorage } from "jotai/utils";
import { Dictionary, Distribution, Equivalence, Frequency } from ".";
import type { Dictionary, Distribution, Equivalence, Frequency } from ".";
import { MiniDb } from "jotai-minidb";

const db = new MiniDb<Dictionary>();
Expand Down
Loading

0 comments on commit b6db7a2

Please sign in to comment.