Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add tree-kdl subcommand #6

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
59 changes: 59 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ 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';
import { toEnglish } from './english/tree';
import { denotationRenderText } from './tree/place';
import { DTree } from './semantics/model';
import {
toPlainText,
toLatex,
toJson,
jsonStringifyCompact,
} from './semantics/render';

function getTrees(argv: {
sentence: string | undefined;
Expand Down Expand Up @@ -152,6 +161,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 Expand Up @@ -183,6 +204,44 @@ yargs
fs.writeFileSync(argv.output as string, toDocument(trees));
},
)
.command(
'denote',
'Full denotation in given format',
yargs => {
yargs.demandOption('sentence');
yargs.option('format', {
type: 'string',
choices: ['text', 'latex', 'json'],
default: 'text',
});
},

function (argv) {
const dtrees = getTrees({ ...argv, semantics: true }) as DTree[];
const format = argv.format as 'text' | 'latex' | 'json';
switch (format) {
case 'text':
for (const dtree of dtrees) {
console.log(toPlainText(dtree.denotation!));
}
break;
case 'latex':
for (const dtree of dtrees) {
console.log(toLatex(dtree.denotation!));
}
break;
case 'json':
console.log(
jsonStringifyCompact(
dtrees.map(({ denotation }) => toJson(denotation!)),
),
);
break;
default:
format satisfies never;
}
},
)
.command(
'test-sentences',
'Test parsing many sentences',
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 } : {}),
},
});
}
Loading
Loading