Public
Edited
Feb 7, 2024
Insert cell
Insert cell
data = [
"hello world",
"milk chocolate ice cream",
"a2 milk",
"almond milk",
"creative arts",
"vanilla ice cream",
"ice pops",
"red wine",
"red bull",
]
Insert cell
viewof prefix = Inputs.text("Query")
Insert cell
Insert cell
Insert cell
matches = {
const tokens = prefix.split(" ").filter((s) => s != "");
let prefixMatches = new Set();
let perTokenMatches = [];
for (const [i, t] of tokens.entries()) {
// let tokenMatches = new Set();
if (edgeNgramIndex.has(t)) {
const matches = edgeNgramIndex.get(t);
const matchedDocs = new Set([...matches].map((x) => data[x[0]]));
perTokenMatches.push([t, matches]);
if (i == 0) {
prefixMatches = matchedDocs;
} else {
prefixMatches = prefixMatches.intersection(matchedDocs);
}
// Array.from(edgeNgramIndex.get(t)).map((x) => prefixMatches.add(x));
} else {
// Make set empty on each non match
prefixMatches = new Set();
break;
}
}
return [prefixMatches, perTokenMatches];
}
Insert cell
queryToEdgeNgrams(data[1])
Insert cell
function queryToEdgeNgrams(query) {
const tokens = query.split(" ");
return tokens.flatMap((q, tidx) =>
Array.from(q).map((x, i) => [q.slice(0, i + 1), q, tidx])
);
}
Insert cell
edgeNgramIndex = makeEdgeNgramsIndex(data)
Insert cell
function makeEdgeNgramsIndex(data) {
let edgeNgramIndex = new Map();
data.forEach((query, docid) => {
queryToEdgeNgrams(query).forEach((x) => {
const [prefix, token, tidx] = x;
if (!edgeNgramIndex.has(prefix)) {
edgeNgramIndex.set(prefix, new Set([[docid, tidx]]));
} else {
edgeNgramIndex.get(prefix).add([docid, tidx]);
}
});
});
return edgeNgramIndex;
}
Insert cell
(Set.prototype.intersection = function (other) {
return new Set(
[...this, ...other].filter((i) => other.has(i) && this.has(i))
);
})
Insert cell
(Set.prototype.union = function (other) {
return new Set([...this, ...other]);
})
Insert cell
{
let a = new Set([1, 2, 3]);
let b = new Set([1, 2, 4]);
let intersect = a.intersection(b);
let union = a.union(b);
return [intersect, union];
}
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