Published
Edited
Oct 14, 2018
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`_Unfortunately I don’t think simplify actually transforms the tree, so styledNLCST doesn’t do the trick here._`
Insert cell
Insert cell
{
const processor = retext().use(retextPlugins.sentiment);
const tree = processor.parse(inputText);
const sentiment = await processor.run(tree);
return styledNLCST(sentiment, node => {
if (node.type === "SentenceNode") {
const color = d3.scaleSequential(d3.interpolateRdYlGn).domain([-5, 5]);
return `background-color: ${color(node.data.polarity)}`;
}
});
}
Insert cell
Insert cell
{
const processor = retext();
const tree = processor.parse(inputText);
await processor.run(tree);
let sentences = [];
visit(tree, "SentenceNode", function(node) {
sentences.push(node);
});
const maxLength = d3.max(sentences, function(sentence) {
return sentence.position.end.offset - sentence.position.start.offset;
});
return styledNLCST(tree, (node, tree) => {
if (node.type === "SentenceNode") {
const length = node.position.end.offset - node.position.start.offset;
const color = d3
.scaleSequential(d3.interpolateGreys)
.domain([0, maxLength])(length);
return `background-color: ${color}; color: transparent;`;
}
});
}
Insert cell
styledNLCST = (root, style) => {
function one(node, parentIds) {
const result = "value" in node ? node.value : all(node, parentIds);
const tag = node.type === "ParagraphNode" ? "p" : "span";
return html`<${tag} style='${style(node, root)}'>${result}</${tag}>`;
}

function all({ children }) {
let results = [];
for (let child of children) {
results = results.concat(one(child));
}
return results;
}

return html`${all(root)}`;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more