Skip to content

Instantly share code, notes, and snippets.

@pietrop
Last active November 12, 2020 14:09
Show Gist options
  • Save pietrop/a98c631031c16cf65d3c9c59c839b1e1 to your computer and use it in GitHub Desktop.
Save pietrop/a98c631031c16cf65d3c9c59c839b1e1 to your computer and use it in GitHub Desktop.
serialize and de-serialize array of words into a tsv string
/**
* Helper functions to serialize and de-serialize array of words into a tsv string
example words list
```
[
{
"id": 0,
"start": 1.4,
"end": 3.9,
"text": "Can"
},
{
"id": 1,
"start": 3.9,
"end": 4,
"text": "you"
},
{
"id": 2,
"start": 4,
"end": 4.1,
"text": "hear"
},
{
"id": 3,
"start": 4.1,
"end": 4.2,
"text": "it?"
},
..
]
```
example converted tsv string
```
1.4\t 3.9\t Can\n
3.9\t 4\t you\n
4\t 4.1\t hear\n
4.1\t 4.2\t it?\n
```
*/
function serializeToTsv(words) {
return words
.map((word) => {
return `${word.start}\t${word.end}\t${word.text}`;
})
.join('\n');
}
function deserializeTsvOfWords(data) {
return data.split('\n').map((w) => {
const warray = w.split('\t');
return {
start: warray[0],
end: warray[1],
text: warray[2],
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment