Public
Edited
Jul 14, 2024
1 star
Insert cell
Insert cell
Insert cell
df = aq.table({
item: ["Q1", "Q1", "Q1", "Q2", "Q2", "Q3", "Q3"],
prop: ["P31", "P27", "P106", "P31", "P69", "P21", "P31"]
})
Insert cell
df.view()
Insert cell
Insert cell
df1 = df.groupby("prop").pivot("item", { prop: (d) => op.valid(d.prop) })
Insert cell
df1.view()
Insert cell
Insert cell
df1
.array("Q1")
.reduce((sum, value, index) => sum + value * df1.array("Q2")[index], 0) /
df1
.array("Q1")
.reduce(
(sum, value, index) => sum + Math.max(value, df1.array("Q2")[index]),
0
)
Insert cell
Insert cell
cols = df1.select(aq.not("prop")).columnNames()
Insert cell
Insert cell
cols.map(
(d) =>
df1
.array(d)
.reduce((sum, value, index) => sum + value * df1.array("Q1")[index], 0) /
df1
.array(d)
.reduce(
(sum, value, index) => sum + Math.max(value, df1.array("Q1")[index]),
0
)
)
Insert cell
Insert cell
cols.reduce((result, d) => {
result[d] =
df1
.array(d)
.reduce((sum, value, index) => sum + value * df1.array("Q1")[index], 0) /
df1
.array(d)
.reduce(
(sum, value, index) => sum + Math.max(value, df1.array("Q1")[index]),
0
);
return result;
}, {})
Insert cell
Insert cell
jaccard = aq.from(
cols.map((e) =>
cols.reduce((result, d) => {
result[d] =
df1
.array(d)
.reduce((sum, value, index) => sum + value * df1.array(e)[index], 0) /
df1
.array(d)
.reduce(
(sum, value, index) => sum + Math.max(value, df1.array(e)[index]),
0
);
return result;
}, {})
)
)
Insert cell
jaccard.view()
Insert cell
Insert cell
query = `SELECT ?item ?itemLabel ?property ?propertyLabel
WITH {
SELECT ?item WHERE {
wd:Q7278 (^(wdt:P279*)/^wdt:P31) ?item.
?item wdt:P17 wd:Q142.
}
LIMIT 10
} AS %PARTIES
WHERE {
{
SELECT DISTINCT ?item ?property WHERE {
INCLUDE %PARTIES
?item ?p ?statement.
?statement ?ps ?ps_.
?property wikibase:claim ?p;
wikibase:statementProperty ?ps.
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en". }
}`
Insert cell
output = fetch(
`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`,
{ headers: { accept: "application/sparql-results+json" } }
).then((response) => response.json())
Insert cell
data = aq
.from(
output.results.bindings.map((d) => ({
item: d.item.value,
itemLabel: d.itemLabel.value,
property: d.property.value,
propertyLabel: d.propertyLabel.value
}))
)
.derive({
propertyId: (d) =>
op.replace(d.property, "http://www.wikidata.org/entity/", ""),

itemId: (d) => op.replace(d.item, "http://www.wikidata.org/entity/", "")
})
.derive({
property2: (d) => d.propertyLabel + " (" + d.propertyId + ")",
item2: (d) => d.itemLabel + " (" + d.itemId + ")"
})
.select("item2", "property2")
Insert cell
data.view()
Insert cell
Insert cell
data_qp = data
.groupby("property2")
.pivot("item2", { prop: (d) => op.valid(d.property2) })
Insert cell
data_qp.view()
Insert cell
Insert cell
qids = data_qp.select(aq.not("property2")).columnNames()
Insert cell
Insert cell
df_jaccard = aq
.from(
qids.map((e) =>
qids.reduce((result, d) => {
result[d] =
data_qp
.array(d)
.reduce(
(sum, value, index) => sum + value * data_qp.array(e)[index],
0
) /
data_qp
.array(d)
.reduce(
(sum, value, index) =>
sum + Math.max(value, data_qp.array(e)[index]),
0
);
return result;
}, {})
)
)
.assign({ qid: qids })
Insert cell
df_jaccard.view()
Insert cell
Insert cell
viewof qq = Inputs.select(qids)
Insert cell
df_jaccard.orderby(aq.desc(qq)).select("qid", qq).view()
Insert cell
Insert cell
import { aq, op } from "@uwdata/arquero"
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