Last active
December 3, 2019 15:11
-
-
Save ci7lus/441bbca59b6e15021d40b36d929d12ef to your computer and use it in GitHub Desktop.
Tweet Entity Parser written in TypeScript / https://is.gd/ltCGV5 (scrapbox.io/ci7lus)
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
// MIT License - Copyright (c) 2019 ci7lus | |
const text = Array.from(status.full_text) | |
const entities = Object.entries(status.entities) | |
.filter(entity => entity[1]) | |
.flatMap(([entityType, entityArr]) => { | |
return entityArr.map((entity: any) => {return {entityType, ...entity}}) | |
}) as ({ entityType: string; indices: [number, number], [name: string]: unknown })[] | |
const slices = Object.fromEntries(entities.map(entity => [entity.indices[0], entity])) | |
const parts = [] | |
let current = 0 | |
for (let i = 0; i < text.length; i++) { | |
if (i in slices) { | |
const slice = slices[i] | |
parts.push(text.slice(current, i).join("")) | |
parts.push(text.slice(i, slice.indices[1]).join("")) | |
current = slice.indices[1] | |
} | |
} | |
parts.push(text.slice(current).join("")) | |
console.log(parts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment