@dxos/react-ui-rdf 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +105 -0
- package/dist/lib/browser/index.mjs +249 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +251 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/FactViewer.d.ts +18 -0
- package/dist/types/src/FactViewer.d.ts.map +1 -0
- package/dist/types/src/FactViewer.stories.d.ts +21 -0
- package/dist/types/src/FactViewer.stories.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +28 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +54 -0
- package/src/FactViewer.stories.tsx +113 -0
- package/src/FactViewer.tsx +161 -0
- package/src/index.ts +6 -0
- package/src/types.ts +138 -0
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/react-ui-rdf",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Presentational viewer for extracted semantic facts.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
12
|
+
"author": "DXOS.org",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"source": "./src/index.ts",
|
|
18
|
+
"types": "./dist/types/src/index.d.ts",
|
|
19
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
20
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"types": "dist/types/src/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@dxos/pipeline-rdf": "0.0.0",
|
|
30
|
+
"@dxos/ui-types": "0.10.0",
|
|
31
|
+
"@dxos/react-ui-graph": "0.10.0",
|
|
32
|
+
"@dxos/react-ui-list": "0.10.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@storybook/react-vite": "^10.4.2",
|
|
36
|
+
"@types/react": "~19.2.17",
|
|
37
|
+
"@types/react-dom": "~19.2.3",
|
|
38
|
+
"react": "~19.2.7",
|
|
39
|
+
"react-dom": "~19.2.7",
|
|
40
|
+
"vite": "^8.0.16",
|
|
41
|
+
"@dxos/react-ui": "0.10.0",
|
|
42
|
+
"@dxos/ui-theme": "0.10.0",
|
|
43
|
+
"@dxos/storybook-utils": "0.10.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"react": "~19.2.7",
|
|
47
|
+
"react-dom": "~19.2.7",
|
|
48
|
+
"@dxos/react-ui": "0.10.0",
|
|
49
|
+
"@dxos/ui-theme": "0.10.0"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Read-only viewer for extracted semantic facts, rendered against static fixtures (no engine/AI).
|
|
7
|
+
*
|
|
8
|
+
* - `Default` passes `DEMO_FACTS` (includes an Alice→Paris/Rome conflict and a coherent Q3-meeting pair).
|
|
9
|
+
* - `Empty` passes `[]` to show the empty state.
|
|
10
|
+
* - Renders `FactViewer`, which groups facts by subject, flags predicate-level conflicts, and filters.
|
|
11
|
+
* - Minimal decorators (`withTheme` + `withLayout`); purely presentational.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
15
|
+
import React from 'react';
|
|
16
|
+
|
|
17
|
+
import { type RDF } from '@dxos/pipeline-rdf';
|
|
18
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
19
|
+
|
|
20
|
+
import { FactViewer, type FactViewerProps } from './FactViewer';
|
|
21
|
+
|
|
22
|
+
const extractor = {
|
|
23
|
+
id: 'default',
|
|
24
|
+
model: 'ai.claude.model.claude-haiku-4-5',
|
|
25
|
+
version: '1',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Static demo facts that exercise the viewer: an entity→entity conflict (f1 vs f2,
|
|
30
|
+
* same subject + predicate, different objects/sources) plus a coherent pair (f3/f4).
|
|
31
|
+
*/
|
|
32
|
+
const DEMO_FACTS: RDF.Fact[] = [
|
|
33
|
+
{
|
|
34
|
+
id: 'f1',
|
|
35
|
+
assertion: {
|
|
36
|
+
subject: { entity: 'alice' },
|
|
37
|
+
predicate: 'travelsTo',
|
|
38
|
+
object: { entity: 'paris' },
|
|
39
|
+
validFrom: '2026-06-12',
|
|
40
|
+
quote: "I think I'm probably going to Paris next week",
|
|
41
|
+
},
|
|
42
|
+
factuality: { value: 'PR+', polarity: '+', confidence: 0.6, nature: 'epistemic' },
|
|
43
|
+
attribution: { agent: 'alice', source: 'dxn:gmail:msg-1', generatedAtTime: '2026-06-06T09:00:00.000Z' },
|
|
44
|
+
recordedAt: '2026-06-06T09:00:00.000Z',
|
|
45
|
+
extractor,
|
|
46
|
+
sourceHash: 'demo',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 'f2',
|
|
50
|
+
assertion: {
|
|
51
|
+
subject: { entity: 'alice' },
|
|
52
|
+
predicate: 'travelsTo',
|
|
53
|
+
object: { entity: 'rome' },
|
|
54
|
+
quote: "Alice told me she's definitely going to Rome, not Paris.",
|
|
55
|
+
},
|
|
56
|
+
factuality: { value: 'CT+', polarity: '+', confidence: 0.95 },
|
|
57
|
+
attribution: { agent: 'bob', source: 'dxn:gmail:msg-2', generatedAtTime: '2026-06-07T10:00:00.000Z' },
|
|
58
|
+
recordedAt: '2026-06-07T10:00:00.000Z',
|
|
59
|
+
extractor,
|
|
60
|
+
sourceHash: 'demo',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: 'f3',
|
|
64
|
+
assertion: {
|
|
65
|
+
subject: { entity: 'q3-board-meeting' },
|
|
66
|
+
predicate: 'scheduledFor',
|
|
67
|
+
object: { literal: '2026-07-15' },
|
|
68
|
+
quote: 'The Q3 board meeting is confirmed for July 15 in London.',
|
|
69
|
+
},
|
|
70
|
+
factuality: { value: 'CT+', polarity: '+' },
|
|
71
|
+
attribution: { agent: 'carol', source: 'dxn:gmail:msg-3', generatedAtTime: '2026-06-08T11:00:00.000Z' },
|
|
72
|
+
recordedAt: '2026-06-08T11:00:00.000Z',
|
|
73
|
+
extractor,
|
|
74
|
+
sourceHash: 'demo',
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
id: 'f4',
|
|
78
|
+
assertion: {
|
|
79
|
+
subject: { entity: 'q3-board-meeting' },
|
|
80
|
+
predicate: 'locatedIn',
|
|
81
|
+
object: { entity: 'london' },
|
|
82
|
+
},
|
|
83
|
+
factuality: { value: 'CT+', polarity: '+' },
|
|
84
|
+
attribution: { agent: 'carol', source: 'dxn:gmail:msg-3', generatedAtTime: '2026-06-08T11:00:00.000Z' },
|
|
85
|
+
recordedAt: '2026-06-08T11:00:00.000Z',
|
|
86
|
+
extractor,
|
|
87
|
+
sourceHash: 'demo',
|
|
88
|
+
},
|
|
89
|
+
] satisfies RDF.Fact[];
|
|
90
|
+
|
|
91
|
+
const DefaultStory = (props: FactViewerProps) => <FactViewer {...props} />;
|
|
92
|
+
|
|
93
|
+
const meta = {
|
|
94
|
+
title: 'ui/react-ui-rdf/FactViewer',
|
|
95
|
+
render: DefaultStory,
|
|
96
|
+
decorators: [withTheme(), withLayout({ layout: 'column' })],
|
|
97
|
+
} satisfies Meta<typeof DefaultStory>;
|
|
98
|
+
|
|
99
|
+
export default meta;
|
|
100
|
+
|
|
101
|
+
type Story = StoryObj<typeof meta>;
|
|
102
|
+
|
|
103
|
+
export const Default: Story = {
|
|
104
|
+
args: {
|
|
105
|
+
facts: DEMO_FACTS,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const Empty: Story = {
|
|
110
|
+
args: {
|
|
111
|
+
facts: [],
|
|
112
|
+
},
|
|
113
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { useMemo, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type RDF, buildFactGraph, factSourceFromFacts } from '@dxos/pipeline-rdf';
|
|
8
|
+
import { Icon, IconButton, Input, Panel, ScrollArea, Tag, type ThemedClassName, Toolbar } from '@dxos/react-ui';
|
|
9
|
+
import { Tree } from '@dxos/react-ui-graph';
|
|
10
|
+
import { Empty, Listbox } from '@dxos/react-ui-list';
|
|
11
|
+
import { mx } from '@dxos/ui-theme';
|
|
12
|
+
|
|
13
|
+
import { Group, factualityColor, formatDate, formatTerm, graphToTreeNode, groupFacts, termKey } from './types';
|
|
14
|
+
|
|
15
|
+
type View = 'list' | 'graph';
|
|
16
|
+
|
|
17
|
+
export type FactViewerProps = ThemedClassName<{
|
|
18
|
+
facts: RDF.Fact[];
|
|
19
|
+
/** Context entity id; scopes the list and roots the graph. */
|
|
20
|
+
context?: string;
|
|
21
|
+
/** Predicate filter; when set, only facts with this predicate are shown. */
|
|
22
|
+
predicate?: string;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Viewer for extracted semantic facts with two views: a grouped **list** (by subject entity, with
|
|
27
|
+
* conflicts highlighted) and a **graph** (tidy tree rooted at the context entity, exploring the fact
|
|
28
|
+
* graph). A `context` entity scopes the list and roots the graph; a `predicate` further filters the
|
|
29
|
+
* list. Pure/presentational.
|
|
30
|
+
*/
|
|
31
|
+
export const FactViewer = ({ classNames, facts, context, predicate }: FactViewerProps) => {
|
|
32
|
+
const [filter, setFilter] = useState('');
|
|
33
|
+
const [view, setView] = useState<View>('list');
|
|
34
|
+
const scoped = useMemo(
|
|
35
|
+
() =>
|
|
36
|
+
facts.filter(
|
|
37
|
+
(fact) =>
|
|
38
|
+
(context == null ||
|
|
39
|
+
termKey(fact.assertion.subject) === context ||
|
|
40
|
+
termKey(fact.assertion.object) === context) &&
|
|
41
|
+
(predicate == null || fact.assertion.predicate === predicate),
|
|
42
|
+
),
|
|
43
|
+
[facts, context, predicate],
|
|
44
|
+
);
|
|
45
|
+
const groups = useMemo(() => groupFacts(scoped, filter), [scoped, filter]);
|
|
46
|
+
const graph = useMemo(
|
|
47
|
+
() => (context == null ? undefined : graphToTreeNode(buildFactGraph(context, factSourceFromFacts(facts)), context)),
|
|
48
|
+
[facts, context],
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<Panel.Root classNames={classNames}>
|
|
53
|
+
<Panel.Toolbar asChild>
|
|
54
|
+
<Toolbar.Root>
|
|
55
|
+
<Input.Root>
|
|
56
|
+
<Input.Label srOnly>Filter facts</Input.Label>
|
|
57
|
+
<Input.TextInput
|
|
58
|
+
placeholder='Filter by entity or predicate…'
|
|
59
|
+
value={filter}
|
|
60
|
+
onChange={(event) => setFilter(event.target.value)}
|
|
61
|
+
/>
|
|
62
|
+
</Input.Root>
|
|
63
|
+
<div role='none' className='grow' />
|
|
64
|
+
<IconButton
|
|
65
|
+
icon='ph--list--regular'
|
|
66
|
+
iconOnly
|
|
67
|
+
label='List view'
|
|
68
|
+
variant={view === 'list' ? 'primary' : 'default'}
|
|
69
|
+
onClick={() => setView('list')}
|
|
70
|
+
/>
|
|
71
|
+
<IconButton
|
|
72
|
+
icon='ph--graph--regular'
|
|
73
|
+
iconOnly
|
|
74
|
+
label='Graph view'
|
|
75
|
+
variant={view === 'graph' ? 'primary' : 'default'}
|
|
76
|
+
onClick={() => setView('graph')}
|
|
77
|
+
/>
|
|
78
|
+
</Toolbar.Root>
|
|
79
|
+
</Panel.Toolbar>
|
|
80
|
+
{view === 'list' ? (
|
|
81
|
+
<Panel.Content asChild>
|
|
82
|
+
<ScrollArea.Root padding>
|
|
83
|
+
<ScrollArea.Viewport classNames='flex flex-col gap-2 py-1'>
|
|
84
|
+
{groups.length === 0 && <Empty label='No facts.' />}
|
|
85
|
+
{groups.map((group) => (
|
|
86
|
+
<SubjectGroup key={group.subject} group={group} />
|
|
87
|
+
))}
|
|
88
|
+
</ScrollArea.Viewport>
|
|
89
|
+
</ScrollArea.Root>
|
|
90
|
+
</Panel.Content>
|
|
91
|
+
) : (
|
|
92
|
+
<Panel.Content classNames='overflow-hidden'>
|
|
93
|
+
{graph ? (
|
|
94
|
+
<Tree data={graph} variant='tidy' margin={80} classNames='w-full h-full' />
|
|
95
|
+
) : (
|
|
96
|
+
<Empty icon='ph--graph--regular' label='Select an entity to root the graph.' />
|
|
97
|
+
)}
|
|
98
|
+
</Panel.Content>
|
|
99
|
+
)}
|
|
100
|
+
</Panel.Root>
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const SubjectGroup = ({ group }: { group: Group }) => (
|
|
105
|
+
<div className='shrink-0 flex flex-col bg-card-surface border border-subdued-separator rounded-sm overflow-hidden'>
|
|
106
|
+
<div className='flex px-3 py-1 items-center justify-between'>
|
|
107
|
+
<h3>{group.subject}</h3>
|
|
108
|
+
{group.conflicted && (
|
|
109
|
+
<Tag hue='warning'>
|
|
110
|
+
<span className='flex items-center gap-1'>
|
|
111
|
+
<Icon icon='ph--warning--regular' size={3} />
|
|
112
|
+
conflict
|
|
113
|
+
</span>
|
|
114
|
+
</Tag>
|
|
115
|
+
)}
|
|
116
|
+
</div>
|
|
117
|
+
<Listbox.Root>
|
|
118
|
+
<Listbox.Content aria-label={group.subject}>
|
|
119
|
+
{group.facts.map((fact) => (
|
|
120
|
+
<FactRow key={fact.id} fact={fact} conflicting={group.conflictedIds.has(fact.id)} />
|
|
121
|
+
))}
|
|
122
|
+
</Listbox.Content>
|
|
123
|
+
</Listbox.Root>
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const cellClassNames =
|
|
128
|
+
'bg-input-surface border border-subdued-separator rounded-sm px-2 py-0.5 font-medium whitespace-nowrap truncate';
|
|
129
|
+
|
|
130
|
+
const FactRow = ({ fact, conflicting }: { fact: RDF.Fact; conflicting: boolean }) => {
|
|
131
|
+
const { assertion, factuality, attribution } = fact;
|
|
132
|
+
return (
|
|
133
|
+
<Listbox.Item
|
|
134
|
+
id={fact.id}
|
|
135
|
+
classNames={mx('flex flex-col items-stretch gap-2 px-3 py-1', conflicting && 'border-is-2 border-warning-border')}
|
|
136
|
+
>
|
|
137
|
+
{assertion.quote && <div className='text-sm text-description italic'>"{assertion.quote}"</div>}
|
|
138
|
+
|
|
139
|
+
<div className='grid grid-cols-[1fr_5rem] items-center justify-between gap-2'>
|
|
140
|
+
<div className='w-full grid grid-cols-[1fr_1rem_1fr_1rem_1fr] items-center flex-wrap'>
|
|
141
|
+
<span className={mx(cellClassNames, 'text-right')}>{formatTerm(assertion.subject)}</span>
|
|
142
|
+
<span className='border border-subdued-separator'></span>
|
|
143
|
+
<span className={mx(cellClassNames, 'text-center text-description')}>{assertion.predicate}</span>
|
|
144
|
+
<span className='border border-subdued-separator'></span>
|
|
145
|
+
<span className={mx(cellClassNames)}>{formatTerm(assertion.object)}</span>
|
|
146
|
+
</div>
|
|
147
|
+
|
|
148
|
+
<div className='flex items-center justify-end gap-2 shrink-0'>
|
|
149
|
+
{factuality.confidence != null && (
|
|
150
|
+
<span className='text-xs text-subdued'>{Math.round(factuality.confidence * 100)}%</span>
|
|
151
|
+
)}
|
|
152
|
+
<Tag hue={factualityColor(factuality.value)}>{factuality.value}</Tag>
|
|
153
|
+
</div>
|
|
154
|
+
</div>
|
|
155
|
+
|
|
156
|
+
<div className='text-xs text-subdued text-right'>
|
|
157
|
+
{[attribution.agent, attribution.source, formatDate(attribution.generatedAtTime)].filter(Boolean).join(' · ')}
|
|
158
|
+
</div>
|
|
159
|
+
</Listbox.Item>
|
|
160
|
+
);
|
|
161
|
+
};
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type FactGraph, type RDF } from '@dxos/pipeline-rdf';
|
|
6
|
+
import { type TreeNode } from '@dxos/react-ui-graph';
|
|
7
|
+
import { type ChromaticPalette, type MessageValence, type NeutralPalette } from '@dxos/ui-types';
|
|
8
|
+
|
|
9
|
+
export type Group = {
|
|
10
|
+
subject: string;
|
|
11
|
+
facts: RDF.Fact[];
|
|
12
|
+
conflicted: boolean;
|
|
13
|
+
conflictedIds: Set<string>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Group facts by subject entity and flag predicate-level conflicts within each group. */
|
|
17
|
+
export const groupFacts = (facts: RDF.Fact[], filter: string): Group[] => {
|
|
18
|
+
const needle = filter.trim().toLowerCase();
|
|
19
|
+
const filtered = needle
|
|
20
|
+
? facts.filter((fact) => {
|
|
21
|
+
const { subject, predicate, object } = fact.assertion;
|
|
22
|
+
return [formatTerm(subject), predicate, formatTerm(object)].some((value) =>
|
|
23
|
+
value.toLowerCase().includes(needle),
|
|
24
|
+
);
|
|
25
|
+
})
|
|
26
|
+
: facts;
|
|
27
|
+
|
|
28
|
+
// Group by the entity slug (so `DXOS`/`dxos` collapse) but remember a display label for the header.
|
|
29
|
+
const bySubject = new Map<string, RDF.Fact[]>();
|
|
30
|
+
const labelByKey = new Map<string, string>();
|
|
31
|
+
for (const fact of filtered) {
|
|
32
|
+
const key = termKey(fact.assertion.subject);
|
|
33
|
+
const list = bySubject.get(key) ?? [];
|
|
34
|
+
list.push(fact);
|
|
35
|
+
bySubject.set(key, list);
|
|
36
|
+
if (!labelByKey.has(key)) {
|
|
37
|
+
labelByKey.set(key, formatTerm(fact.assertion.subject));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return [...bySubject.entries()].map(([key, groupFactsList]) => {
|
|
42
|
+
const subject = labelByKey.get(key) ?? key;
|
|
43
|
+
const byPredicate = new Map<string, RDF.Fact[]>();
|
|
44
|
+
for (const fact of groupFactsList) {
|
|
45
|
+
const list = byPredicate.get(fact.assertion.predicate) ?? [];
|
|
46
|
+
list.push(fact);
|
|
47
|
+
byPredicate.set(fact.assertion.predicate, list);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const conflictedIds = new Set<string>();
|
|
51
|
+
for (const list of byPredicate.values()) {
|
|
52
|
+
const objects = new Set(list.map((fact) => termKey(fact.assertion.object)));
|
|
53
|
+
if (objects.size > 1) {
|
|
54
|
+
for (const fact of list) {
|
|
55
|
+
conflictedIds.add(fact.id);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
subject,
|
|
62
|
+
facts: groupFactsList,
|
|
63
|
+
conflicted: conflictedIds.size > 0,
|
|
64
|
+
conflictedIds,
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** The join/grouping key: the entity slug (or literal). Casing variants collapse to one key. */
|
|
70
|
+
export const termKey = (term: RDF.Term): string => ('entity' in term ? term.entity : term.literal);
|
|
71
|
+
|
|
72
|
+
/** Display form: the preserved surface label, else a prettified slug; literals render verbatim. */
|
|
73
|
+
export const formatTerm = (term: RDF.Term): string =>
|
|
74
|
+
'entity' in term ? (term.label ?? humanize(term.entity)) : term.literal;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Convert a {@link FactGraph} into a rooted {@link TreeNode} hierarchy for the tidy-tree renderer.
|
|
78
|
+
* A fact graph is a general (cyclic) graph; the tidy tree needs a hierarchy, so this takes the BFS
|
|
79
|
+
* spanning tree from `rootId` over the undirected edges (each node appears once). Returns `undefined`
|
|
80
|
+
* if the root isn't in the graph.
|
|
81
|
+
*/
|
|
82
|
+
export const graphToTreeNode = (graph: FactGraph, rootId: string): TreeNode | undefined => {
|
|
83
|
+
const nodeById = new Map(graph.nodes.map((node) => [node.id, node]));
|
|
84
|
+
if (!nodeById.has(rootId)) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
const adjacency = new Map<string, string[]>();
|
|
88
|
+
const link = (from: string, to: string) => {
|
|
89
|
+
const list = adjacency.get(from) ?? [];
|
|
90
|
+
list.push(to);
|
|
91
|
+
adjacency.set(from, list);
|
|
92
|
+
};
|
|
93
|
+
for (const edge of graph.edges) {
|
|
94
|
+
link(edge.source, edge.target);
|
|
95
|
+
link(edge.target, edge.source);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const visited = new Set<string>([rootId]);
|
|
99
|
+
const build = (id: string): TreeNode => {
|
|
100
|
+
const children: TreeNode[] = [];
|
|
101
|
+
for (const neighbour of adjacency.get(id) ?? []) {
|
|
102
|
+
if (!visited.has(neighbour)) {
|
|
103
|
+
visited.add(neighbour);
|
|
104
|
+
children.push(build(neighbour));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { id, label: nodeById.get(id)?.label ?? id, ...(children.length > 0 ? { children } : {}) };
|
|
108
|
+
};
|
|
109
|
+
return build(rootId);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** Render ids like `q3-board-meeting` as `Q3 Board Meeting`. */
|
|
113
|
+
export const humanize = (value: string): string =>
|
|
114
|
+
value.replace(/[-_]/g, ' ').replace(/\b\w/g, (char) => char.toUpperCase());
|
|
115
|
+
|
|
116
|
+
export const formatDate = (iso: string): string => iso.slice(0, 10);
|
|
117
|
+
|
|
118
|
+
/** Map FactBank factuality to a real react-ui palette member. */
|
|
119
|
+
export const factualityColor = (
|
|
120
|
+
factuality: RDF.FactualityValue,
|
|
121
|
+
): NeutralPalette | ChromaticPalette | MessageValence => {
|
|
122
|
+
switch (factuality) {
|
|
123
|
+
case 'CT+':
|
|
124
|
+
return 'emerald';
|
|
125
|
+
case 'PR+':
|
|
126
|
+
return 'amber';
|
|
127
|
+
case 'PS+':
|
|
128
|
+
return 'sky';
|
|
129
|
+
case 'CT-':
|
|
130
|
+
case 'PR-':
|
|
131
|
+
case 'PS-':
|
|
132
|
+
return 'red';
|
|
133
|
+
case 'CTu':
|
|
134
|
+
case 'Uu':
|
|
135
|
+
default:
|
|
136
|
+
return 'neutral';
|
|
137
|
+
}
|
|
138
|
+
};
|