1<?php 2 3use dokuwiki\plugin\aichat\Chunk; 4use dokuwiki\Search\Indexer; 5 6/** 7 * DokuWiki Plugin aichat (Syntax Component) 8 * 9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 10 * @author Andreas Gohr <gohr@cosmocode.de> 11 */ 12class syntax_plugin_aichat_similar extends \dokuwiki\Extension\SyntaxPlugin 13{ 14 /** @inheritDoc */ 15 public function getType() 16 { 17 return 'substition'; 18 } 19 20 /** @inheritDoc */ 21 public function getPType() 22 { 23 return 'block'; 24 } 25 26 /** @inheritDoc */ 27 public function getSort() 28 { 29 return 155; 30 } 31 32 /** @inheritDoc */ 33 public function connectTo($mode) 34 { 35 $this->Lexer->addSpecialPattern('~~similar~~', $mode, 'plugin_aichat_similar'); 36 } 37 38 39 /** @inheritDoc */ 40 public function handle($match, $state, $pos, Doku_Handler $handler) 41 { 42 return []; 43 } 44 45 /** @inheritDoc */ 46 public function render($mode, Doku_Renderer $renderer, $data) 47 { 48 global $INFO; 49 50 /** @var helper_plugin_aichat $helper */ 51 $helper = $this->loadHelper('aichat'); 52 53 $id = $INFO['id']; 54 55 $pages = (new Indexer())->getPages(); 56 $pos = array_search($id, $pages); 57 if($pos === false) return true; 58 59 $storage = $helper->getStorage(); 60 $chunks = $storage->getPageChunks($id, $pos*100); 61 $similar = []; 62 foreach ($chunks as $chunk) { 63 $similar += $storage->getSimilarChunks($chunk->getEmbedding()); 64 } 65 $similar = array_unique($similar); 66 usort($similar, function ($a, $b) { 67 /** @var Chunk $a */ 68 /** @var Chunk $b */ 69 return $b->getScore() <=> $a->getScore(); 70 }); 71 72 if(!$similar) return true; 73 74 $renderer->listu_open(); 75 foreach ($similar as $chunk) { 76 /** @var Chunk $chunk */ 77 $renderer->listitem_open(1); 78 $renderer->listcontent_open(); 79 $renderer->cdata($chunk->getPage()); 80 $renderer->listcontent_close(); 81 $renderer->listitem_close(); 82 } 83 84 $renderer->listu_close(); 85 86 87 return true; 88 } 89} 90 91