Skip to content

Commit bd43156

Browse files
committed
Fix the recommendation order, add text filtering for the known string
1 parent 37eb2f7 commit bd43156

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/App.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ function App() {
2323
setKanjiRoot(relevantRoot);
2424

2525
// Get order in string form
26-
const orderString = getRecommendedOrder(relevantRoot).map(x => x.name).join('');
26+
const orderString = getRecommendedOrder(relevantRoot).map(x => x.name).filter(x => !knownKanjiString.includes(x)).join('');
2727
setRecommendedOrder(orderString);
2828

29-
30-
31-
3229
// Cleanup function
3330
return () => {
3431
// Perform any necessary cleanup here

src/components/KanjiGraph.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ const KanjiGraph = ({ kanjiNodeList }: KanjiGraphProps) => {
4242
target: "n-" + child.name,
4343
// label: "Edge " + node.name + "-" + child.name
4444
};
45-
edges.push(currentEdge);
45+
46+
const existingEdge = edges.find(edge =>
47+
edge.id === currentEdge.id &&
48+
edge.source === currentEdge.source &&
49+
edge.target === currentEdge.target
50+
);
51+
52+
if (!existingEdge) {
53+
edges.push(currentEdge);
54+
}
4655
}
4756
}
4857

src/logic/kanjiorder.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ export class KanjiNode {
6666
//---------------------------------------------------------------------
6767
// FUNCTIONS TO CREATE THE GRAPH
6868

69-
function findOrCreateNode(name: string, thelist: KanjiNode[]): KanjiNode {
69+
function findOrCreateNode(name: string, thelist: KanjiNode[], properties?: KanjiNode): KanjiNode {
7070
let node = thelist.find(x => x.name === name);
7171
if (!node) {
72-
node = new KanjiNode(name);
72+
if (properties) {
73+
node = properties.shallowCopy();
74+
} else {
75+
node = new KanjiNode(name);
76+
}
7377
thelist.push(node);
7478
}
7579
return node;
@@ -99,6 +103,7 @@ function buildDAG(): [KanjiNode[], KanjiNode] {
99103
}
100104

101105
// New subgraph functions
106+
102107
/**
103108
* This function takes the whole kanji topography as graph and node list and returns a subgraph of kanjis to know.
104109
* The subgraph is a DAG with the same structure as the original, but with only the relevant nodes.
@@ -128,22 +133,24 @@ function getTargetSubgraph(allRoot: KanjiNode, nodeList: KanjiNode[], targetKanj
128133
}
129134

130135
// Step 2: create a new graph with only the relevant nodes
131-
const relevantWalker = (node: KanjiNode): KanjiNode => {
136+
const relevantWalker = (startNode: KanjiNode, foundList: KanjiNode[]): KanjiNode => {
132137
// copy the node properties
133-
const currentNewNode = node.shallowCopy();
138+
// debugger;
139+
const currentNewNode = findOrCreateNode(startNode.name, foundList, startNode);
134140

135141
// add only the relevant parents
136-
for (const child of node.children) {
142+
for (const child of startNode.children) {
137143
if (child.isRelevant) {
138-
const relevantChildrenRoot = relevantWalker(child);
144+
const relevantChildrenRoot = relevantWalker(child, foundList);
139145
currentNewNode.addChild(relevantChildrenRoot);
140146
relevantChildrenRoot.addParent(currentNewNode);
141147
}
142148
}
143149

144150
return currentNewNode
145151
}
146-
const relevantRootNode = relevantWalker(allRoot);
152+
const relevantKanji: KanjiNode[] = [];
153+
const relevantRootNode = relevantWalker(allRoot, relevantKanji);
147154

148155
// Step 2a: clean up the big graph
149156
for (const node of nodeList) {
@@ -209,6 +216,23 @@ function markKnown(rootNode: KanjiNode, knownList: string[]) {
209216
// ---------------------------------------------------------------------
210217
// Order Recommendation
211218

219+
// This function could be used to check if all parents of a node are in the list, but
220+
// it is not used since I instead check on insert time.
221+
// function isReachable(target: KanjiNode, included: KanjiNode[]): boolean {
222+
// // Special case: if the target is a root, it is always reachable
223+
// if (target.parents.length === 0) {
224+
// return true;
225+
// }
226+
227+
// // Node is reachable if all parents are included
228+
// for (const parent of target.parents) {
229+
// if (!included.includes(parent)) {
230+
// return false;
231+
// }
232+
// }
233+
// return true;
234+
// }
235+
212236
/**
213237
* Generates an order to study the kanji, where
214238
* 1. Consitituating parts are always learned before the kanji that uses them
@@ -227,7 +251,6 @@ export function getRecommendedOrder(rootNode: KanjiNode): KanjiNode[] {
227251

228252
while (possibleCandidates.length > 0) {
229253
const candidate = possibleCandidates.shift();
230-
231254
// This should never happen but the compiler wants this line
232255
if (!candidate) throw new Error("unexpectedly didn't receive a candidate");
233256

@@ -263,7 +286,6 @@ export function getRecommendedOrder(rootNode: KanjiNode): KanjiNode[] {
263286
return (a.priority || 0) - (b.priority || 0);
264287
});
265288
}
266-
267289
return returnList;
268290
}
269291

0 commit comments

Comments
 (0)