From f18e37721e1354b70814f3f907370719afef15b4 Mon Sep 17 00:00:00 2001 From: Ludy Su Date: Thu, 9 Nov 2017 14:58:19 +1300 Subject: [PATCH] Fine tuned matching algo, use similar_text() instead --- INFO | 4 ++-- netease.php | 51 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/INFO b/INFO index c486822..22899f0 100644 --- a/INFO +++ b/INFO @@ -1,7 +1,7 @@ { "name": "ludysu_neteaselrc", - "displayname": "NE Cloud Music Lrc", - "description": "NE Cloud Music Lrc", + "displayname": "网易云音乐", + "description": "特色:根据曲名、艺术家匹配程度高低自动排序", "version": "1.0", "site": "http://music.163.com", "module": "netease.php", diff --git a/netease.php b/netease.php index 2a37b40..4d62296 100644 --- a/netease.php +++ b/netease.php @@ -42,9 +42,11 @@ class LudysuNetEaseLrc { $exactMatchArray = array(); $partialMatchArray = array(); foreach ($songArray as $song) { - if (strtolower($title) === strtolower($song['name'])) { + $lowTitle = strtolower($title); + $lowResult = strtolower($song['name']); + if (strtolower($lowTitle) === strtolower($lowResult)) { array_push($exactMatchArray, $song); - } else if (strpos($song['name'], $title) !== FALSE || strpos($title, $song['name']) !== FALSE) { + } else if (strpos($lowResult, $lowTitle) !== FALSE || strpos($lowTitle, $lowResult) !== FALSE) { array_push($partialMatchArray, $song); } } @@ -66,11 +68,11 @@ class LudysuNetEaseLrc { ); // Find the best match artist from all artists belong to a song - $min = 256; + $max = 0; foreach ($song['artists'] as $item) { - $distance = levenshtein($artist, $item['name']); - if ($distance < $min) { - $min = $distance; + $score = $this->getStringSimilarity($artist, $item['name']); + if ($score > $max) { + $max = $distance; $elem['artist'] = $item['name']; } } @@ -103,18 +105,25 @@ class LudysuNetEaseLrc { return true; } - + private function cmp($lhs, $rhs) { - // levenshtein(): the smaller the more similarity - $scoreArtistL = levenshtein($this->mArtist, $lhs['artist']); - $scoreArtistR = levenshtein($this->mArtist, $rhs['artist']); - $scoreTitleL = levenshtein($this->mTitle, $lhs['title']); - $scoreTitleR = levenshtein($this->mTitle, $rhs['title']); + $scoreArtistL = $this->getStringSimilarity($this->mArtist, $lhs['artist']); + $scoreArtistR = $this->getStringSimilarity($this->mArtist, $rhs['artist']); + $scoreTitleL = $this->getStringSimilarity($this->mTitle, $lhs['title']); + $scoreTitleR = $this->getStringSimilarity($this->mTitle, $rhs['title']); + + printf("artist " . $lhs['artist'] . " vs " . $rhs['artist'] . " | " . $scoreArtistL . " vs " . $scoreArtistR . "
"); + printf("title " . $lhs['title'] . " vs " . $rhs['title'] . " | " . $scoreTitleL . " vs " . $scoreTitleR. "
"); - // echo "artist " . $lhs['artist'] . " vs " . $rhs['artist'] . " | " . $scoreArtistL . " vs " . $scoreArtistR . "\n"; - // echo "title " . $lhs['title'] . " vs " . $rhs['title'] . " | " . $scoreTitleL . " vs " . $scoreTitleR. "\n\n"; + return $scoreArtistR + $scoreTitleR - $scoreArtistL - $scoreTitleL; + } - return $scoreArtistL + $scoreTitleL - $scoreTitleR - $scoreArtistR; + /** + * Gets similarity score of 0-100 between 2 strings, the bigger the score is, the more similarity. + */ + private static function getStringSimilarity($lhs, $rhs) { + similar_text($lhs, $rhs, $percent); + return $percent; } private static function search($word) { @@ -183,21 +192,21 @@ if ($DEBUG == true) { } public function addLyrics($lyric, $id) { - printf("\n"); + printf("
"); printf("song id: %s\n", $id); - printf("\n"); + printf("
"); printf("== lyric ==\n"); printf("%s\n", $lyric); printf("** END of lyric **\n\n"); } public function addTrackInfoToList($artist, $title, $id, $prefix) { - printf("\n"); + printf("
"); printf("song id: %s\n", $id); printf("artist [%s]\n", $artist); printf("title [%s]\n", $title); printf("prefix [%s]\n", $prefix); - printf("\n"); + printf("
"); array_push($this->items, array( 'artist' => $artist, @@ -222,8 +231,8 @@ if ($DEBUG == true) { /** * Main */ - $title = "longing"; - $artist = "ユナ CV.神田さやか"; + $title = "tell your world"; + $artist = "初音ミク"; echo "Trying to find lyrics for ['$title'] by artist ['$artist'] ...
"; $testObj = new TestObj();