Case 4. Intelligent navigation over events / timelines

Implementation in pure PHP

Goal: quickly find the most relevant events in a timeline by meaning, not by exact keyword matches. We compute embeddings for event descriptions, cache them to disk, and rank events using cosine similarity against the query.

 
<?php

require_once __DIR__ '/code.php';

$events = [
    [
        
'id' => 1,
        
'title' => 'Restrictions on technology corporations',
        
'description' => 'New economic measures were introduced against major technology companies.',
    ],
    [
        
'id' => 2,
        
'title' => 'Expansion of space programs',
        
'description' => 'Several countries in the region increased funding for national satellite initiatives.',
    ],
    [
        
'id' => 3,
        
'title' => 'Escalation of political conflict',
        
'description' => 'Political tensions intensified across several provinces.',
    ],
    [
        
'id' => 4,
        
'title' => 'Restrictions on the IT sector',
        
'description' => 'The government announced new restrictions for companies operating in information technology.',
    ],
    [
        
'id' => 5,
        
'title' => 'Inflation growth and key rate revision',
        
'description' => 'The central bank raised the key rate amid faster inflation and rising import prices.',
    ],
    [
        
'id' => 6,
        
'title' => 'Launch of a small business support program',
        
'description' => 'Authorities announced subsidized loans and tax relief for small and medium businesses in the regions.',
    ],
    [
        
'id' => 8,
        
'title' => 'Data leak in online retail',
        
'description' => 'An online store is investigating a customer data leak after employee accounts were compromised.',
    ],
    [
        
'id' => 9,
        
'title' => 'Medical breakthrough: new diagnostic method',
        
'description' => 'Researchers presented an early-detection method based on biomarkers that reduces analysis time.',
    ],
    [
        
'id' => 10,
        
'title' => 'Seasonal rise in illness cases',
        
'description' => 'Several cities reported a rise in respiratory infections, and clinics increased patient intake capacity.',
    ],
    [
        
'id' => 12,
        
'title' => 'Drought and agricultural risks',
        
'description' => 'Due to prolonged drought, farmers forecast lower yields while support measures are being discussed.',
    ],
    [
        
'id' => 13,
        
'title' => 'Final of a major sports tournament',
        
'description' => 'In the season-deciding match, a team won in extra time and set a new attendance record.',
    ],
    [
        
'id' => 14,
        
'title' => 'Player transfer strengthens lineup',
        
'description' => 'The club signed a new striker to reinforce attack ahead of an upcoming derby series.',
    ],
    [
        
'id' => 15,
        
'title' => 'New regulations for marketplaces',
        
'description' => 'The regulator proposed requirements for product labeling and transparent commissions on online trading platforms.',
    ],
    [
        
'id' => 17,
        
'title' => 'Semiconductor supply disruptions',
        
'description' => 'Electronics manufacturers warned of chip delivery delays due to export controls and factory overload.',
    ],
    [
        
'id' => 18,
        
'title' => 'Opening of a contemporary art festival',
        
'description' => 'A contemporary art festival opened in the capital with exhibitions, performances, and artist talks.',
    ],
    [
        
'id' => 19,
        
'title' => 'Major real estate market deal',
        
'description' => 'An investment fund acquired a commercial real estate portfolio for reconstruction and energy-efficiency upgrades.',
    ],
    [
        
'id' => 20,
        
'title' => 'Ocean research reveals new data',
        
'description' => 'A scientific expedition gathered current and water temperature data, refining climate change forecasts.',
    ],
];

$query 'sanctions against IT companies';

$search = new SemanticEventSearch(topN3cachePath__DIR__ '/../intelligent-timelines/embeddings.events.json');
$search->setModel('Xenova/paraphrase-multilingual-MiniLM-L12-v2');
$search->setEvents($events);
$search->setQuery($query);
$out $search->run();
$search->render($out['query'], $out['results']);
Result: Memory: 0.001 Mb Time running: < 0.001 sec.
Query: sanctions against IT companies

[0.4288] #4 Restrictions on the IT sector
The government announced new restrictions for companies operating in information technology.

[0.3356] #15 New regulations for marketplaces
The regulator proposed requirements for product labeling and transparent commissions on online trading platforms.

[0.2598] #8 Data leak in online retail
An online store is investigating a customer data leak after employee accounts were compromised.