Public
Edited
Jun 25, 2022
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dataTranscript = {
const attachment = await FileAttachment(
"SupremeCourtAbortionArguments@1.txt"
).text();

return attachment
.replaceAll("\n", "")
.replaceAll("\f", "")
.replaceAll("\t", "")
.replaceAll(/[ ]+/g, " ");
}
Insert cell
dataSegments = {
const segmentsMatches = dataTranscript.matchAll(/[A-Z]+[.]?[A-Z ]+(?=:)/g);
const segmentsIndexes = [];

// console.log(segmentsMatches);

for (const segment of segmentsMatches) {
segmentsIndexes.push(segment.index);
}

const segments = [];

for (let i = 0; i < segmentsIndexes.length; i++) {
if (i < segmentsIndexes.length - 1) {
segments.push(
dataTranscript.slice(segmentsIndexes[i], segmentsIndexes[i + 1])
);
} else {
segments.push(dataTranscript.slice(segmentsIndexes[i]));
}
}

const segmentsProcessed = segments.map(d => {
const colonPosition = d.indexOf(":");

const name = d.slice(0, colonPosition);
const nameSplit = name.split(" ");
const nameLast = nameSplit[nameSplit.length - 1];
const nameLastFormatted = nameLast[0] + nameLast.slice(1).toLowerCase();

const line = d.slice(colonPosition + 1);

return { name, nameLastFormatted, line };
});

return segmentsProcessed;
}
Insert cell
dataGrouped = d3
.rollups(
dataSegments,
v => v.map(e => e["line"]),
d => d["nameLastFormatted"]
)
.map(d => {
return { name: d[0], lines: d[1] };
})
.sort((a, b) => d3.ascending(a["name"], b["name"]))
Insert cell
dataSpeakers = dataGrouped.map(d => {
const { name } = d;

return name;
})
Insert cell
Insert cell
results = {
const input = `\\b${keywords}\\b`;
const regex = new RegExp(input, "ig");

return dataGrouped.map(d => {
const { name, lines } = d;

let count = 0;
const quotes = [];
const quotesSplit = [];
const matches = [];

lines.map(e => {
const quote = e;

const quoteMatchesIterator = quote.matchAll(regex);
const quoteMatches = [];

const quoteSplitPoints = [0];

for (const match of quoteMatchesIterator) {
quoteMatches.push(match[0]);
quoteSplitPoints.push(match.index);
quoteSplitPoints.push(match.index + match[0].length);
}

const quoteMatchesCount = quoteMatches.length;
const quoteSplit = [];

for (let i = 0; i < quoteSplitPoints.length; i++) {
if (i != quoteSplitPoints.length - 1) {
quoteSplit.push(
quote.slice(quoteSplitPoints[i], quoteSplitPoints[i + 1])
);
} else {
quoteSplit.push(quote.slice(quoteSplitPoints[i]));
}
}

if (keywords.length > 0) {
if (quoteMatches.length > 0) {
quotes.push(quote);
quotesSplit.push(quoteSplit);
}

matches.push(quoteMatches);
count = count + quoteMatchesCount;
}
});

return { name, count, quotes, quotesSplit };
});
}
Insert cell
totalCount = d3.sum(results, d => d.count)
Insert cell
totalCountFiltered = d3.sum(
results.filter(f =>
speakers.length > 0 ? speakers.includes(f["name"]) : true
),
d => d.count
)
Insert cell
Insert cell
Insert cell
Insert cell
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