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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more