Published
Edited
May 31, 2018
7 forks
20 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
predict = {
const trimmed = text.trim().toLowerCase().replace(/(\.|\,|\!)/g, '').split(' ');
const inputBuffer = tf.buffer([1, metadata.max_len], "float32");
trimmed.forEach((word, i) => inputBuffer.set(metadata.word_index[word] + metadata.index_from, 0, i));
const input = inputBuffer.toTensor();
const predictOut = model.predict(input);
mutable positivity = predictOut.dataSync()[0];
predictOut.dispose();
return predictOut;
}
Insert cell
model = {
return this || await tf.loadModel("https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/model.json")
}
Insert cell
metadata = (await fetch("https://storage.googleapis.com/tfjs-models/tfjs/sentiment_cnn_v1/metadata.json")).json()
Insert cell
tf = require("@tensorflow/tfjs@0.8.0")
Insert cell
Insert cell
Insert cell
review = {
randomReview;
if (!this) return true;
return reviews[Math.floor(Math.random() * reviews.length)];
}
Insert cell
tljReviews = ({text: await (await fetch("https://gist.githubusercontent.com/jashkenas/ee5b07c30b99b23a6d5baf1675542bce/raw/03f093bc6d7d5d349307c84db61deb187edf1c90/last-jedi-user-reviews.html")).text()})
Insert cell
tljHTML = {
const doc = document.implementation.createHTMLDocument();
const div = doc.createElement("div");
div.innerHTML = await tljReviews.text;
doc.body.appendChild(div);
return doc;
}
Insert cell
reviews = {
const reviews = tljHTML.querySelectorAll(".review");
return Array.from(reviews).map(el => {
return {
score: +el.querySelector(".metascore_w").innerText,
review: (el.querySelector(".blurb_expanded") || el.querySelector(".review_body")).innerText.trim()
};
});
}
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