lwazi 1.8.3 → 1.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Console/BuildContentIndexCommand.php +84 -0
- package/src/Installer/ProjectAnalyzer.php +15 -1
- package/src/Providers/LwaziServiceProvider.php +2 -0
- package/src/Services/ContentIndexer.php +30 -6
- package/src/Services/KnowledgeBaseGenerator.php +16 -1
- package/src/Services/LwaziService.php +75 -27
- package/src/Services/NavigationTree.php +43 -5
package/package.json
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
|
|
3
|
+
namespace Lwazi\Core\Console;
|
|
4
|
+
|
|
5
|
+
use Illuminate\Console\Command;
|
|
6
|
+
use Illuminate\Support\Facades\File;
|
|
7
|
+
use Lwazi\Core\Services\ContentIndexer;
|
|
8
|
+
|
|
9
|
+
class BuildContentIndexCommand extends Command
|
|
10
|
+
{
|
|
11
|
+
protected $signature = 'lwazi:build-content-index';
|
|
12
|
+
protected $description = 'Build content index from blade files for website search';
|
|
13
|
+
|
|
14
|
+
public function handle(): int
|
|
15
|
+
{
|
|
16
|
+
$this->info('Building content index from blade files...');
|
|
17
|
+
|
|
18
|
+
$viewsPath = resource_path('views');
|
|
19
|
+
if (!is_dir($viewsPath)) {
|
|
20
|
+
$this->error('Views directory not found');
|
|
21
|
+
return Command::FAILURE;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
$indexer = new ContentIndexer();
|
|
25
|
+
$this->indexBladeFiles($viewsPath, $indexer);
|
|
26
|
+
|
|
27
|
+
$docCount = $indexer->getDocumentCount();
|
|
28
|
+
$this->info('Indexed ' . $docCount . ' pages');
|
|
29
|
+
|
|
30
|
+
$data = $indexer->toArray();
|
|
31
|
+
$data['generated_at'] = now()->toIso8601String();
|
|
32
|
+
|
|
33
|
+
$path = storage_path('lwazi/content_index.json');
|
|
34
|
+
if (!is_dir(dirname($path))) {
|
|
35
|
+
mkdir(dirname($path), 0755, true);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
|
39
|
+
|
|
40
|
+
$this->info("Content index saved to: {$path}");
|
|
41
|
+
|
|
42
|
+
return Command::SUCCESS;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected function indexBladeFiles(string $path, ContentIndexer $indexer): void
|
|
46
|
+
{
|
|
47
|
+
$files = File::allFiles($path);
|
|
48
|
+
|
|
49
|
+
foreach ($files as $file) {
|
|
50
|
+
$ext = $file->getExtension();
|
|
51
|
+
if ($ext !== 'blade.php' && $ext !== 'php') {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
$filePath = $file->getPathname();
|
|
56
|
+
$relativePath = str_replace($path . '/', '', $filePath);
|
|
57
|
+
|
|
58
|
+
if (str_contains($relativePath, 'admin/') || str_contains($relativePath, '/admin.')) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
$content = File::get($filePath);
|
|
63
|
+
|
|
64
|
+
$content = preg_replace('/@php.*?@endphp/s', '', $content);
|
|
65
|
+
$content = preg_replace('/@section.*?@endsection/s', '', $content);
|
|
66
|
+
$content = preg_replace('/@yield.*?(?=\s|$)/', '', $content);
|
|
67
|
+
$content = preg_replace('/\{\{.*?\}\}/', '', $content);
|
|
68
|
+
$content = preg_replace('/\{!!.*?!!\}/', '', $content);
|
|
69
|
+
$content = strip_tags($content);
|
|
70
|
+
$content = preg_replace('/\s+/', ' ', $content);
|
|
71
|
+
$content = trim($content);
|
|
72
|
+
|
|
73
|
+
if (strlen($content) < 20) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
$pathName = '/' . str_replace(['.blade.php', '.php', '/'], ['', '', '-'], $relativePath);
|
|
78
|
+
$pathName = preg_replace('/-+/', '-', $pathName);
|
|
79
|
+
$pathName = rtrim($pathName, '-') ?: '/';
|
|
80
|
+
|
|
81
|
+
$indexer->indexPage($pathName, '<html><body><p>' . $content . '</p></body></html>');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -338,8 +338,22 @@ class ProjectAnalyzer
|
|
|
338
338
|
$pages = $this->knowledge['pages'] ?? [];
|
|
339
339
|
$routes = $this->knowledge['routes'] ?? [];
|
|
340
340
|
|
|
341
|
+
$publicRoutes = array_filter($routes, function($r) {
|
|
342
|
+
$uri = $r['uri'] ?? '';
|
|
343
|
+
$name = $r['name'] ?? '';
|
|
344
|
+
|
|
345
|
+
if (str_starts_with($uri, '/admin') || str_starts_with($uri, '/api') || str_starts_with($uri, '/_')) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
if ($name && (str_starts_with($name, 'admin.') || str_starts_with($name, 'api.'))) {
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return true;
|
|
353
|
+
});
|
|
354
|
+
|
|
341
355
|
$tree->buildFromPages($pages);
|
|
342
|
-
$tree->buildFromRoutes($
|
|
356
|
+
$tree->buildFromRoutes(array_values($publicRoutes));
|
|
343
357
|
|
|
344
358
|
$this->knowledge['navigation_tree'] = $tree->toArray();
|
|
345
359
|
|
|
@@ -13,6 +13,7 @@ use Lwazi\Core\Services\LwaziAgent;
|
|
|
13
13
|
use Lwazi\Core\Console\AnalyzeProjectCommand;
|
|
14
14
|
use Lwazi\Core\Console\LwaziIngestCommand;
|
|
15
15
|
use Lwazi\Core\Console\SetupCommand;
|
|
16
|
+
use Lwazi\Core\Console\BuildContentIndexCommand;
|
|
16
17
|
use Lwazi\Core\Http\Middleware\InjectLwaziChat;
|
|
17
18
|
|
|
18
19
|
class LwaziServiceProvider extends ServiceProvider
|
|
@@ -36,6 +37,7 @@ class LwaziServiceProvider extends ServiceProvider
|
|
|
36
37
|
AnalyzeProjectCommand::class,
|
|
37
38
|
SetupCommand::class,
|
|
38
39
|
LwaziIngestCommand::class,
|
|
40
|
+
BuildContentIndexCommand::class,
|
|
39
41
|
]);
|
|
40
42
|
}
|
|
41
43
|
}
|
|
@@ -184,13 +184,31 @@ class ContentIndexer
|
|
|
184
184
|
|
|
185
185
|
protected function generateSnippet(array $doc, array $terms): string
|
|
186
186
|
{
|
|
187
|
-
|
|
187
|
+
if (!is_array($doc)) {
|
|
188
|
+
return '';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
$paragraphs = $doc['paragraphs'] ?? null;
|
|
192
|
+
$text = '';
|
|
188
193
|
|
|
189
|
-
if (
|
|
190
|
-
$
|
|
194
|
+
if (is_array($paragraphs) && count($paragraphs) > 0) {
|
|
195
|
+
$first = $paragraphs[0];
|
|
196
|
+
if (is_string($first)) {
|
|
197
|
+
$text = implode(' ', $paragraphs);
|
|
198
|
+
}
|
|
191
199
|
}
|
|
192
200
|
|
|
193
|
-
if (
|
|
201
|
+
if ($text === '') {
|
|
202
|
+
$lists = $doc['lists'] ?? null;
|
|
203
|
+
if (is_array($lists) && count($lists) > 0) {
|
|
204
|
+
$first = $lists[0];
|
|
205
|
+
if (is_string($first)) {
|
|
206
|
+
$text = implode(' ', $lists);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if ($text === '' || !is_string($text)) {
|
|
194
212
|
return '';
|
|
195
213
|
}
|
|
196
214
|
|
|
@@ -199,6 +217,7 @@ class ContentIndexer
|
|
|
199
217
|
$bestLen = PHP_INT_MAX;
|
|
200
218
|
|
|
201
219
|
foreach ($terms as $term) {
|
|
220
|
+
if (!is_string($term)) continue;
|
|
202
221
|
$term = strtolower($term);
|
|
203
222
|
$pos = strpos($text, $term);
|
|
204
223
|
if ($pos !== false && $pos < $bestLen) {
|
|
@@ -218,9 +237,9 @@ class ContentIndexer
|
|
|
218
237
|
$snippet = '...' . $snippet;
|
|
219
238
|
}
|
|
220
239
|
|
|
221
|
-
$snippet = preg_replace('/\s+/', ' ', $snippet);
|
|
240
|
+
$snippet = preg_replace('/\s+/', ' ', $snippet ?? '');
|
|
222
241
|
|
|
223
|
-
return trim($snippet) . '...';
|
|
242
|
+
return (is_string($snippet) ? trim($snippet) : '') . '...';
|
|
224
243
|
}
|
|
225
244
|
|
|
226
245
|
public function toArray(): array
|
|
@@ -233,6 +252,11 @@ class ContentIndexer
|
|
|
233
252
|
];
|
|
234
253
|
}
|
|
235
254
|
|
|
255
|
+
public function getDocumentCount(): int
|
|
256
|
+
{
|
|
257
|
+
return $this->totalDocuments;
|
|
258
|
+
}
|
|
259
|
+
|
|
236
260
|
public static function fromArray(array $data): self
|
|
237
261
|
{
|
|
238
262
|
$indexer = new self();
|
|
@@ -50,8 +50,23 @@ class KnowledgeBaseGenerator
|
|
|
50
50
|
protected function buildNavigationTree(array $pages, array $routes): array
|
|
51
51
|
{
|
|
52
52
|
$tree = new NavigationTree();
|
|
53
|
+
|
|
54
|
+
$publicRoutes = array_filter($routes, function($r) {
|
|
55
|
+
$uri = $r['uri'] ?? '';
|
|
56
|
+
$name = $r['name'] ?? '';
|
|
57
|
+
|
|
58
|
+
if (str_starts_with($uri, '/admin') || str_starts_with($uri, '/api') || str_starts_with($uri, '/_')) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if ($name && (str_starts_with($name, 'admin.') || str_starts_with($name, 'api.'))) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
});
|
|
67
|
+
|
|
53
68
|
$tree->buildFromPages($pages);
|
|
54
|
-
$tree->buildFromRoutes($
|
|
69
|
+
$tree->buildFromRoutes(array_values($publicRoutes));
|
|
55
70
|
return $tree->toArray();
|
|
56
71
|
}
|
|
57
72
|
|
|
@@ -45,7 +45,7 @@ class LwaziService
|
|
|
45
45
|
|
|
46
46
|
$this->conversationHistory[] = ['role' => 'user', 'content' => $message];
|
|
47
47
|
|
|
48
|
-
if ($this->looksLikeNavigationQuery($message)) {
|
|
48
|
+
if ($this->looksLikeNavigationQuery($message) && !$this->looksLikeDataQuery($message)) {
|
|
49
49
|
error_log('Lwazi: looks like navigation query');
|
|
50
50
|
$nav = $this->findNavigationAnswer($message);
|
|
51
51
|
if ($nav) {
|
|
@@ -53,11 +53,10 @@ class LwaziService
|
|
|
53
53
|
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $nav];
|
|
54
54
|
return $nav;
|
|
55
55
|
}
|
|
56
|
-
$
|
|
57
|
-
if ($
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return $fallback;
|
|
56
|
+
$contentResponse = $this->searchContent($message);
|
|
57
|
+
if ($contentResponse) {
|
|
58
|
+
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
|
|
59
|
+
return $contentResponse;
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
62
|
|
|
@@ -70,27 +69,12 @@ class LwaziService
|
|
|
70
69
|
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $nav];
|
|
71
70
|
return $nav;
|
|
72
71
|
}
|
|
73
|
-
$fallback = $this->navigationFallbackWithLLM($message);
|
|
74
|
-
if ($fallback) {
|
|
75
|
-
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $fallback];
|
|
76
|
-
return $fallback;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if ($intent === 'content' || $intent === 'navigation') {
|
|
81
72
|
$contentResponse = $this->searchContent($message);
|
|
82
73
|
if ($contentResponse) {
|
|
83
74
|
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
|
|
84
75
|
return $contentResponse;
|
|
85
76
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if ($intent === 'navigation') {
|
|
89
|
-
$nav = $this->navigationFallbackWithLLM($message);
|
|
90
|
-
if ($nav) {
|
|
91
|
-
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $nav];
|
|
92
|
-
return $nav;
|
|
93
|
-
}
|
|
77
|
+
return "I couldn't find a page matching that. Could you try a different question?";
|
|
94
78
|
}
|
|
95
79
|
|
|
96
80
|
if ($intent === 'content') {
|
|
@@ -99,6 +83,7 @@ class LwaziService
|
|
|
99
83
|
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
|
|
100
84
|
return $contentResponse;
|
|
101
85
|
}
|
|
86
|
+
return "I couldn't find information about that. Could you try a different question?";
|
|
102
87
|
}
|
|
103
88
|
|
|
104
89
|
if ($intent === 'data') {
|
|
@@ -107,6 +92,12 @@ class LwaziService
|
|
|
107
92
|
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $dataResponse];
|
|
108
93
|
return $dataResponse;
|
|
109
94
|
}
|
|
95
|
+
$contentResponse = $this->searchContent($message);
|
|
96
|
+
if ($contentResponse) {
|
|
97
|
+
$this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
|
|
98
|
+
return $contentResponse;
|
|
99
|
+
}
|
|
100
|
+
return "I couldn't find information about that. Could you try a different question?";
|
|
110
101
|
}
|
|
111
102
|
|
|
112
103
|
$prompt = $this->buildPrompt();
|
|
@@ -123,16 +114,26 @@ class LwaziService
|
|
|
123
114
|
|
|
124
115
|
protected function selectIntentWithLLM(string $message): ?string
|
|
125
116
|
{
|
|
126
|
-
if ($this->
|
|
117
|
+
if ($this->looksLikeDataQuery($message)) {
|
|
118
|
+
return 'data';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if ($this->looksLikeNavigationQuery($message) && !$this->looksLikeContentQuery($message)) {
|
|
127
122
|
return 'navigation';
|
|
128
123
|
}
|
|
129
124
|
|
|
125
|
+
$msgLower = strtolower($message);
|
|
126
|
+
|
|
127
|
+
if (preg_match('/\b(is there|are there|do i have|can i find|show me|find|get|list|what.*contain|information about|info about)\b/i', $message)) {
|
|
128
|
+
return 'content';
|
|
129
|
+
}
|
|
130
|
+
|
|
130
131
|
$prompt =
|
|
131
132
|
"Classify the user's intent as one of: navigation, content, data, general. Return JSON: {\"intent\":\"navigation|content|data|general\"}.\n\n" .
|
|
132
|
-
"- navigation: user wants to find a page or link\n" .
|
|
133
|
-
"- content: user is asking for information that might be on a
|
|
134
|
-
"- data: user wants to query database records\n" .
|
|
135
|
-
"- general: casual conversation\n\n" .
|
|
133
|
+
"- navigation: user wants to find a specific page or link (where, which page, how do i get to)\n" .
|
|
134
|
+
"- content: user is asking for information that might be on a webpage, NOT database data (is there, are there, show me, what does X contain)\n" .
|
|
135
|
+
"- data: user explicitly wants to query database records about their own data (my data, my records, my account)\n" .
|
|
136
|
+
"- general: casual conversation, greetings\n\n" .
|
|
136
137
|
"QUESTION:\n" .
|
|
137
138
|
$message;
|
|
138
139
|
|
|
@@ -155,6 +156,33 @@ class LwaziService
|
|
|
155
156
|
return null;
|
|
156
157
|
}
|
|
157
158
|
|
|
159
|
+
protected function looksLikeDataQuery(string $message): bool
|
|
160
|
+
{
|
|
161
|
+
$msgLower = strtolower($message);
|
|
162
|
+
|
|
163
|
+
$personalPatterns = [
|
|
164
|
+
'/\bmy\b/', '/\bme\b/', '/\bmine\b/', '/\bmy own\b/',
|
|
165
|
+
'/\bmy account\b/', '/\bmy profile\b/', '/\bmy data\b/',
|
|
166
|
+
'/\bmy records\b/', '/\bmy applications\b/', '/\bmy submissions\b/',
|
|
167
|
+
];
|
|
168
|
+
|
|
169
|
+
foreach ($personalPatterns as $pattern) {
|
|
170
|
+
if (preg_match($pattern, $msgLower)) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
protected function looksLikeContentQuery(string $message): bool
|
|
179
|
+
{
|
|
180
|
+
return (bool) preg_match(
|
|
181
|
+
'/\b(is there|are there|can i find|show me|what.*contain|information about|passed|failed|grade|result|score|check if)\b/i',
|
|
182
|
+
$message
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
158
186
|
public function looksLikeNavigationQuery(string $message): bool
|
|
159
187
|
{
|
|
160
188
|
return (bool) preg_match(
|
|
@@ -324,6 +352,20 @@ class LwaziService
|
|
|
324
352
|
|
|
325
353
|
protected function findBestRouteWithTerms(array $routes, array $terms): ?string
|
|
326
354
|
{
|
|
355
|
+
$filtered = array_filter($routes, function($r) {
|
|
356
|
+
$uri = $r['uri'] ?? '';
|
|
357
|
+
if (str_starts_with($uri, '/admin') || str_starts_with($uri, '/api') || str_starts_with($uri, '/_')) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
return true;
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
$routes = array_values($filtered);
|
|
364
|
+
|
|
365
|
+
if (empty($routes)) {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
327
369
|
$scored = [];
|
|
328
370
|
$termSet = array_filter(array_map('strtolower', array_map('trim', $terms)), fn($t) => strlen($t) >= 3);
|
|
329
371
|
|
|
@@ -593,6 +635,12 @@ class LwaziService
|
|
|
593
635
|
|
|
594
636
|
protected function fetchRelevantData(string $message): ?string
|
|
595
637
|
{
|
|
638
|
+
if ($this->isPersonalQuery($message) && empty($this->currentContextId)) {
|
|
639
|
+
$context = $this->ragService->getContextConfig();
|
|
640
|
+
$contextParam = $context['param'] ?? config('lwazi.context_param', 'context_id');
|
|
641
|
+
return "I can look that up for you, but I need your {$contextParam} to access your personal information.";
|
|
642
|
+
}
|
|
643
|
+
|
|
596
644
|
$fieldInfo = $this->selectFieldWithLLM($message) ?: $this->ragService->findFieldForQuery($message);
|
|
597
645
|
|
|
598
646
|
if ($fieldInfo) {
|
|
@@ -126,6 +126,10 @@ class NavigationTree
|
|
|
126
126
|
$heading = $page['heading'] ?? '';
|
|
127
127
|
$links = $page['links'] ?? [];
|
|
128
128
|
|
|
129
|
+
if (preg_match('#/admin/#', $file) || preg_match('#/admin\.blade\.php$#', $file)) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
129
133
|
$labels = array_filter([$title, $heading]);
|
|
130
134
|
$primaryLabel = reset($labels) ?: basename($file, '.blade.php');
|
|
131
135
|
|
|
@@ -137,6 +141,10 @@ class NavigationTree
|
|
|
137
141
|
continue;
|
|
138
142
|
}
|
|
139
143
|
|
|
144
|
+
if (str_starts_with($href, '/admin') || str_starts_with($href, '/api') || str_starts_with($href, '/_')) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
140
148
|
$this->addPath($href, $text, $primaryLabel);
|
|
141
149
|
}
|
|
142
150
|
}
|
|
@@ -221,6 +229,11 @@ class NavigationTree
|
|
|
221
229
|
{
|
|
222
230
|
$queryNormalized = $this->normalizeText($query);
|
|
223
231
|
$queryTokens = $this->tokenize($query);
|
|
232
|
+
|
|
233
|
+
if (count($queryTokens) === 0) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
|
|
224
237
|
$expandedTokens = $this->expandWithSynonyms($queryTokens);
|
|
225
238
|
|
|
226
239
|
$candidates = [];
|
|
@@ -238,7 +251,11 @@ class NavigationTree
|
|
|
238
251
|
|
|
239
252
|
usort($candidates, fn($a, $b) => $b['score'] <=> $a['score']);
|
|
240
253
|
|
|
241
|
-
|
|
254
|
+
if (empty($candidates) || $candidates[0]['score'] < 15) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return $candidates[0];
|
|
242
259
|
}
|
|
243
260
|
|
|
244
261
|
public function searchTree(string $query): ?array
|
|
@@ -326,16 +343,19 @@ class NavigationTree
|
|
|
326
343
|
$labelNormalized = $entry['normalizedLabel'] ?? $this->normalizeText($entry['label'] ?? '');
|
|
327
344
|
|
|
328
345
|
foreach ($queryTokens as $token) {
|
|
346
|
+
if (strlen($token) < 3) continue;
|
|
347
|
+
|
|
329
348
|
if ($token === $labelNormalized) {
|
|
330
349
|
$score += 20;
|
|
331
|
-
} elseif (str_contains($labelNormalized, $token)) {
|
|
350
|
+
} elseif (strlen($token) >= 4 && str_contains($labelNormalized, $token)) {
|
|
332
351
|
$score += 10;
|
|
333
|
-
} elseif ($this->fuzzyMatch($labelNormalized, $token)) {
|
|
352
|
+
} elseif (strlen($token) >= 4 && $this->fuzzyMatch($labelNormalized, $token)) {
|
|
334
353
|
$score += 15;
|
|
335
354
|
}
|
|
336
355
|
}
|
|
337
356
|
|
|
338
357
|
foreach ($expandedTokens as $token) {
|
|
358
|
+
if (strlen($token) < 4) continue;
|
|
339
359
|
if (str_contains($labelNormalized, $token)) {
|
|
340
360
|
$score += 5;
|
|
341
361
|
}
|
|
@@ -369,13 +389,27 @@ class NavigationTree
|
|
|
369
389
|
return true;
|
|
370
390
|
}
|
|
371
391
|
|
|
392
|
+
$commonPrefix = 0;
|
|
393
|
+
$minLen = min(strlen($a), strlen($b));
|
|
394
|
+
for ($i = 0; $i < $minLen; $i++) {
|
|
395
|
+
if ($a[$i] === $b[$i]) {
|
|
396
|
+
$commonPrefix++;
|
|
397
|
+
} else {
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if ($commonPrefix < 3) {
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
|
|
372
406
|
$lev = levenshtein($a, $b);
|
|
373
407
|
$maxLen = max(strlen($a), strlen($b));
|
|
374
408
|
if ($maxLen === 0) {
|
|
375
409
|
return false;
|
|
376
410
|
}
|
|
377
411
|
|
|
378
|
-
return $lev / $maxLen < 0.
|
|
412
|
+
return $lev / $maxLen < 0.2;
|
|
379
413
|
}
|
|
380
414
|
|
|
381
415
|
protected function normalizeText(string $text): string
|
|
@@ -506,6 +540,10 @@ class NavigationTree
|
|
|
506
540
|
$queryStems = $tokenizer->tokenizeAndStem($query);
|
|
507
541
|
$queryStems = $tokenizer->removeStopWords($queryStems);
|
|
508
542
|
|
|
543
|
+
if (count($queryStems) === 0) {
|
|
544
|
+
return null;
|
|
545
|
+
}
|
|
546
|
+
|
|
509
547
|
$candidates = [];
|
|
510
548
|
|
|
511
549
|
foreach ($this->stemmedIndex as $path => $stems) {
|
|
@@ -525,7 +563,7 @@ class NavigationTree
|
|
|
525
563
|
}
|
|
526
564
|
}
|
|
527
565
|
|
|
528
|
-
if (empty($candidates)) {
|
|
566
|
+
if (empty($candidates) || $candidates[0]['score'] < 2) {
|
|
529
567
|
return null;
|
|
530
568
|
}
|
|
531
569
|
|