101f06932SAndreas Gohr<?php 201f06932SAndreas Gohr 301f06932SAndreas Gohruse dokuwiki\plugin\aichat\Chunk; 401f06932SAndreas Gohruse dokuwiki\Search\Indexer; 501f06932SAndreas Gohr 601f06932SAndreas Gohr/** 701f06932SAndreas Gohr * DokuWiki Plugin aichat (Syntax Component) 801f06932SAndreas Gohr * 901f06932SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 1001f06932SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1101f06932SAndreas Gohr */ 1201f06932SAndreas Gohrclass syntax_plugin_aichat_similar extends \dokuwiki\Extension\SyntaxPlugin 1301f06932SAndreas Gohr{ 1401f06932SAndreas Gohr /** @inheritDoc */ 1501f06932SAndreas Gohr public function getType() 1601f06932SAndreas Gohr { 1701f06932SAndreas Gohr return 'substition'; 1801f06932SAndreas Gohr } 1901f06932SAndreas Gohr 2001f06932SAndreas Gohr /** @inheritDoc */ 2101f06932SAndreas Gohr public function getPType() 2201f06932SAndreas Gohr { 2301f06932SAndreas Gohr return 'block'; 2401f06932SAndreas Gohr } 2501f06932SAndreas Gohr 2601f06932SAndreas Gohr /** @inheritDoc */ 2701f06932SAndreas Gohr public function getSort() 2801f06932SAndreas Gohr { 2901f06932SAndreas Gohr return 155; 3001f06932SAndreas Gohr } 3101f06932SAndreas Gohr 3201f06932SAndreas Gohr /** @inheritDoc */ 3301f06932SAndreas Gohr public function connectTo($mode) 3401f06932SAndreas Gohr { 3501f06932SAndreas Gohr $this->Lexer->addSpecialPattern('~~similar~~', $mode, 'plugin_aichat_similar'); 3601f06932SAndreas Gohr } 3701f06932SAndreas Gohr 3801f06932SAndreas Gohr 3901f06932SAndreas Gohr /** @inheritDoc */ 4001f06932SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 4101f06932SAndreas Gohr { 4201f06932SAndreas Gohr return []; 4301f06932SAndreas Gohr } 4401f06932SAndreas Gohr 4501f06932SAndreas Gohr /** @inheritDoc */ 4601f06932SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) 4701f06932SAndreas Gohr { 4801f06932SAndreas Gohr global $INFO; 4901f06932SAndreas Gohr 5001f06932SAndreas Gohr /** @var helper_plugin_aichat $helper */ 5101f06932SAndreas Gohr $helper = $this->loadHelper('aichat'); 5201f06932SAndreas Gohr 5301f06932SAndreas Gohr $id = $INFO['id']; 5401f06932SAndreas Gohr 5501f06932SAndreas Gohr $pages = (new Indexer())->getPages(); 5601f06932SAndreas Gohr $pos = array_search($id, $pages); 5701f06932SAndreas Gohr if($pos === false) return true; 5801f06932SAndreas Gohr 5901f06932SAndreas Gohr $storage = $helper->getStorage(); 6001f06932SAndreas Gohr $chunks = $storage->getPageChunks($id, $pos*100); 6101f06932SAndreas Gohr $similar = []; 6201f06932SAndreas Gohr foreach ($chunks as $chunk) { 63*2ad4d158SAndreas Gohr $similar += $storage->getSimilarChunks($chunk->getEmbedding(), 10); 6401f06932SAndreas Gohr } 6501f06932SAndreas Gohr $similar = array_unique($similar); 66*2ad4d158SAndreas Gohr $similar = array_filter($similar, function ($chunk) use ($id) { 67*2ad4d158SAndreas Gohr return $chunk->getPage() !== $id; 68*2ad4d158SAndreas Gohr }); 6901f06932SAndreas Gohr usort($similar, function ($a, $b) { 7001f06932SAndreas Gohr /** @var Chunk $a */ 7101f06932SAndreas Gohr /** @var Chunk $b */ 7201f06932SAndreas Gohr return $b->getScore() <=> $a->getScore(); 7301f06932SAndreas Gohr }); 7401f06932SAndreas Gohr 7501f06932SAndreas Gohr if(!$similar) return true; 7601f06932SAndreas Gohr 77*2ad4d158SAndreas Gohr $similar = array_slice($similar, 0, 5); 78*2ad4d158SAndreas Gohr 7901f06932SAndreas Gohr $renderer->listu_open(); 8001f06932SAndreas Gohr foreach ($similar as $chunk) { 8101f06932SAndreas Gohr /** @var Chunk $chunk */ 8201f06932SAndreas Gohr $renderer->listitem_open(1); 8301f06932SAndreas Gohr $renderer->listcontent_open(); 84*2ad4d158SAndreas Gohr $renderer->internallink($chunk->getPage(), null, null, false, 'navigation'); 8501f06932SAndreas Gohr $renderer->listcontent_close(); 8601f06932SAndreas Gohr $renderer->listitem_close(); 8701f06932SAndreas Gohr } 8801f06932SAndreas Gohr $renderer->listu_close(); 8901f06932SAndreas Gohr 9001f06932SAndreas Gohr return true; 9101f06932SAndreas Gohr } 9201f06932SAndreas Gohr} 9301f06932SAndreas Gohr 94