File size: 5,529 Bytes
d1cda0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cfc1e6
d1cda0b
2cfc1e6
d1cda0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { GraphNode } from "@/lib/lexicon";

const REL_COLOR: Record<string, string> = {
  hypernym: "#576139",
  hyponym: "#486830",
  mero_part: "#886620",
  mero_substance: "#a0733a",
  mero_member: "#b8864c",
  holo_part: "#7a667a",
  holo_substance: "#918091",
  holo_member: "#a89aa8",
  attribute: "#a85070",
};

/**
 * Mini force-directed-style semantic graph (SRS §7.7.2, §8.1.2, §9.2).
 * Renders the focus synset at the centre with immediate neighbours laid
 * out radially. Nodes are clickable for graph navigation. When
 * `showIds` is false (Visitor view) no synset IDs are rendered — nodes
 * are labelled by their Bengali words only.
 */
export function SemanticGraph({
  nodes,
  showIds,
  hrefFor,
  width = 640,
  height = 420,
}: {
  nodes: GraphNode[];
  showIds: boolean;
  /** Build the navigation URL for a node; return null for non-clickable. */
  hrefFor: (node: GraphNode) => string | null;
  width?: number;
  height?: number;
}) {
  if (nodes.length === 0) return null;
  const centre = nodes[0];
  const neighbours = nodes.slice(1);
  const cx = width / 2;
  const cy = height / 2;
  const radius = Math.min(width, height) / 2 - 70;

  const placed = neighbours.map((node, i) => {
    // Distribute outgoing relations on top half, incoming on bottom
    const angle =
      (2 * Math.PI * i) / neighbours.length - Math.PI / 2 +
      (neighbours.length === 1 ? Math.PI / 4 : 0);
    const jitter = i % 2 === 0 ? 0 : 18;
    return {
      node,
      x: cx + (radius + jitter) * Math.cos(angle),
      y: cy + (radius + jitter) * Math.sin(angle),
    };
  });

  const label = (n: GraphNode, max = 16) => {
    const word = n.words[0] ?? (showIds ? n.synsetId : "—");
    return word.length > max ? `${word.slice(0, max)}…` : word;
  };

  const usedRels = [...new Set(neighbours.map((n) => n.relation))];

  return (
    <figure>
      <svg
        viewBox={`0 0 ${width} ${height}`}
        className="h-auto w-full select-none"
        role="img"
        aria-label="Semantic neighbourhood graph"
      >
        {/* edges */}
        {placed.map(({ node, x, y }) => (
          <g key={`edge-${node.synsetId}-${node.relation}`}>
            <line
              x1={cx}
              y1={cy}
              x2={x}
              y2={y}
              stroke={REL_COLOR[node.relation] ?? "#c0be9e"}
              strokeWidth={1.5}
              strokeDasharray={node.direction === "in" ? "4 3" : undefined}
              opacity={0.55}
            />
            <text
              x={(cx + x) / 2}
              y={(cy + y) / 2 - 4}
              textAnchor="middle"
              fontSize={9}
              fill={REL_COLOR[node.relation] ?? "#848870"}
              className="font-medium"
            >
              {node.relation}
            </text>
          </g>
        ))}

        {/* neighbour nodes */}
        {placed.map(({ node, x, y }) => {
          const href = hrefFor(node);
          const circle = (
            <g
              className={href ? "cursor-pointer" : undefined}
              key={`node-${node.synsetId}-${node.relation}`}
            >
              <circle
                cx={x}
                cy={y}
                r={26}
                fill="white"
                stroke={REL_COLOR[node.relation] ?? "#c0be9e"}
                strokeWidth={2}
              />
              <text
                x={x}
                y={y + 1}
                textAnchor="middle"
                fontSize={11}
                className="font-bengali fill-slate-800 font-semibold"
              >
                {label(node, 8)}
              </text>
              {showIds ? (
                <text
                  x={x}
                  y={y + 40}
                  textAnchor="middle"
                  fontSize={8}
                  className="fill-slate-400 font-mono"
                >
                  {node.synsetId}
                </text>
              ) : null}
            </g>
          );
          return href ? (
            <a href={href} key={`link-${node.synsetId}-${node.relation}`}>
              {circle}
            </a>
          ) : (
            circle
          );
        })}

        {/* centre node */}
        <g>
          <circle
            cx={cx}
            cy={cy}
            r={36}
            fill="#576139"
            stroke="#3e4828"
            strokeWidth={2}
          />
          <text
            x={cx}
            y={cy + 2}
            textAnchor="middle"
            fontSize={13}
            className="font-bengali fill-white font-bold"
          >
            {label(centre, 10)}
          </text>
          {showIds ? (
            <text
              x={cx}
              y={cy + 52}
              textAnchor="middle"
              fontSize={9}
              className="fill-slate-500 font-mono"
            >
              {centre.synsetId}
            </text>
          ) : null}
        </g>
      </svg>
      <figcaption className="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-[11px] text-slate-500">
        {usedRels.map((rel) => (
          <span key={rel} className="inline-flex items-center gap-1.5">
            <span
              className="inline-block h-2 w-2 rounded-full"
              style={{ background: REL_COLOR[rel] ?? "#c0be9e" }}
            />
            {rel}
          </span>
        ))}
        <span className="ml-auto italic">
          dashed = incoming relation · click a node to navigate
        </span>
      </figcaption>
    </figure>
  );
}