Public
Edited
Apr 21, 2024
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
stratify = d3.stratify()(SuttaPitakaTree)
Insert cell
DOM.download(serialize(SuttaPitakaTree), "SuttaPitakaTree", "Download JSON")
Insert cell
Insert cell
Insert cell
Insert cell
SuttaPitakaTree = [
{
id: "sutta",
parentId: null,
name: "Suttapiṭaka",
nameEn: "Basket of Discourses",
acro: "SP"
},
// {
// id: "test",
// parentId: "sutta",
// name: "",
// acro: ""
// },
// { id: "test2", parentId: "test", name: "", acro: "" },
// { id: "test3", parentId: "test2", name: "", acro: "" },
// { id: "test4A", parentId: "test3", name: "", acro: "" },
// { id: "test4B", parentId: "test3", name: "", acro: "" },
// { id: "test4C", parentId: "test3", name: "", acro: "" },
// { id: "test4D", parentId: "test3", name: "", acro: "" },
// { id: "test4E", parentId: "test3", name: "", acro: "" },
...dn,
...mn,
...sn, // without sutta
...an, // without sutta
// ...kn // with sutta
// {
// id: "testA",
// parentId: "sutta",
// name: "",
// acro: ""
// },
// { id: "testA2", parentId: "testA", name: "", acro: "" },
// { id: "testA3", parentId: "testA2", name: "", acro: "" },
// { id: "testA4", parentId: "testA3", name: "", acro: "" },
// { id: "testB4", parentId: "testA3", name: "", acro: "" },
// { id: "testC4", parentId: "testA3", name: "", acro: "" },
// { id: "testD4", parentId: "testA3", name: "", acro: "" },
// { id: "testE4", parentId: "testA3", name: "", acro: "" }
]
Insert cell
DOM.download(
serialize(SuttaPitakaTreeFull),
"SuttaPitakaTreeFull",
"Download JSON"
)
Insert cell
SuttaPitakaTreeFull = [
{
id: "sutta",
ParentId: null,
name: "Suttapiṭaka",
nameEn: "Basket of Discourses",
acro: "SP"
},
// {
// id: "test",
// parentId: "sutta",
// name: "",
// acro: ""
// },
//{ id: "test2", parentId: "test", name: "", acro: "" },
...dn,
...mn,
...sn, // without sutta
...an, // without sutta
...knStarNoRoot // with sutta
]
Insert cell
suttapitakatreefull = FileAttachment("SuttaPitakaTreeFull (2).json").json()
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
SuttaPitakaTree2 = [
{
id: "sutta",
ParentId: null,
name: "Sutta Pitaka",
acro: "SP"
},
...dn,
...mn,
...sn, // without sutta
...an, // without sutta
...kn2 // without sutta removed done afterward
]
Insert cell
kn2 = {
kn.shift();
return [
{ id: "kn", parentId: "sutta", name: "Khuddakanikāya", acro: "KN" },
...kn
];
}
Insert cell
Insert cell
acro = super_extra_info.find((e) => e.uid == "long").acronym
Insert cell
Insert cell
uid_expansion_new_en = {
const afterDot = (str) => str.substring(str.indexOf(".") + 1);
const uids = [];
for (const d in super_name_en) {
const uid = afterDot(d);
const acro = super_extra_info.find((e) => e.uid == uid)?.acronym;
const obj = { uid: uid, acro: acro, name: super_name_en[d] };
uids.push(obj);
}
return uids;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
console.log(kn_name)
Insert cell
Insert cell
Insert cell
Insert cell
dn = traverseTree(
dn_tree,
uid_expansion_new,
traverseName(dn_name),
child_range,
traverseName(dn_name_en),
uid_expansion_new_en
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
an_name_en = fetch(
"https://raw.githubusercontent.com/suttacentral/sc-data/master/sc_bilara_data/translation/en/sujato/name/sutta/an-name_translation-en-sujato.json",
{
headers: { Accept: "application/json" }
}
).then((response) => response.json())
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
kn_name_en = kn_name_en_Plus[0]
Insert cell
kn = FileAttachment("knStar (5).json").json()
Insert cell
kn3 = FileAttachment("knStar (1)@1.json").json()
Insert cell
import { serialize } from "@palewire/saving-json"
Insert cell
DOM.download(serialize(knStar), "knStar", "Download JSON")
Insert cell
Insert cell
Insert cell
Insert cell
// this is just to ease the function below and get rid of the unwanted part of the id in names.

traverseName = (nameArray) => {
const tree = [];
for (var [key, value, path, parent] of traverse(nameArray)) {
if (typeof value === "string") {
var ar = key.split("."); // split(".",2) with limit dosen't work
ar = [ar.shift(), ar.join(".")];
const result = { id: ar[1], name: value };
tree.push(result);
}
}
return tree;
}
Insert cell
// inspired from https://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript

// we need to transform the json from SuttaCentral repo into a flat hierachy array that d3 can understand

traverseTree = (
suttaTree,
uidNameTree = [],
nameTree = [],
childRangeTree = [],
nameTreeEn = [],
uidNameTreeEn = [],
leaf = true,
rootId = "sutta"
) => {
const tree = [];
for (var [key, value, path] of traverse(suttaTree)) {
// this is a node
if (Array.isArray(value)) {
let name = uidNameTree.find((d) => d.uid == key)?.name;
if (name == null) name = nameTree.find((d) => d.id == key)?.name;
let nameEn = uidNameTreeEn.find((d) => d.uid == key)?.name;
if (nameEn == null) nameEn = nameTreeEn.find((d) => d.id == key)?.name;
const acro = uidNameTree.find((d) => d.uid == key)?.acro;
const childRange = childRangeTree[key];
const par = path.length > 2 ? path[path.length - 3] : rootId;
const result = {
id: key,
parentId: par,
name: name,
nameEn: nameEn,
acro: childRange == null ? (acro == null ? "" : acro) : childRange
// this give empty string if acro is undefined.
};
// const result = { id: key, parentId: par };
tree.push(result);
}
// this is a leaf
if (typeof value === "string" && leaf == true) {
const name = nameTree.find((d) => d.id == value)?.name;
const nameEn = nameTreeEn.find((d) => d.id == value)?.name;
const childRange = childRangeTree[value];
const letters = value.split(/\d/)[0];
const numbers = value.slice(value.split(/\d/)[0].length);
// .replace("-", "–");
const acroLetters = uidNameTree.find((d) => d.uid == letters)?.acro;
const acro = acroLetters + " " + numbers;
const par = path[path.length - 2];
const result = {
id: value,
parentId: par,
name: name,
nameEn: nameEn,
acro: childRange == null ? (acro == null ? "" : acro) : childRange
};
tree.push(result);
}
}
return tree;
}
Insert cell
Insert cell
Insert cell
suttapitakatreestar = FileAttachment("SuttaPitakaTreeStar.json").json()
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