Published
Edited
Feb 22, 2021
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
categoryCodeSet = ({
"@context": "https://schema.org",
"@type": "CategoryCodeSet",
alternateName: "North American Industry Classification System",
author: {
"@type": "Corporation",
name: "NAICS Association",
legalName: "NAICS Association, LLC"
},
dateModified: "2017",
datePublished: "1997",
description: "See: https://www.naics.com/history-naics-code/", // Text type expected
hasCategoryCode,
name: "NAICS",
url: "https://www.naics.com/"
})
Insert cell
Insert cell
Insert cell
hasCategoryCode = codes.map(d => {
const categoryCode = {
"@type": "CategoryCode",
codeValue: d.codeValue,
name: d.name
}
const keyphrases = getKeyphrases(d);
if (keyphrases && (keyphrases.length > 0)) {
// singular naming, in contrast to https://schema.org/keywords, see: https://schema.org/docs/styleguide.html
categoryCode.keyphrase = keyphrases.length === 1 ? keyphrases[0] : keyphrases;
}
if (d.description) {
categoryCode.description = d.description;
}
categoryCode.url = `https://www.naics.com/naics-code-description/?code=${d.codeValue}`;
return categoryCode;
})
Insert cell
codes = {
const text = await FileAttachment("codes@2.csv").text(); // exported from Excel
return dsv.parse(text, d => {
let name = d.name.trim(); // trimming might be unneccessary
const isTrilateral = name.endsWith("T"); // trilateral agreement between United States, Canada, and Mexico
if (isTrilateral) {
name = name.slice(0, name.length - 1);
}
name = toSentenceCase(name);
const code = {
codeValue: d.codeValue.trim(), // trimming might be unneccessary
isTrilateral,
name
};
const description = shorten(d.description);
if (!description.startsWith("See industry description for") && (description !== "NULL")) {
code.description = description;
}
return code;
});
}
Insert cell
toSentenceCase = (name) => name[0].toUpperCase() + name.toLowerCase().slice(1)
Insert cell
shorten = (description) => {
const cut = (text, passage) => {
let index = text.indexOf(passage);
if (index >= 0) {
return text.slice(0, index);
}
return text;
};
let result = cut(description, "Illustrative Examples");
result = cut(result, "Cross-References");
return result.trim();
}
Insert cell
keyphrases = {
const text = await FileAttachment("keyphrases.csv").text(); // exported from Excel
const keyphrases = dsv.parse(text);
return d3.rollup(
keyphrases,
v => Array.from(v, d => d.keyphrase),
d => d.codeValue);
}
Insert cell
getKeyphrases = (code) =>
keyphrases.get(code.codeValue)
?.filter(d => d.toUpperCase() !== code.name.toUpperCase())
Insert cell
dsv = d3.dsvFormat(";")
Insert cell
d3 = require("d3-array@2", "d3-dsv@2", "d3-format@2")
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