Export Transformer
/**
* This function should be written as this template and return string.
*/
(document: Exportable): string => {
/// Implement export function here
return document.cells.map((cell) => cell.content).join('\n');
};Sample Case
function getCellMap(cells: Cell[]) {
const cellMap = new Map<number, Cell>();
cells.forEach(cell => {
cellMap.set(cell.line, cell);
})
return cellMap;
}
function getLabelSetMap(labelSets: LabelSet[]) {
const labelSetMap = new Map<string, LabelItem>();
labelSets.forEach(labelSet => {
labelSet.labelItems.forEach(labelItem => {
labelSetMap.set(labelItem.id, labelItem);
})
});
return labelSetMap;
}
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;
let offsetCounter = 0;
for (let i = 0; i <= endTokenIndex; i++) {
if (i == startTokenIndex) {
offset.start_offset = offsetCounter + startCharIndex;
}
if (i == endTokenIndex) {
offset.end_offset = offsetCounter + endCharIndex + 1;
break
}
offsetCounter = offsetCounter + cell.tokens[i].length + 1;
}
return offset;
}
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
return result;
}
/**
* 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);
} else {
const example = {
"annotations": [annotation],
"text_snippet": {"content": cell.tokens.join(' ')}
};
examplesMap.set(label.startCellLine, example);
}
});
let output = [];
examplesMap.forEach((value) => {
output.push(stringifyWithSpaces(value));
})
return output.join('\n');
};Uploading the Export File Transformer
Last updated