Skip to content

Commit 2174cd3

Browse files
committed
refactored parse tree and data types
1 parent 19e7f5e commit 2174cd3

9 files changed

Lines changed: 41 additions & 96 deletions

File tree

backend/problem/views/parse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ def send_to_parser(self, data: ParserInput) -> dict | None:
7070
logger.info("Sending to LangPro service:", asdict(data))
7171

7272
params = asdict(data)
73+
# ask LangPro container to return results in a format suitable for
74+
# this application
7375
params['format'] = 'annotator'
7476

7577
try:

frontend/src/app/annotate/annotate.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ParseResponse, ParseService } from "@/services/parse.service";
99
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
1010
import { ProblemResponse } from "@/types";
1111
import { ProblemService } from "@/services/problem.service";
12+
import { Tree } from "@/tree";
1213

1314
@Component({
1415
selector: "la-annotate",
@@ -29,7 +30,7 @@ export class AnnotateComponent implements OnInit {
2930
private parseService = inject(ParseService);
3031
private problemService = inject(ProblemService);
3132

32-
public ccgTrees: any[] = [];
33+
public ccgTrees: Tree<string>[] = [];
3334

3435
private problem: ProblemResponse | null = null;
3536

@@ -46,7 +47,7 @@ export class AnnotateComponent implements OnInit {
4647

4748
onParse(response: ParseResponse) {
4849
console.log("Parse response:", response);
49-
this.ccgTrees = response!.data.ccg_trees;
50+
this.ccgTrees = response!.data.ccg_trees.map((tree: any) => new Tree(tree));
5051
}
5152

5253
startParse() {

frontend/src/app/annotate/annotation-menu/annotation-menu.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
faTree,
77
faPenNib,
88
} from "@fortawesome/free-solid-svg-icons";
9-
import { AnnotationParseResultsComponent } from "../annotation-parse-results/annotation-parse-results.component";
109
import { AnnotationTableauComponent } from "../annotation-tableau/annotation-tableau.component";
1110
import { AnnotationCommentsComponent } from "../annotation-comments/annotation-comments.component";
1211
import { ParseSVG } from "../parse-tree/parse-svg.component";
@@ -17,7 +16,6 @@ import { ParseSVG } from "../parse-tree/parse-svg.component";
1716
imports: [
1817
NgbNavModule,
1918
FontAwesomeModule,
20-
AnnotationParseResultsComponent,
2119
AnnotationTableauComponent,
2220
AnnotationCommentsComponent,
2321
ParseSVG
Lines changed: 1 addition & 1 deletion
Loading

frontend/src/app/annotate/parse-tree/parse-svg.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Component, ChangeDetectorRef, ElementRef, Input, ViewChild, afterNextRender} from '@angular/core';
22
import { CommonModule } from "@angular/common";
33
import { Subject } from "rxjs";
4-
import { Dimensions } from '@/types';
4+
import { CCGTerm, Dimensions } from '@/types';
55
import { ParseTree } from './parse-tree.component';
6+
import { Tree } from "@/tree";
67
import svgPanZoom from 'svg-pan-zoom';
78

89
@Component({
@@ -34,5 +35,5 @@ export class ParseSVG {
3435
}
3536

3637
@Input()
37-
tree: any = {};
38+
tree: Tree<CCGTerm> = Tree.empty();
3839
}

frontend/src/app/annotate/parse-tree/parse-term.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Component, ElementRef, Input, Output, ViewChild, EventEmitter } from '@angular/core';
2-
import { Dimensions } from '@/types';
2+
import { CCGTerm, Dimensions } from '@/types';
33

44
@Component({
5-
selector: "[term]",
5+
selector: "[parse-term]",
66
standalone: true,
77
imports: [],
88
templateUrl: "./parse-term.component.svg",
@@ -19,7 +19,7 @@ export class ParseTerm {
1919
public bg?: string;
2020

2121
@Input()
22-
public term: string = '';
22+
public term: CCGTerm = [];
2323

2424
@Input()
2525
public end: boolean = false;
Lines changed: 13 additions & 21 deletions
Loading
Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, Input, Output, EventEmitter } from '@angular/core';
22
import { ParseTerm } from './parse-term.component';
3-
import { Dimensions } from '@/types';
4-
import { sum } from "@/util";
3+
import { Dimensions, CCGTerm } from '@/types';
4+
import { TreeNode } from "@/tree";
55

66
@Component({
77
selector: "[parse-tree]",
@@ -11,38 +11,10 @@ import { sum } from "@/util";
1111
styleUrl: "./parse-tree.component.scss",
1212
})
1313
export class ParseTree {
14-
expanded: boolean[] = [];
14+
expanded: boolean = true;
1515

16-
_tree: any;
1716
@Input()
18-
get tree(): any {
19-
return this._tree;
20-
}
21-
22-
set tree(value: any) {
23-
if (value.children) {
24-
// this is a temporary translation until the tree code can be refactored:
25-
let translated =
26-
(function translate(p: any) {
27-
if (!p) return;
28-
let out: any = {};
29-
if (Array.isArray(p.node)) {
30-
out.nodes = [{term: p.node}];
31-
}
32-
else {
33-
out.nodes = [{term: [p.node]}];
34-
}
35-
out.subtrees = p.children ? p.children.map(translate) : [];
36-
return out;
37-
})(value);
38-
this._tree = translated;
39-
}
40-
else {
41-
this._tree = value;
42-
}
43-
44-
this.expanded = [true];
45-
}
17+
treeNode: TreeNode<CCGTerm> = {value: [], children: []};
4618

4719
levelHeight = 40;
4820

@@ -73,62 +45,38 @@ export class ParseTree {
7345

7446
emitSize() {
7547
this.onSize.emit({
76-
width: Math.max(this.subWidth * (this.tree.subtrees?.length ?? 0), this.width),
77-
height: this.subHeight + this.totalNodeHeight() + (this.tree.subtrees?.length ? this.levelHeight : 0)
48+
width: Math.max(this.subWidth * (this.treeNode?.children?.length ?? 0), this.width),
49+
height: this.subHeight + this.nodeHeight() + (this.treeNode?.children?.length ? this.levelHeight : 0)
7850
});
7951
}
8052

8153
/* Determines the X coordinate of where subtree of index `idx` should be drawn */
8254
subtreePosition(idx: number) {
8355
let widthWithPadding = 1.15 * this.subWidth;
84-
return widthWithPadding * idx - (widthWithPadding / 2) * (this.tree.subtrees.length - 1);
56+
return widthWithPadding * idx - (widthWithPadding / 2) * (this.treeNode.children.length - 1);
8557
}
8658

8759
/* Generates a path definition for linking the end of the current node to subtree index `idx` */
8860
subtreeLinkPath(idx: number) {
8961
return [
9062
// move to end of current node
91-
`M 0 ${this.totalNodeHeight() - 15 }`,
63+
`M 0 ${this.nodeHeight() - 15 }`,
9264
// curve to half-way to subtree
9365
`q 0 ${this.levelHeight/2} ${this.subtreePosition(idx)/2 } ${this.levelHeight/2}`,
9466
// curve from half-way to subtree, to subtree position
9567
`q ${this.subtreePosition(idx)/2} 0 ${this.subtreePosition(idx)/2} ${this.levelHeight/2}`
9668
].join(' ');
9769
}
9870

99-
nodeHeight(node: any) {
100-
// note that in this case height includes bottom padding for the node
101-
return node.rule ? 60 : 40;
102-
}
103-
104-
totalNodeHeight() {
105-
return sum(this.tree.nodes.map(this.nodeHeight));
106-
}
107-
108-
nodeY(idx: number) {
109-
// y position of a given node is the sum of heights of all preceeding nodes
110-
return sum(this.tree.nodes.slice(0, idx).map(this.nodeHeight));
111-
}
112-
113-
termClick(idx: number) {
114-
// the last node can't be collapsed
115-
// unless there are subtrees
116-
if (idx < this.expanded.length - 1 || this.tree.subtrees) {
117-
this.expanded[idx] = !this.expanded[idx];
118-
}
71+
nodeHeight() {
72+
return 40;
11973
}
12074

121-
visibleNodes() {
122-
const firstCollpased = this.expanded.indexOf(false);
123-
if (firstCollpased == -1) {
124-
return this.tree.nodes;
125-
}
126-
return this.tree.nodes.slice(0, firstCollpased + 1);
75+
termClick() {
76+
this.expanded = !this.expanded;
12777
}
12878

12979
showSubtrees() {
130-
// if any of the current level nodes is collapsed, this also means
131-
// we shouldn't render any subtrees
132-
return this.expanded.indexOf(false) == -1;
80+
return this.expanded;
13381
}
13482
}

frontend/src/app/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ export interface Dimensions {
8383
width: number;
8484
height: number;
8585
}
86+
87+
88+
export type CCGTerm = string[];

0 commit comments

Comments
 (0)