More search faves
authorCameron Ball <cameron@moodle.com>
Tue, 7 May 2019 08:16:41 +0000 (16:16 +0800)
committerCameron Ball <cameron@moodle.com>
Tue, 7 May 2019 08:26:12 +0000 (16:26 +0800)
src/gnb.php

index fcf4a9d..337b14d 100644 (file)
@@ -26,6 +26,15 @@ getTelegram()->addCommand(
         protected $description = 'List favourites';
 
         public function handle($arguments) {
+            if (!$arguments) {
+                $this->replyWithMessage(
+                    [
+                        'text' => "Searchfaves command requires a query to search."
+                    ]
+                );
+                return;
+            }
+
             $faveFiles = foldMap(☐(null)(∘(lines, 'trim', 'file_get_contents')))(glob(PATH_TO_GROOVENET . '/*.favourites.txt'));
             $names = map(☐(produceValue(0))(null))(array_keys($faveFiles));
             $hashes = map(map(☐(produceValue(0))(null)))(array_values($faveFiles));
@@ -35,14 +44,14 @@ getTelegram()->addCommand(
 
             $thing = map(function ($fave) use ($arguments) {
                 $justTitle = join(' ', array_slice(explode(' ', $fave), 0 , -2));
-                similar_text($arguments, $justTitle ?: '', $perc);
+                similar_text(strtolower($arguments), strtolower($justTitle), $perc);
                 return ['title' => $fave, 'similarity' => $perc];
-            });
+            })($newFaves);
             $top5 = ∘(unlines, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘(∘f(field('similarity')), ⬄, field('similarity'))));
 
             $this->replyWithMessage(
                 [
-                    'text' => unlines($top5($thing))
+                    'text' => "Here are the 5 most similar favourites I could find...\n\n" . $top5($thing)
                 ]
             );
         }