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. 51
      netease.php

4
INFO

@ -1,7 +1,7 @@ @@ -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",

51
netease.php

@ -42,9 +42,11 @@ class LudysuNetEaseLrc { @@ -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 { @@ -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 { @@ -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 . "</br>");
printf("title " . $lhs['title'] . " vs " . $rhs['title'] . " | " . $scoreTitleL . " vs " . $scoreTitleR. "</br>");
// 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) { @@ -183,21 +192,21 @@ if ($DEBUG == true) {
}
public function addLyrics($lyric, $id) {
printf("\n");
printf("</br>");
printf("song id: %s\n", $id);
printf("\n");
printf("</br>");
printf("== lyric ==\n");
printf("%s\n", $lyric);
printf("** END of lyric **\n\n");
}
public function addTrackInfoToList($artist, $title, $id, $prefix) {
printf("\n");
printf("</br>");
printf("song id: %s\n", $id);
printf("artist [%s]\n", $artist);
printf("title [%s]\n", $title);
printf("prefix [%s]\n", $prefix);
printf("\n");
printf("</br>");
array_push($this->items, array(
'artist' => $artist,
@ -222,8 +231,8 @@ if ($DEBUG == true) { @@ -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'] ...</br>";
$testObj = new TestObj();

Loading…
Cancel
Save