Published
Edited
Oct 26, 2021
Importers
Insert cell
Insert cell
Insert cell
words = text.replace(",", "").replace(".", "").replace("\n", " ").split(" ").map(d => d.toLowerCase())
Insert cell
[...text.matchAll("\\w+")]
Insert cell
[...text.matchAll("[\\w\\@\\#\\'\\&\\]\\*\\-\\/\\[\\=\\;]+")]
Insert cell
parser = function(txt) {
const regex = "[\\w\\@\\#\\'\\&\\]\\*\\-\\/\\[\\=\\;]+";
return [...txt.replace('---', ' ').replace('--', ' ').replace("''", " ").matchAll(regex)].map(d => d[0].toLowerCase());
}
Insert cell
parser(text)
Insert cell
dictionary = [{word: "a", value: 10, rank: 0}, {word: "b", value: 20, rank: 1}]
Insert cell
dictionary_words = dictionary.map(d => d.word)
Insert cell
index_lookup = Object.fromEntries(dictionary.map(d => [d.word, d.rank]))
Insert cell
counts = {
var counter = Array(Object.keys(index_lookup).length);
for (var i = 0; i < Object.keys(index_lookup).length; i++) {
counter[i] = 0;
}
for (var i = 0; i < words.length; i++) {
var word = words[i];
console.log(word);
if (word in index_lookup) {
console.log(index_lookup[word]);
counter[index_lookup[word]] += 1
}
}
return counter
}
Insert cell
Object.keys(index_lookup).length
Insert cell
counter = function(index_lookup, words) {
var tmp = Array(Object.keys(index_lookup).length);
for (var i = 0; i < Object.keys(index_lookup).length; i++) {
tmp[i] = 0;
}
for (var i = 0; i < words.length; i++) {
var word = words[i];
// console.log(word);
if (word in index_lookup) {
// console.log(index_lookup[word]);
tmp[index_lookup[word]] += 1
}
}
return tmp
}
Insert cell
counter(index_lookup, words)
Insert cell
"a" in dictionary_words
Insert cell
"a" in index_lookup
Insert cell
index_lookup["a"]
Insert cell
"c" in index_lookup
Insert cell
index_lookup["c"]
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