35 lines
1.7 KiB
PHP
35 lines
1.7 KiB
PHP
<?php
|
|
// opus-arch-graphrag.php - Cap 17 GraphRAG
|
|
header('Content-Type: application/json');
|
|
$action = $_GET['action'] ?? 'query';
|
|
if ($action === 'build') {
|
|
$srcs = ['/var/www/html/wiki', '/opt/obsidian-vault/doctrines', '/opt/wevads/vault'];
|
|
$ents = []; $rels = [];
|
|
foreach ($srcs as $dir) {
|
|
if (!is_dir($dir)) continue;
|
|
foreach (array_slice(glob($dir.'/*.md'), 0, 50) as $f) {
|
|
$c = file_get_contents($f);
|
|
preg_match_all('/\b([A-Z][A-Z_]{3,})\b/', $c, $m);
|
|
foreach ($m[1] as $e) $ents[$e] = ($ents[$e] ?? 0) + 1;
|
|
preg_match_all('/\b(V\d+|WEVIA|OPUS|YACINE|CLAUDE)\b/', $c, $m2);
|
|
$u = array_unique($m2[1]);
|
|
foreach ($u as $i=>$a) foreach (array_slice($u,$i+1) as $b) $rels[$a.':'.$b] = ($rels[$a.':'.$b] ?? 0) + 1;
|
|
}
|
|
}
|
|
arsort($ents); arsort($rels);
|
|
$g = ['entities_top20'=>array_slice($ents,0,20,true), 'relations_top20'=>array_slice($rels,0,20,true), 'total_e'=>count($ents), 'total_r'=>count($rels)];
|
|
@file_put_contents('/opt/weval-ops/graphrag-index.json', json_encode($g));
|
|
echo json_encode(['ok'=>true,'graph'=>$g]); exit;
|
|
}
|
|
if ($action === 'query') {
|
|
$q = strtoupper($_GET['q'] ?? '');
|
|
$f = '/opt/weval-ops/graphrag-index.json';
|
|
if (!file_exists($f)) { echo json_encode(['ok'=>false,'error'=>'build first']); exit; }
|
|
$g = json_decode(file_get_contents($f), true);
|
|
$em = []; $rm = [];
|
|
foreach (($g['entities_top20'] ?? []) as $k=>$v) if (strpos($k,$q) !== false) $em[$k] = $v;
|
|
foreach (($g['relations_top20'] ?? []) as $k=>$v) if (strpos($k,$q) !== false) $rm[$k] = $v;
|
|
echo json_encode(['ok'=>true,'q'=>$q,'entities'=>$em,'relations'=>$rm]); exit;
|
|
}
|
|
echo json_encode(['ok'=>false,'actions'=>['build','query']]);
|