| import type { FrontendAnalyzeResult } from '../api/GLTR_API'; |
|
|
| |
| |
| |
| |
| export function contextAndTargetFromTokenIndex( |
| rd: FrontendAnalyzeResult, |
| tokenIndex: number |
| ): { context: string; targetPrediction: string } | null { |
| const text = rd.originalText ?? ''; |
| const tokens = rd.bpe_strings; |
| if (!tokens?.length || tokenIndex < 0 || tokenIndex >= tokens.length) return null; |
| const tok = tokens[tokenIndex]; |
| const off = tok?.offset; |
| if (!off || off.length < 2) return null; |
| const [a, b] = off; |
| const context = text.slice(0, a); |
| const targetPrediction = tok.raw ?? text.slice(a, b); |
| return { context, targetPrediction }; |
| } |
|
|