Skip to content

Thematic retrieval

The memory keeps no items, so retrieval runs on the History. Every output on this page comes from reading nine years of the Wikipedia Current Events Portal (2016–2024, 49,090 items, CC BY-SA) with Memory(); see Example data. hours_at and day_of convert between dates and the stream’s hours.

Write the theme as a short query, encode and project it like any item, and ask which nodes match it around a date:

query = memory.project(encoder.encode(["Russian invasion of Ukraine"])[0])
history.query(query, time=hours_at("2022-02-24"), k=3)
Output
[(5073, 0.6188776843033941), (4802, 0.5125656839229904), (5038, 0.46890935325831506)]

Only items from a week before to thirty days after the date count (before=168, after=720, in hours), and a node’s centre is the mean of its items in that window, so the answer reflects the story as it was then. Without time, the whole stream counts.

Four events, with the cosine of the second-best node for comparison:

events = [("2020-03-11", "Coronavirus COVID-19 pandemic"),
("2022-02-24", "Russian invasion of Ukraine"),
("2023-10-07", "Hamas attack on Israel and war in Gaza"),
("2016-11-08", "United States presidential election")]
queries = memory.project(encoder.encode([q for _, q in events]))
for (day, text), q in zip(events, queries):
(node, cosine), (_, second) = history.query(q, time=hours_at(day), k=2)
items = history.items(node)
print(f"{text[:38]:<38} node {node:>5} cosine {cosine:.3f} (next {second:.3f}) "
f"born {day_of(hours[items[0]])} {len(items):>5} items")
Output
Coronavirus COVID-19 pandemic          node  3629  cosine 0.665 (next 0.629)  born 2020-02-10    161 items
Russian invasion of Ukraine            node  5073  cosine 0.619 (next 0.513)  born 2022-02-08    495 items
Hamas attack on Israel and war in Gaza node  6207  cosine 0.641 (next 0.581)  born 2023-10-07    278 items
United States presidential election    node   760  cosine 0.726 (next 0.581)  born 2016-10-05     19 items

The first three nodes were born before or on the day of their event: the COVID-19 node on 10 February 2020, a month before the pandemic was declared; the Ukraine node in the prelude, two weeks before the invasion; the Gaza node on the day of the attack. The fourth is a miss, and worth looking at.

The 2016 United States election finds a node with a high cosine, 0.73, but only 19 items. Its exemplars show what it is:

node = history.query(queries[3], time=hours_at("2016-11-08"), k=1)[0][0]
for i in history.exemplars(node, central=3, diverse=0):
print(day_of(hours[i]), texts[i][:110])
Output
2017-04-28 Ilir Meta is elected the new President of Albania, winning 87 votes out of 140 members of the Assembly.
2017-03-13 János Áder is re-elected President of Hungary by the National Assembly.
2017-01-17 Antonio Tajani has been elected as new President of the European Parliament.

A node of presidents elected elsewhere. The query matched the kind of news, the election of a president, rather than the American election. Two signs give such cases away: a node that is small for the event it should hold, and exemplars that do not mention it. Always read the exemplars.

History.exemplars returns the items closest to a node’s recency-weighted centre, then a few chosen for diversity by maximal marginal relevance, skipping near-duplicates (the same report under several outlets). until restricts the node to its items up to a time, so the exemplars describe the story as it was then rather than at its end:

ukraine = history.query(queries[1], time=hours_at("2022-02-24"), k=1)[0][0]
for i in history.exemplars(ukraine, central=3, diverse=2, until=hours_at("2022-03-26")):
print(day_of(hours[i]), texts[i][:110])
Output
2022-02-21 Russia says that it has killed five Ukrainian soldiers who tried to infiltrate its territory. Ukraine disputes
2022-03-02 Russian Ground Forces say that they have captured the Black Sea port of Kherson. The Ukrainian Ministry of Def
2022-02-24 Russian troops enter the Chernobyl Exclusion Zone. Ukraine says that Russian forces have captured the Chernoby
2022-03-20 Bulgaria says it will end gas imports from Russia's Gazprom when its 10-year deal with Gazprom expires at the 
2022-02-24 Russia says that two Russian civilian ships have been hit by anti-ship missiles in the Sea of Azov, causing mu

The first three are central: the fighting itself. The last two are the diverse ones and show what else the node had gathered within a month: energy sanctions, the war at sea.

History.active gives the memory’s working set at a moment: the nodes whose forgotten mass is still above a threshold, which could receive the next item.

alive = history.active(hours_at("2022-03-01"), min_mass=0.1)
print(len(alive), "nodes alive on 2022-03-01; the five heaviest:")
for node, mass in sorted(alive.items(), key=lambda x: -x[1])[:5]:
first = history.exemplars(node, central=1, diverse=0, until=hours_at("2022-03-01"))[0]
print(f"node {node:>5} mass {mass:5.1f} {texts[first][:80]}")
Output
56 nodes alive on 2022-03-01; the five heaviest:
node  5073  mass  52.6  Russian troops enter the Chernobyl Exclusion Zone. Ukraine says that Russian for
node  4988  mass  12.4  Canada, the European Union, the United Kingdom, the United States and Japan free
node  4802  mass   9.2  Russian forces strike the Kyiv TV Tower with a missile, causing a temporary disr
node  5026  mass   6.6  Kremlin Press Secretary Dmitry Peskov says that a Russian delegation has arrived
node  4972  mass   4.1  Hong Kong reports a record 1,325 new COVID-19 cases in the past 24 hours, bringi

Five days into the invasion, the war node holds a mass of 53, four times the next one, and the other heavy nodes are its satellites: sanctions, the strikes on Kyiv, the negotiations. Below them, ordinary news goes on, here a COVID-19 wave in Hong Kong.

During reading, Memory.nearest compares a query with the centres of the active nodes as they are now. At the end of the stream, three weeks after the fall of the Assad government:

memory.nearest(encoder.encode(["Fall of the Assad regime in Syria"])[0], k=3)
Output
[(5153, 0.4431310320241748), (7044, 0.428990885565141), (7043, 0.397418678937023)]
node = memory.nearest(encoder.encode(["Fall of the Assad regime in Syria"])[0], k=1)[0][0]
for i in history.exemplars(node, central=2, diverse=0):
print(day_of(hours[i]), texts[i][:110])
Output
2024-12-31 Ten Islamic State militants are killed by Puntland forces in an attempted attack involving suicide bombers on 
2024-12-19 The Military Operations Command forces take control of several towns in Raqqah Governorate, Syria.

The best match is a broad node of armed conflict that has been alive since March 2022, with Syria among many fronts; its cosine, 0.44, is low. The live memory at a given moment knows only its active nodes, and a broad, long-lived node can absorb an event that a reader would consider separate. Timelines and the explorer help to see such nodes.