Browse Source

Fine tuned matching algo, use similar_text() instead

pull/8/head
Ludy Su 7 years ago
parent
commit
f18e37721e
  1. 4
      INFO
  2. 49
      netease.php

4
INFO

@ -1,7 +1,7 @@
{ {
"name": "ludysu_neteaselrc", "name": "ludysu_neteaselrc",
"displayname": "NE Cloud Music Lrc", "displayname": "网易云音乐",
"description": "NE Cloud Music Lrc", "description": "特色:根据曲名、艺术家匹配程度高低自动排序",
"version": "1.0", "version": "1.0",
"site": "http://music.163.com", "site": "http://music.163.com",
"module": "netease.php", "module": "netease.php",

49
netease.php

@ -42,9 +42,11 @@ class LudysuNetEaseLrc {
$exactMatchArray = array(); $exactMatchArray = array();
$partialMatchArray = array(); $partialMatchArray = array();
foreach ($songArray as $song) { 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); 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); array_push($partialMatchArray, $song);
} }
} }
@ -66,11 +68,11 @@ class LudysuNetEaseLrc {
); );
// Find the best match artist from all artists belong to a song // Find the best match artist from all artists belong to a song
$min = 256; $max = 0;
foreach ($song['artists'] as $item) { foreach ($song['artists'] as $item) {
$distance = levenshtein($artist, $item['name']); $score = $this->getStringSimilarity($artist, $item['name']);
if ($distance < $min) { if ($score > $max) {
$min = $distance; $max = $distance;
$elem['artist'] = $item['name']; $elem['artist'] = $item['name'];
} }
} }
@ -105,16 +107,23 @@ class LudysuNetEaseLrc {
} }
private function cmp($lhs, $rhs) { private function cmp($lhs, $rhs) {
// levenshtein(): the smaller the more similarity $scoreArtistL = $this->getStringSimilarity($this->mArtist, $lhs['artist']);
$scoreArtistL = levenshtein($this->mArtist, $lhs['artist']); $scoreArtistR = $this->getStringSimilarity($this->mArtist, $rhs['artist']);
$scoreArtistR = levenshtein($this->mArtist, $rhs['artist']); $scoreTitleL = $this->getStringSimilarity($this->mTitle, $lhs['title']);
$scoreTitleL = levenshtein($this->mTitle, $lhs['title']); $scoreTitleR = $this->getStringSimilarity($this->mTitle, $rhs['title']);
$scoreTitleR = levenshtein($this->mTitle, $rhs['title']);
// echo "artist " . $lhs['artist'] . " vs " . $rhs['artist'] . " | " . $scoreArtistL . " vs " . $scoreArtistR . "\n"; printf("artist " . $lhs['artist'] . " vs " . $rhs['artist'] . " | " . $scoreArtistL . " vs " . $scoreArtistR . "</br>");
// echo "title " . $lhs['title'] . " vs " . $rhs['title'] . " | " . $scoreTitleL . " vs " . $scoreTitleR. "\n\n"; printf("title " . $lhs['title'] . " vs " . $rhs['title'] . " | " . $scoreTitleL . " vs " . $scoreTitleR. "</br>");
return $scoreArtistL + $scoreTitleL - $scoreTitleR - $scoreArtistR; return $scoreArtistR + $scoreTitleR - $scoreArtistL - $scoreTitleL;
}
/**
* 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) { private static function search($word) {
@ -183,21 +192,21 @@ if ($DEBUG == true) {
} }
public function addLyrics($lyric, $id) { public function addLyrics($lyric, $id) {
printf("\n"); printf("</br>");
printf("song id: %s\n", $id); printf("song id: %s\n", $id);
printf("\n"); printf("</br>");
printf("== lyric ==\n"); printf("== lyric ==\n");
printf("%s\n", $lyric); printf("%s\n", $lyric);
printf("** END of lyric **\n\n"); printf("** END of lyric **\n\n");
} }
public function addTrackInfoToList($artist, $title, $id, $prefix) { public function addTrackInfoToList($artist, $title, $id, $prefix) {
printf("\n"); printf("</br>");
printf("song id: %s\n", $id); printf("song id: %s\n", $id);
printf("artist [%s]\n", $artist); printf("artist [%s]\n", $artist);
printf("title [%s]\n", $title); printf("title [%s]\n", $title);
printf("prefix [%s]\n", $prefix); printf("prefix [%s]\n", $prefix);
printf("\n"); printf("</br>");
array_push($this->items, array( array_push($this->items, array(
'artist' => $artist, 'artist' => $artist,
@ -222,8 +231,8 @@ if ($DEBUG == true) {
/** /**
* Main * Main
*/ */
$title = "longing"; $title = "tell your world";
$artist = "ユナ CV.神田さやか"; $artist = "初音ミク";
echo "Trying to find lyrics for ['$title'] by artist ['$artist'] ...</br>"; echo "Trying to find lyrics for ['$title'] by artist ['$artist'] ...</br>";
$testObj = new TestObj(); $testObj = new TestObj();

Loading…
Cancel
Save