Add search command for all songs
authorCameron Ball <cameron@moodle.com>
Wed, 8 May 2019 02:21:26 +0000 (10:21 +0800)
committerCameron Ball <cameron@moodle.com>
Wed, 8 May 2019 02:31:04 +0000 (10:31 +0800)
src/gnb.php

index 337b14d..02f3ef4 100644 (file)
@@ -22,8 +22,38 @@ function scoresFileContentsToScoresList($who) {
 
 getTelegram()->addCommand(
     new class extends Command {
+        protected $name = 'search';
+        protected $description = 'Search songs list';
+
+        public function handle($arguments) {
+            if (!$arguments) {
+                $this->replyWithMessage(
+                    [
+                        'text' => "Search command requires a query to search."
+                    ]
+                );
+                return;
+            }
+
+            $songs = map(function ($song) use ($arguments) {
+                similar_text(strtolower($arguments), strtolower($song), $perc);
+                return ['title' => $song, 'similarity' => $perc];
+            })(unserialize(file_get_contents(__DIR__ . '/hashmap.txt')));
+            $top5 = ∘(unlines, aaray_column('title'), aaray_slice(0)(5), 'array_reverse', 'array_values', ssort(∘(∘f(field('similarity')), ⬄, field('similarity'))));
+
+            $this->replyWithMessage(
+                [
+                    'text' => "Here are the 5 most similar favourites I could find...\n\n" . $top5($songs)
+                ]
+            );
+        }
+    }
+);
+
+getTelegram()->addCommand(
+    new class extends Command {
         protected $name = 'searchfaves';
-        protected $description = 'List favourites';
+        protected $description = 'Search favourites';
 
         public function handle($arguments) {
             if (!$arguments) {