@@ -5,13 +5,20 @@ import React, { useState, useEffect, useMemo } from "react"
5
5
import type { NLPAnnotatorProps } from "../../types"
6
6
import SequenceAnnotator from "../SequenceAnnotator"
7
7
import DocumentLabeler from "../DocumentLabeler"
8
+ import RelationshipAnnotator from "../RelationshipAnnotator"
8
9
import Transcriber from "../Transcriber"
9
10
import colors from "../../colors"
10
11
import Container from "../Container"
11
12
import useEventCallback from "use-event-callback"
12
13
13
14
const defaultValidator = ( ) => [ ]
14
15
16
+ const addColors = inputLabels => {
17
+ return ( inputLabels || [ ] ) . map ( ( label , i ) =>
18
+ label . color ? label : { ...label , color : colors [ i % colors . length ] }
19
+ )
20
+ }
21
+
15
22
export default function NLPAnnotator ( props : NLPAnnotatorProps ) {
16
23
const validator = props . validator || defaultValidator
17
24
let [ output , changeOutput ] = useState ( null )
@@ -22,6 +29,12 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
22
29
if ( output === null && props . type === "label-sequence" ) {
23
30
output = props . initialSequence || [ { text : props . document } ]
24
31
}
32
+ if ( output === null && props . type === "label-relationships" ) {
33
+ output = {
34
+ sequence : props . initialSequence ,
35
+ relationships : props . initialRelationships
36
+ }
37
+ }
25
38
26
39
useEffect ( ( ) => {
27
40
const eventFunc = e => {
@@ -40,22 +53,18 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
40
53
changeOutput ( newOutput )
41
54
}
42
55
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
+ ] )
53
63
54
64
const isPassingValidation = ! validator ( output ) . some ( msg =>
55
65
msg . toLowerCase ( ) . includes ( "error" )
56
66
)
57
67
58
- console . log ( { output } )
59
68
const onFinish = useEventCallback ( ( ) => {
60
69
if ( ! isPassingValidation ) return
61
70
console . log ( "onFinish" , output )
@@ -73,6 +82,7 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
73
82
} )
74
83
75
84
let annotator
85
+ console . log ( "props.type" , props . type )
76
86
switch ( props . type ) {
77
87
case "label-sequence" :
78
88
annotator = (
@@ -87,6 +97,16 @@ export default function NLPAnnotator(props: NLPAnnotatorProps) {
87
97
case "transcribe" :
88
98
annotator = < Transcriber { ...props } onChange = { onChange } />
89
99
break
100
+ case "label-relationships" :
101
+ annotator = (
102
+ < RelationshipAnnotator
103
+ { ...props }
104
+ entityLabels = { entityLabels }
105
+ relationshipLabels = { relationshipLabels }
106
+ onChange = { onChange }
107
+ />
108
+ )
109
+ break
90
110
default :
91
111
annotator = null
92
112
}
0 commit comments