1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
import React, { memo } from "react";
import { motion } from "motion/react";
import "./parsing.css";
import type { ViewProps } from "./logic/types";
import Word from "./Word";
import { clauseVariants, createHoverEffect } from "./animations";
import { useZoom } from "./hooks/useZoom";
import { NLP } from "sortug-ai";
// Function to check if a string is a punctuation character
const isLeaf = (node: NLP.Stanza.TreeNode): boolean => {
return node.children && node.children.length === 0;
};
const toIgnore = ["root", "s"];
// Component to render each node in the constituency tree
const TreeNode = ({
node,
nest = 0,
idx,
}: {
idx: number;
nest?: number;
node: NLP.Stanza.TreeNode;
}) => {
const neatChildren = node.children.reduce(
(acc: NLP.Stanza.TreeNode[], item) => {
if (NLP.isPunctuation(item.label)) return acc;
else return [...acc, item];
},
[],
);
return !isLeaf(node) ? (
<BranchNode
node={{ ...node, children: neatChildren }}
nest={nest}
idx={idx}
/>
) : (
<LeafNode text={node.label} />
);
};
const BranchNode = ({
node,
nest = 0,
idx,
}: {
idx: number;
nest?: number;
node: NLP.Stanza.TreeNode;
}) => {
const { viewState, handleElementClick } = useZoom();
const { level, cIndex } = viewState;
const selected = cIndex === idx;
const isFocused = level === "clause" && selected;
const color = `rgba(${SYNTAX_COLORS[node.label]})` || "100, 100, 100";
const style: any = { "--clause-underline-color": color };
const out = toIgnore.includes(node.label.toLowerCase());
const isPunct = NLP.isPunctuation(node.label);
if (isPunct) return null;
const clauseLabel = out ? null : NLP.Stanza.oneDescendant(node) ? (
<div className="word-pos">{node.label}</div>
) : (
<div className="clause-pos">{node.label}</div>
);
return (
<motion.div
style={style}
className={`clause${selected ? " selected" : ""}`}
custom={selected}
variants={clauseVariants}
initial="sentence"
animate={level}
onClick={(e) => handleElementClick(e, idx)}
whileHover={createHoverEffect(
level,
"sentence",
SYNTAX_COLORS[node.label],
)}
>
<div className="clause-inner">
<div style={{ display: "flex" }}>
{node.children.map((child, childIdx) => (
<TreeNode
key={childIdx}
node={child}
nest={nest + 1}
idx={childIdx + idx + 1} // Ensure unique index
/>
))}
</div>
{clauseLabel}
</div>
</motion.div>
);
};
function LeafNode({ text }: { text: string }) {
return <motion.div className="tree-leaf">{text}</motion.div>;
}
// Main Clause component
interface Props extends ViewProps {
sentence: NLP.Stanza.Sentence;
data: NLP.Stanza.TreeNode;
}
function SimpleClause(props: Props) {
const { sentence, data, context, rawText, idx } = props;
const { viewState, handleElementClick } = useZoom();
const { level, cIndex } = viewState;
const selected = cIndex === idx;
const isFocused = level === "clause" && selected;
console.log({ viewState, rawText, f: isFocused, idx, cIndex });
const words = extractWordsFromTree(sentence, data);
const segmented = words.map((w) => w.text);
console.log({ words });
return (
<motion.div
className={`clause-container ${selected ? "selected" : ""}`}
custom={selected}
variants={clauseVariants}
initial="sentence"
animate={level}
onClick={(e) => handleElementClick(e, idx)}
whileHover={createHoverEffect(level, "sentence", "220, 200, 255")}
>
{level === "sentence" ? (
<span className="clause">{rawText}</span>
) : (
<div className="words-container">
{words.map((word, wordIdx) => (
<Word
{...props}
key={word.text + wordIdx}
idx={wordIdx}
rawText={word.text}
word={word}
context={{
idx: wordIdx,
parentText: rawText,
segmented,
}}
/>
))}
</div>
)}
</motion.div>
);
}
function RecursiveClause(props: Props) {
const { sentence, data, context, rawText, idx } = props;
const { viewState, handleElementClick } = useZoom();
const { level, cIndex } = viewState;
const selected = cIndex === idx;
const isFocused = level === "clause" && selected;
// If we're at the word level, display words instead of the constituency tree
if (level === "word" && selected && data.children) {
// Extract word objects if available
const words = extractWordsFromTree(sentence, data);
if (words.length > 0) {
return (
<div className="words-container">
{words.map((word, wordIdx) => (
<Word
{...props}
key={word.text + wordIdx}
idx={wordIdx}
rawText={word.text}
word={word}
context={{
idx: wordIdx,
parentText: rawText,
segmented: words.map((w) => w.text),
}}
/>
))}
</div>
);
}
}
return (
<>
<TreeNode node={data} idx={idx} />
</>
);
}
// Helper function to extract words from the tree
function extractWordsFromTree(
sentence: NLP.Stanza.Sentence,
node: NLP.Stanza.TreeNode,
): NLP.Stanza.Word[] {
const words: NLP.Stanza.Word[] = [];
if (node.label && node.children.length === 0) {
// Check if this is a punctuation node
if (!NLP.isPunctuation(node.label)) {
// Find the matching word in the sentence
const word = sentence.words.find((w) => w.text === node.label);
if (word) {
words.push(word);
}
}
}
// Recursively process children
if (node.children) {
for (const child of node.children) {
words.push(...extractWordsFromTree(sentence, child));
}
}
// Remove duplicates by word id
const uniqueWords = Array.from(
new Map(words.map((word) => [word?.id, word])).values(),
).filter(Boolean) as NLP.Stanza.Word[];
return uniqueWords;
}
// Syntax highlighting colors - reusing from Stanza utils
const SYNTAX_COLORS: any = {
// Sentence - cornflower blue
S: "100,149,237",
// Sentence - cornflower blue
SBAR: "100,149,237",
// Sentence - cornflower blue
SBARQ: "100,149,237",
// Noun Phrase - coral
NP: "255,127,80",
// Verb Phrase - lime green
VP: "50,205,50",
// Prepositional Phrase - medium purple
PP: "147,112,219",
// Adjective Phrase - gold
AP: "255,215,0",
// Adverb Phrase - hot pink
AVP: "255,105,180",
// Noun - light salmon
NN: "255,160,122",
// Verb - light green
V: "144,238,144",
// Verb - light green
VB: "144,238,144",
// Verb - light green
VBP: "144,238,144",
// Verb - light green
VBG: "144,238,144",
// Verb - light green
VBZ: "144,238,144",
// Verb - light green
VBD: "144,238,144",
// Verb - light green
VBN: "144,238,144",
// Adjective - khaki
JJ: "240,230,140",
// Adverb - plum
ADV: "221,160,221",
// Preposition - light sky blue
PR: "135,206,250",
// Preposition - light sky blue
IN: "135,206,250",
// Preposition - light sky blue
TO: "135,206,250",
// Determiner - light gray
DT: "211,211,211",
// Personal pronoun - thistle
PPN: "216,191,216",
// Coordinating conjunction - dark gray
CC: "169,169,169",
};
export default memo(RecursiveClause);
|