PHP code example of ozcan39 / ir_evaluation_php

1. Go to this page and download the library: Download ozcan39/ir_evaluation_php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ozcan39 / ir_evaluation_php example snippets


composer 

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset1.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    var_dump(trim(fgets($dataset)));
}

$interactions=array();

# dataset3 formation: id|query_id|related_document_id

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset3.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    $row=explode('|',trim(fgets($dataset)));
    $interactions[$row[1]]['related_documents'][]=$row[2];
}
fclose($dataset);

# dataset4 formation: id|query_id|total_result|visited_or_related_document_id|order_number_of_the_document

$dataset2 = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset4.txt", "r") or die("Unable to open file!");
while(!feof($dataset2))
{
    $row=explode('|',trim(fgets($dataset2)));
    $interactions[$row[1]]['total_result']=$row[2];
    $interactions[$row[1]]['visited_documents'][]=$row[3];
    $interactions[$row[1]]['visited_documents_orders'][$row[3]]=$row[4];
}
fclose($dataset2);

########################################################################################
# parameters => (data, boundaries)

echo "<h1>Average Precision@n</h1>";
$ap_at_n=$ir->ap_at_n($interactions,array(5,10,15,20,'all'));
var_dump($ap_at_n);

echo "<h1>R-Precision</h1>";
$rprecision=$ir->rprecision($interactions,array(5,10,15,20,'all'));
var_dump($rprecision);

echo "<h1>Mean Average Precision</h1>";
$mean_ap=$ir->mean_ap($interactions,array(5,10,15,20,'all'));
var_dump($mean_ap);

echo "<h1>F-Measure</h1>";
$fmeasure=$ir->fmeasure($interactions,array(5,10,15,20,'all'));
var_dump($fmeasure);
########################################################################################
# parameters -> (data, constant, boundaries)

echo "<h1>Geometric Mean Average Precision</h1>";
$gmap=$ir->gmap($interactions,0.3,array(5,10,15,20,'all'));
var_dump($gmap);
########################################################################################
# parameters -> (data)

echo "<h1>Eleven Point - Interpolated Average Precision</h1>";
echo "<h4>Recall => Precision</h4>";
$iap=$ir->iap($interactions);
var_dump($iap);

$interactions=array();

# dataset1 formation: id|query_id|total_result|visited_or_related_document_id|order_number_of_the_document|assessment_of_the_document
# assessment_of_the_document: assessment is between 1 and 5 for this example

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset1.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    $row=explode('|',trim(fgets($dataset)));
    $interactions[$row[1]]['total_result']=$row[2];
    $interactions[$row[1]]['assessed_documents'][$row[3]]=array($row[4],$row[5]);
}
fclose($dataset);

# parameters => (data, boundaries)

echo "<h1>Cumulative Gain</h1>";
$cgain=$ir->cgain($interactions,array(5,10,15,20,"all"));
var_dump($cgain);

echo "<h1>Normalized Cumulative Gain</h1>";
$ncgain=$ir->ncgain($interactions,array(5,10,15,20));
var_dump($ncgain);

echo "<h1>Discounted Cumulative Gain</h1>";
$dcgain=$ir->dcgain($interactions,array(5,10,15,20));
var_dump($dcgain);

echo "<h1>Normalized Discounted Cumulative Gain</h1>";
$ndcgain=$ir->ndcgain($interactions,array(5,10,15,20,"all"));
var_dump($ndcgain);

$interactions=array();

# dataset2 formation: id|query_id|visited_or_related_document_id|order_number_of_the_document

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset2.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    $row=explode('|',trim(fgets($dataset)));
    $interactions[$row[1]]['visited_documents_orders'][]=$row[3];
}
fclose($dataset);

# parameters => (data)

echo "<h1>Mean Reciprocal Rank</h1>";
$mrr=$ir->mrr($interactions);
var_dump($mrr);

$interactions=array();

# dataset4 formation: id|query_id|total_result|visited_or_related_document_id|order_number_of_the_document

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset4.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    $row=explode('|',trim(fgets($dataset)));
    $interactions[$row[1]]['total_result']=$row[2];
    $interactions[$row[1]]['visited_documents'][]=$row[3];
    $interactions[$row[1]]['visited_documents_orders'][$row[3]]=$row[4];
}
fclose($dataset);

# parameters => (data, persistence (or probability) levels, boundaries)

echo "<h1>Rank Biased Precision</h1>";
$rbprecision=$ir->rbprecision($interactions,array(0.5,0.8,0.95),array(5,10,15,20,'all'));
var_dump($rbprecision);

$interactions=array();

# dataset1 formation: id|query_id|total_result|visited_or_related_document_id|order_number_of_the_document|assessment_of_the_document
# assessment_of_the_document: assessment is between 1 and 5 for this example

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset1.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    $row=explode('|',trim(fgets($dataset)));
    $interactions[$row[1]]['total_result']=$row[2];
    $interactions[$row[1]]['assessed_documents'][$row[3]]=array($row[4],$row[5]);
}
fclose($dataset);

# parameters => (data, max_grade_level, boundaries)

echo "<h1>Expected Reciprocal Rank</h1>";
$err=$ir->err($interactions,5,array(5,10,15,20,"all"));
var_dump($err);

$interactions=array();

# dataset5 just consists of judged documents. Similar to dataset1, but last column has 2 different (boolean) values: 1: related, 0: unrelated
# data, which belong to unjudged documents, do not need to be inside of the dataset
# dataset5 formation: id|query_id|total_result|visited_or_related_document_id|order_number_of_the_document|judgement_of_the_document

$dataset = fopen("vendor/ozcan39/ir_evaluation_php/src/datasets/dataset5.txt", "r") or die("Unable to open file!");
while(!feof($dataset))
{
    $row=explode('|',trim(fgets($dataset)));
    $interactions[$row[1]]['total_result']=$row[2];
    $interactions[$row[1]]['assessed_documents'][$row[3]]=array($row[4],$row[5]);
}
fclose($dataset);

# parameters => (data, boundaries)

echo "<h1>BPREF</h1>";
$bpref=$ir->bpref($interactions,array(5,10,15,20,"all"));
var_dump($bpref);