function getCellMap(cells: Cell[]) {
const cellMap = new Map<number, Cell>();
cellMap.set(cell.line, cell);
function getLabelSetMap(labelSets: LabelSet[]) {
const labelSetMap = new Map<string, LabelItem>();
labelSets.forEach(labelSet => {
labelSet.labelItems.forEach(labelItem => {
labelSetMap.set(labelItem.id, labelItem);
function convertOffset(label: SimpleLabel, cell: Cell) {
const offset = { "end_offset": 0, "start_offset": 0 };
const startTokenIndex = label.startTokenIndex;
const endTokenIndex = label.endTokenIndex;
const startCharIndex = label.startCharIndex;
const endCharIndex = label.endCharIndex;
for (let i = 0; i <= endTokenIndex; i++) {
if (i == startTokenIndex) {
offset.start_offset = offsetCounter + startCharIndex;
if (i == endTokenIndex) {
offset.end_offset = offsetCounter + endCharIndex + 1;
offsetCounter = offsetCounter + cell.tokens[i].length + 1;
function stringifyWithSpaces(obj) {
let result = JSON.stringify(obj, null, 1); // stringify, with line-breaks and indents
result = result.replace(/^ +/gm, " "); // remove all but the first space for each line
result = result.replace(/\n/g, ""); // remove line-breaks
result = result.replace(/{ /g, "{").replace(/ }/g, "}"); // remove spaces between object-braces and first/last props
result = result.replace(/\[ /g, "[").replace(/ \]/g, "]"); // remove spaces between array-brackets and first/last items
* This function should be written as this template and return string.
(document: Exportable): string => {
/// Implement export function here
const cellMap = getCellMap(document.cells);
const labelSetMap = getLabelSetMap(document.labelSets);
const examplesMap = new Map<number, Object>();
document.labels.forEach(label => {
const labelItem = labelSetMap.get(label.labelSetItemId);
const cell = cellMap.get(label.startCellLine);
const offset = convertOffset(label, cell);
const annotation = { "text_extraction": {"text_segment": offset}, "display_name": labelItem.labelName };
if (examplesMap.has(label.startCellLine)) {
const example = examplesMap.get(label.startCellLine);
example["annotations"].push(annotation);
examplesMap.set(label.startCellLine, example);
"annotations": [annotation],
"text_snippet": {"content": cell.tokens.join(' ')}
examplesMap.set(label.startCellLine, example);
examplesMap.forEach((value) => {
output.push(stringifyWithSpaces(value));
return output.join('\n');