<commit_before><?php
/**
 * Created by PhpStorm.
 * User: Taiki
 * Date: 19/09/2017
 * Time: 09:27
 */

	include_once ('../db.inc.php');
	$input = json_decode(file_get_contents('php://input'),true);

	$bdd = connectDB();

	$nbEntries = getNbSongs($bdd);

	//Compute the number of alternative suggestions to provide
	if(isset($input['nbSuggestions']) && is_numeric($input['nbSuggestions']))
		$nbSuggestions = min($nbEntries, $input['nbSuggestions']);

	else
		$nbSuggestions = $defaultNbSuggestions;

	//Validate the genre passed
	$requestedGenre = [];
	if(isset($input['genre']) && is_array($input['genre']))
	{
		foreach ($input['genre'] as $currentGenre)
		{
			if(validateGenre((int) $currentGenre))
			{
				array_push($requestedGenre, (int) $currentGenre);
			}
		}
	}

	//The list could contains deleted items...
	$hasExclusion = false;
	if(isset($input['excludeRecentlyTried']))
	{
		$exclusion = $input['excludeRecentlyTried'];

		if($exclusion != null && is_array($exclusion))
			$hasExclusion = true;
	}
	else
		$exclusion = [];

	//Get the ID of the song that will have to be guessed
	$questions = getRandomSongs($bdd, 1, $requestedGenre);
	if(empty($questions))
		exit('{"status":"error","error":"could not generate a song"}');

	$question = $questions[0];
	$output = [$question];

	//Get related songs to populate the suggestions
	$outputLength = 1;
	$suggestions = getCloseRelativeSong($question, $bdd, $nbSuggestions - 1 + ($hasExclusion ? count($exclusion) : 0));

	$tries = 0;

	while($outputLength < $nbSuggestions && $tries < 15)
	{
		foreach ($suggestions as $relative)
		{
			if((!in_array($relative, $output) || $tries >= 10) && 
				(!$hasExclusion || !in_array($relative, $exclusion) || $tries >= 5))
			{
				array_push($output, $relative);
				$outputLength += 1;

				if($outputLength != $nbSuggestions)
					break;
			}
		}

		//If we don't have enough related songs, so we get a few more random one.
		//We may have to do that multiple times depending of the exclusion list
		if($outputLength < $nbSuggestions)
		{
			$suggestions = getRandomSongs($bdd, $nbSuggestions - $outputLength);
			$tries += 1;
		}
	}

	shuffle($output);

	$processedOutput = extractDataForSongs($output, $bdd);

	if(empty($processedOutput))
		exit('{"status":"error","error":"could not generate output"}');

	$finalOutput = array(
		'status' => 'success',
		'songID' => $question,
		'filename' => getFileName($question, $bdd),
		'suggestions' => $processedOutput);

	echo json_encode($finalOutput);
<commit_msg>Fix a bug preventing from taking full advantage from our heuristics<commit_after><?php
/**
 * Created by PhpStorm.
 * User: Taiki
 * Date: 19/09/2017
 * Time: 09:27
 */

	include_once ('../db.inc.php');
	$input = json_decode(file_get_contents('php://input'),true);

	$bdd = connectDB();

	$nbEntries = getNbSongs($bdd);

	//Compute the number of alternative suggestions to provide
	if(isset($input['nbSuggestions']) && is_numeric($input['nbSuggestions']))
		$nbSuggestions = min($nbEntries, $input['nbSuggestions']);

	else
		$nbSuggestions = $defaultNbSuggestions;

	//Validate the genre passed
	$requestedGenre = [];
	if(isset($input['genre']) && is_array($input['genre']))
	{
		foreach ($input['genre'] as $currentGenre)
		{
			if(validateGenre((int) $currentGenre))
			{
				array_push($requestedGenre, (int) $currentGenre);
			}
		}
	}

	//The list could contains deleted items...
	$hasExclusion = false;
	if(isset($input['excludeRecentlyTried']))
	{
		$exclusion = $input['excludeRecentlyTried'];

		if($exclusion != null && is_array($exclusion))
			$hasExclusion = true;
	}
	else
		$exclusion = [];

	//Get the ID of the song that will have to be guessed
	$questions = getRandomSongs($bdd, 1, $requestedGenre);
	if(empty($questions))
		exit('{"status":"error","error":"could not generate a song"}');

	$question = $questions[0];
	$output = [$question];

	//Get related songs to populate the suggestions
	$outputLength = 1;
	$suggestions = getCloseRelativeSong($question, $bdd, $nbSuggestions - 1 + ($hasExclusion ? count($exclusion) : 0));

	$tries = 0;

	while($outputLength < $nbSuggestions && $tries < 15)
	{
		foreach ($suggestions as $relative)
		{
			if((!in_array($relative, $output) || $tries >= 10) && 
				(!$hasExclusion || !in_array($relative, $exclusion) || $tries >= 5))
			{
				array_push($output, $relative);
				$outputLength += 1;

				if($outputLength === $nbSuggestions)
					break;
			}
		}

		//If we don't have enough related songs, so we get a few more random one.
		//We may have to do that multiple times depending of the exclusion list
		if($outputLength < $nbSuggestions)
		{
			$suggestions = getRandomSongs($bdd, $nbSuggestions - $outputLength);
			$tries += 1;
		}
	}

	shuffle($output);

	$processedOutput = extractDataForSongs($output, $bdd);

	if(empty($processedOutput))
		exit('{"status":"error","error":"could not generate output"}');

	$finalOutput = array(
		'status' => 'success',
		'songID' => $question,
		'filename' => getFileName($question, $bdd),
		'suggestions' => $processedOutput);

	echo json_encode($finalOutput);

{'fraction_non_alphanumeric': 0.17076046374533307, 'fraction_numerical': 0.009825112988799371, 'mean_word_length': 4.550708833151581, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3668288', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1686, 'n_words': 400}