Skip to content

Commit 6d211be

Browse files
committed
relationship annotator completely integrated
1 parent 9b6c92f commit 6d211be

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

src/components/Document/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import classNames from "classnames"
1616

1717
const Container = styled("div")(({ relationshipsOn }) => ({
1818
lineHeight: 1.5,
19-
marginTop: 64,
19+
marginTop: relationshipsOn ? 64 : 0,
2020
display: "flex",
2121
flexWrap: "wrap"
2222
}))

src/components/NLPAnnotator/index.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import React, { useState, useEffect, useMemo } from "react"
55
import type { NLPAnnotatorProps } from "../../types"
66
import SequenceAnnotator from "../SequenceAnnotator"
77
import DocumentLabeler from "../DocumentLabeler"
8+
import RelationshipAnnotator from "../RelationshipAnnotator"
89
import Transcriber from "../Transcriber"
910
import colors from "../../colors"
1011
import Container from "../Container"
1112
import useEventCallback from "use-event-callback"
1213

1314
const defaultValidator = () => []
1415

16+
const addColors = inputLabels => {
17+
return (inputLabels || []).map((label, i) =>
18+
label.color ? label : { ...label, color: colors[i % colors.length] }
19+
)
20+
}
21+
1522
export default function NLPAnnotator(props: NLPAnnotatorProps) {
1623
const validator = props.validator || defaultValidator
1724
let [output, changeOutput] = useState(null)
@@ -22,6 +29,12 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
2229
if (output === null && props.type === "label-sequence") {
2330
output = props.initialSequence || [{ text: props.document }]
2431
}
32+
if (output === null && props.type === "label-relationships") {
33+
output = {
34+
sequence: props.initialSequence,
35+
relationships: props.initialRelationships
36+
}
37+
}
2538

2639
useEffect(() => {
2740
const eventFunc = e => {
@@ -40,22 +53,18 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
4053
changeOutput(newOutput)
4154
}
4255

43-
let labels = useMemo(() => {
44-
let labels = props.labels || []
45-
if (!labels.some(l => !l.color)) {
46-
labels = labels.map((l, i) => ({
47-
...l,
48-
color: colors[i % colors.length]
49-
}))
50-
}
51-
return labels
52-
}, [props.labels])
56+
let labels = useMemo(() => addColors(props.labels), [props.labels])
57+
let entityLabels = useMemo(() => addColors(props.entityLabels), [
58+
props.entityLabels
59+
])
60+
let relationshipLabels = useMemo(() => addColors(props.relationshipLabels), [
61+
props.relationshipLabels
62+
])
5363

5464
const isPassingValidation = !validator(output).some(msg =>
5565
msg.toLowerCase().includes("error")
5666
)
5767

58-
console.log({ output })
5968
const onFinish = useEventCallback(() => {
6069
if (!isPassingValidation) return
6170
console.log("onFinish", output)
@@ -73,6 +82,7 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
7382
})
7483

7584
let annotator
85+
console.log("props.type", props.type)
7686
switch (props.type) {
7787
case "label-sequence":
7888
annotator = (
@@ -87,6 +97,16 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
8797
case "transcribe":
8898
annotator = <Transcriber {...props} onChange={onChange} />
8999
break
100+
case "label-relationships":
101+
annotator = (
102+
<RelationshipAnnotator
103+
{...props}
104+
entityLabels={entityLabels}
105+
relationshipLabels={relationshipLabels}
106+
onChange={onChange}
107+
/>
108+
)
109+
break
90110
default:
91111
annotator = null
92112
}

src/components/NLPAnnotator/index.story.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,34 @@ storiesOf("NLPAnnotator", module)
7878
onFinish={action("onFinish")}
7979
/>
8080
))
81+
.add("Relationship Labeler", () => (
82+
<NLPAnnotator
83+
hotkeysEnabled
84+
onChange={action("onChange")}
85+
onFinish={action("onFinish")}
86+
onNext={action("onNext")}
87+
onPrev={action("onPrev")}
88+
type="label-relationships"
89+
document={`Lorem ipsum dolor sit amet.`}
90+
entityLabels={[
91+
{
92+
id: "noun",
93+
displayName: "Noun"
94+
},
95+
{
96+
id: "proper-noun",
97+
displayName: "Proper Noun"
98+
}
99+
]}
100+
relationshipLabels={[
101+
{
102+
id: "eaten by",
103+
displayName: "Eaten By"
104+
},
105+
{
106+
id: "lives in",
107+
displayName: "Lives In"
108+
}
109+
]}
110+
/>
111+
))

src/components/RelationshipAnnotator/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const LabelSelectorContainer = styled("div")({ display: "flex" })
2929
export default function RelationshipAnnotator(
3030
props: RelationshipAnnotatorProps
3131
) {
32+
console.log({ props })
3233
const [highlightedItems, changeHighlightedItems] = useState([])
3334
const [relationships, setRelationships] = useState(props.relationships || [])
3435
const [activePair, setActivePair] = useState(null)
@@ -52,7 +53,9 @@ export default function RelationshipAnnotator(
5253
(props.entityLabels || [])
5354
.concat(props.relationshipLabels)
5455
.reduce(
55-
(acc, l, i) => ((acc[l.id] = colors[i % colors.length]), acc),
56+
(acc, l, i) => (
57+
(acc[l.id] = l.color || colors[i % colors.length]), acc
58+
),
5659
{}
5760
),
5861
[props.entityLabels, props.relationshipLabels]

src/types.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export type RelationshipAnnotatorProps = {
4949
type: "label-relationships",
5050
hotkeysEnabled?: boolean,
5151
separatorRegex?: RegExp,
52-
relationships: Array<Relationship>,
5352
entityLabels?: Array<Label>,
5453
relationshipLabels?: Array<Label>,
5554
initialSequence?: Array<SequenceItem>,
55+
initialRelationships?: Array<Relationship>,
5656
document: string,
5757
onChange: ({
5858
sequence: Array<SequenceItem>,
@@ -74,7 +74,8 @@ export type NLPAnnotatorProps = {
7474
...
7575
| $Exact<SequenceAnnotatorProps>
7676
| $Exact<LabelDocumentProps>
77-
| $Exact<TranscriberProps>,
77+
| $Exact<TranscriberProps>
78+
| $Exact<RelationshipAnnotatorProps>,
7879
onNext?: Function,
7980
onPrev?: Function,
8081
titleContent?: string | ReactNode,
@@ -99,3 +100,9 @@ export type Output =
99100
initialTranscriptionText?: string,
100101
transcription: string
101102
}
103+
| {
104+
outputType: "label-relationships",
105+
document: string,
106+
sequence: Array<SequenceItem>,
107+
relationships: Array<Relationship>
108+
}

0 commit comments

Comments
 (0)