1
0
Fork 0
oldhaven/php-bak/core/classes/TrackWeight.class.php

99 lines
2.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*****************************************************************
Пример использования:
$weight_calc = new TrackWeight();
$weight_calc->setTrackData('Blondie', 'Call Me', 210);
$weight_calc->setFiles($files); // Файлы, полученные от парсера
$weight_calc->calculateWeight();
$files = $weight_calc->getFiles();
*****************************************************************/
/**
* Класс посчета веса файла (коэфициента, определяющего релевантность)
*
* @package classes
* @author chez
**/
class TrackWeight {
private $_artist; // Имя исполнителя
private $_track; // Название трека
private $_duration; // Длительность трека в секундах
private $_files; // Массив файлов для сравнения
/**
* Задает параметры оригинального трека
*
* @param string $artist Имя исполнителя
* @param string $track Запрос
* @param int $duration Длительность трека в секундах
* @return void
* @author chez
**/
public function setTrackData($artist, $track, $duration) {
$this->_artist = $artist;
$this->_track = $track;
$this->_duration = $duration;
}
/**
* Задает массив файлов для сравнения
*
* @param array $files Массив файлов для сравнения
* @return void
* @author chez
**/
public function setFiles($files) {
$this->_files = $files;
}
/**
* Возвращает файлы с проставленным весом
*
* @return array $files Массив файлов
* @author chez
**/
public function getFiles() {
return $this->_files;
}
/**
* Рассчитывает вес для каждого файла
*
* @return void
* @author chez
**/
public function calculateWeight() {
foreach ($this->_files as $i => $file) {
$weight = 0;
if ($file['artist'] == $this->_artist) {
$weight += 10;
} elseif (strpos($file['artist'], $this->_artist) !== false) {
$weight += 5;
} elseif (strpos($file['track'], $this->_artist) !== false) {
$weight += 4;
}
if ($file['track'] == $this->_track) {
$weight += 10;
} elseif (strpos($file['track'], $this->_track) !== false) {
$weight += 5;
}
if ($file['duration'] == $this->_duration) {
$weight += 10;
} else {
$delta = abs($file['duration'] - $this->_duration);
if ($delta < 5) {
$weight += (5 - $delta);
}
}
$this->_files[$i]['weight'] = $weight;
}
}
}