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