Skip to content

Commit

Permalink
cli: add tree-kdl subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
uakci committed May 13, 2024
1 parent bd12d4c commit cf77988
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
56 changes: 56 additions & 0 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 @@ -50,6 +50,7 @@
"better-react-mathjax": "^2.0.3",
"canvas": "^2.11.2",
"discord.js": "^14.13.0",
"kdljs": "^0.2.0",
"lodash": "^4.17.21",
"mathjax": "^3.2.2",
"mathjax-full": "^3.2.2",
Expand Down
14 changes: 14 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { trimTree } from './tree/trim';
import { drawTreeToCanvas } from './tree/draw';
import { parse } from './modes/parse';
import { textual_tree_from_json } from './modes/textual-tree';
import KDL from 'kdljs';
import { formatTreeAsKdl } from './modes/kdl';
import { testSentences } from './modes/test-sentences';
import { denote } from './semantics/denote';
import { ToaqTokenizer } from './morphology/tokenize';
Expand Down Expand Up @@ -152,6 +154,18 @@ yargs
console.log(JSON.stringify(trees));
},
)
.command(
'tree-kdl',
'List of parse trees in KDL format',
yargs => {
yargs.demandOption('sentence');
},

function (argv) {
const trees = getTrees(argv);
console.log(KDL.format(trees.map(formatTreeAsKdl)));
},
)
.command(
'tree-text',
'List of parse trees in plain text format',
Expand Down
60 changes: 60 additions & 0 deletions src/modes/kdl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import KDL from 'kdljs';
import { Tree } from '../tree';

const toStringRecord = <O extends object>(obj: O) => {
for (const key in obj) {
if (!Object.hasOwn(obj, key)) continue;
if (obj[key] === undefined) delete obj[key];
else (obj as Record<string, string>)[key] = String(obj[key]);
}
return obj as { [k in keyof O]: O[k] extends undefined ? never : string };
};

export interface PartialKdlNode {
name: string;
values?: any[];
properties?: {};
children?: PartialKdlNode[];
tags?: {
name?: string;
values?: any[];
properties?: {};
};
}

export const kdlNode = (partialNode: PartialKdlNode): KDL.Node => ({
name: partialNode.name,
values: toStringRecord(partialNode.values ?? []),
properties: toStringRecord(partialNode.properties ?? {}),
children: partialNode.children?.map(kdlNode) ?? [],
tags: {
// kdljs type stubs define this type too strictly; the API itself does
// accept undefined here
name: partialNode.tags?.name ?? (undefined as unknown as string),
values: toStringRecord(partialNode.tags?.values ?? []),
properties: toStringRecord(partialNode.tags?.properties ?? {}),
},
});

export function formatTreeAsKdl(tree: Tree): KDL.Node {
const children =
'word' in tree
? []
: 'children' in tree
? tree.children
: [tree.left, tree.right];

return kdlNode({
name: tree.label,
children: children.map(formatTreeAsKdl),
values:
'word' in tree
? ['value' in tree.word ? tree.word.value : tree.word.text]
: [],
properties: {
binding: tree.binding,
coindex: tree.coindex,
...('word' in tree ? { id: tree.id, movedTo: tree.movedTo } : {}),
},
});
}

0 comments on commit cf77988

Please sign in to comment.