-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first progress to annotation suggestion visualization
- Loading branch information
Showing
5 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...tend/src/components/LLMDialog/steps/AnnotationResultStep/AnnotationResultStepDocument.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { AnnotationResult } from "../../../../api/openapi/models/AnnotationResult.ts"; | ||
import SdocHooks from "../../../../api/SdocHooks.ts"; | ||
import TextAnnotatorValidator from "../../../../views/annotation/TextAnnotatorValidator/TextAnnotatorValidator.tsx"; | ||
|
||
function AnnotationResultStepDocument({ annotationResult }: { annotationResult: AnnotationResult }) { | ||
const sdoc = SdocHooks.useGetDocument(annotationResult.sdoc_id); | ||
|
||
if (sdoc.isSuccess) { | ||
return <TextAnnotatorValidator sdoc={sdoc.data} annotations={annotationResult.suggested_annotations} />; | ||
} | ||
return null; | ||
} | ||
|
||
export default AnnotationResultStepDocument; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
frontend/src/views/annotation/DocumentRenderer/useComputeTokenDataWithAnnotations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useMemo } from "react"; | ||
import { SourceDocumentWithDataRead } from "../../../api/openapi/models/SourceDocumentWithDataRead.ts"; | ||
import { SpanAnnotationReadResolved } from "../../../api/openapi/models/SpanAnnotationReadResolved.ts"; | ||
import { IToken } from "./IToken.ts"; | ||
|
||
function useComputeTokenDataWithAnnotations({ | ||
sdoc, | ||
annotations, | ||
}: { | ||
sdoc: SourceDocumentWithDataRead; | ||
annotations: SpanAnnotationReadResolved[]; | ||
}) { | ||
// computed | ||
// todo: maybe implement with selector? | ||
const tokenData: IToken[] | undefined = useMemo(() => { | ||
const offsets = sdoc.token_character_offsets; | ||
const texts = sdoc.tokens; | ||
const result = texts.map((text, index) => ({ | ||
beginChar: offsets[index][0], | ||
endChar: offsets[index][1], | ||
index, | ||
text, | ||
whitespace: offsets.length > index + 1 && offsets[index + 1][0] - offsets[index][1] > 0, | ||
newLine: text.split("\n").length - 1, | ||
})); | ||
return result; | ||
}, [sdoc]); | ||
|
||
// todo: maybe implement with selector? | ||
// this map stores annotationId -> SpanAnnotationReadResolved | ||
const annotationMap = useMemo(() => { | ||
const result = new Map<number, SpanAnnotationReadResolved>(); | ||
annotations.forEach((a) => result.set(a.id, a)); | ||
return result; | ||
}, [annotations]); | ||
|
||
// this map stores tokenId -> spanAnnotationId[] | ||
const annotationsPerToken = useMemo(() => { | ||
const result = new Map<number, number[]>(); | ||
annotations.forEach((annotation) => { | ||
for (let i = annotation.begin_token; i <= annotation.end_token - 1; i++) { | ||
const tokenAnnotations = result.get(i) || []; | ||
tokenAnnotations.push(annotation.id); | ||
result.set(i, tokenAnnotations); | ||
} | ||
}); | ||
return result; | ||
}, [annotations]); | ||
|
||
return { tokenData, annotationsPerToken, annotationMap }; | ||
} | ||
|
||
export default useComputeTokenDataWithAnnotations; |
35 changes: 35 additions & 0 deletions
35
frontend/src/views/annotation/TextAnnotatorValidator/TextAnnotatorValidator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { SourceDocumentWithDataRead } from "../../../api/openapi/models/SourceDocumentWithDataRead.ts"; | ||
import { SpanAnnotationReadResolved } from "../../../api/openapi/models/SpanAnnotationReadResolved.ts"; | ||
import DocumentRenderer from "../DocumentRenderer/DocumentRenderer.tsx"; | ||
import useComputeTokenDataWithAnnotations from "../DocumentRenderer/useComputeTokenDataWithAnnotations.ts"; | ||
|
||
interface TextAnnotatorValidatorProps { | ||
sdoc: SourceDocumentWithDataRead; | ||
annotations: SpanAnnotationReadResolved[]; | ||
} | ||
|
||
function TextAnnotatorValidator({ sdoc, annotations }: TextAnnotatorValidatorProps) { | ||
// computed / custom hooks | ||
const { tokenData, annotationsPerToken, annotationMap } = useComputeTokenDataWithAnnotations({ | ||
sdoc: sdoc, | ||
annotations: annotations, | ||
}); | ||
|
||
return ( | ||
<> | ||
<DocumentRenderer | ||
className="myFlexFillAllContainer" | ||
onMouseUp={() => console.log("HI")} | ||
html={sdoc.html} | ||
tokenData={tokenData} | ||
annotationsPerToken={annotationsPerToken} | ||
annotationMap={annotationMap} | ||
isViewer={false} | ||
projectId={sdoc.project_id} | ||
style={{ zIndex: 1, overflowY: "auto" }} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
export default TextAnnotatorValidator; |