@@ -5,13 +5,20 @@ import React, { useState, useEffect, useMemo } from "react"
55import type { NLPAnnotatorProps } from "../../types"
66import SequenceAnnotator from "../SequenceAnnotator"
77import DocumentLabeler from "../DocumentLabeler"
8+ import RelationshipAnnotator from "../RelationshipAnnotator"
89import Transcriber from "../Transcriber"
910import colors from "../../colors"
1011import Container from "../Container"
1112import useEventCallback from "use-event-callback"
1213
1314const 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+
1522export 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 }
0 commit comments