/learn_one_category_5.php
PHP | 184 lines | 128 code | 37 blank | 19 comment | 9 complexity | 1f39ab74dd3591ea74dbc97264d2c664 MD5 | raw file
- #!/usr/bin/php
- <?php
- $folder = $argv[1] ;
- $count = $argv[2] ;
- $limit = $argv[3] ;
- include("functions.php");
- echo "working on folder '".$folder."' \n\n";
- $dllearnerurl = "http://139.18.2.57:8888/interfaces/rest" ;
-
- $ListInstancesOfCategory = file_array_outputer($folder."/instances.list"); // list of relevant examples (+)
- $rand = 5;
- $pos = get_random_uri($ListInstancesOfCategory, $rand);
- //$pos = array("http://dbpedia.org/resource/John_Skandalis","http://dbpedia.org/resource/Michael_Crockett","http://dbpedia.org/resource/Gareth_Ellis","http://dbpedia.org/resource/Ken_McGuinness","http://dbpedia.org/resource/Tim_Moltzen");
- var_dump($pos);
- $ListInstancesOfCategorysSister = file_array_outputer($folder."/instances_catsister.list"); // list of non-relevant examples (-)
- $neg = get_random_uri($ListInstancesOfCategorysSister, $rand);
- //$neg = array("http://dbpedia.org/resource/Aaron_Murphy","http://dbpedia.org/resource/Lee_Te_Maari","http://dbpedia.org/resource/Liam_Harrison_%28rugby_league%29","http://dbpedia.org/resource/Gavin_Dodd","http://dbpedia.org/resource/Dominic_Feau%27nati");
- var_dump($neg);
- $allinstances = file($folder."/instances.list");
- if(count($allinstances)!= 100 ){
- echo "ERROR count was not 100: ". count($allinstances)."\n";
- };
- $step = 1;
- $sum_step = 5; // LOOP counter
- $maxfoundeditems = 0;
- $collectmachteditems = array();
- $allfoundeditems = 0; // collection all founded instances
- $lastsparql = "";
- $instancesOfCategory = file_array_outputer($folder."/instances.list");
- $index_position = 4;
- $category_name = substr($folder,$index_position);
- // INITIAL VARIABLES TO COLLECT DATA
- $bestaccuracy = 0;
- $lastaccuracy = 0;
- $bestconcept = 0;
- $lastconcept = "";
- $lastsparql = "";
- $best_count_matching_in_all_steps = 0; // collection the best number of positive results in any steps
-
- while ($step <= $sum_step) {
- $count_matching = 0;
- $learned_results = learn($dllearnerurl, $limit, $pos, $neg);
- echo "######################## RESULTS after ".$step.".learning ######################## \n\n";
- print_r($learned_results);
- echo "######################## [END] RESULTS after ".$step.".learning ######################## \n\n";
- // STORE ALL RESULTS AFTER LEARNING IN AN ARRAY
- $SPARQL["success"] = $learned_results ["learningresult"]["success"];
- $SPARQL["accuracy"] = $learned_results ["learningresult"]["accuracy"];
- $SPARQL["concept"] = $learned_results ["learningresult"]["manchester"];
- $SPARQL["sparql"] = $learned_results ["learningresult"]["sparql"];
-
- /* Checking for SUCCESSFUL */
- if($SPARQL["success"] == '1') {
- /** collecting the best accuracy & his concept */
- if ($bestaccuracy < $SPARQL["accuracy"]) {
- $bestaccuracy = $SPARQL["accuracy"];
- $bestconcept = $SPARQL["concept"];
- }
-
- /** collecting the last accuracy & his concept */
- $lastaccuracy = $SPARQL["accuracy"];
- $lastconcept = $SPARQL["concept"];
- $lastsparql = $SPARQL["sparql"];
-
- /* get URI over live.dbpedia.org endpoint within the received query */
- $defaultgraphURI = 'http://dbpedia.org';
- $sparqlQueryString = $SPARQL["sparql"];
- $sparqlQueryString = str_replace("SELECT", "SELECT DISTINCT", $sparqlQueryString);
- $ep = "http://hanne.aksw.org:8892/sparql?query=";
-
- /* EXECUTE THE RETURNED SPARQL QUERY */
- $ListOfResultbyLearning = executeSparqlQuery_json ($sparqlQueryString, "subject");
- $ListOfResultPosbyLearning = array ();
- echo "============= results from executing the sparql query ============= \n";
- foreach($ListOfResultbyLearning as $ins_uri) {
-
- foreach($ListInstancesOfCategory as $ins)
- {
- if($ins_uri === $ins) {
- $collectmachteditems[$ins] = 1 ;
- $ListOfResultPosbyLearning [] = $ins; // collect all positive results
- $allfoundeditems++;
- $count_matching++;
- }
- }
- }
- echo "ListOfResultbyLearning \n";
- print_r($ListOfResultbyLearning);
-
- // Compare two arrays: ListOfResultbyLearning and ListOfResultPosbyLearning
- // to get an new arrays: ListOfResultNegbyLearning
- $ListOfResultNegbyLearning = array_diff($ListOfResultbyLearning,$ListOfResultPosbyLearning); // collect all negative results
- echo "* Total Matching: ".$count_matching."\n";
-
- /** Compare to get the best number of matching results */
- if($best_count_matching_in_all_steps < $count_matching) {
- $best_count_matching_in_all_steps = $count_matching;
- }
-
-
- echo "Positive examples \n\n";
- print_r($ListOfResultPosbyLearning); // List of the returned items which are matched agains the items of the specified category
- $pos = array_merge(get_random_uri($ListOfResultPosbyLearning, 5),$pos);
- echo "**** (+) Preparation a new list of postive examples for the next step **** \n";
- print_r($pos);
-
- echo "Negative examples \n\n";
- print_r($ListOfResultNegbyLearning); // List of the returned items which are matched agains the items of the specified category
-
- // initial a new negative example list
- $neg = array_merge(get_random_uri($ListOfResultNegbyLearning, 5),$neg);
- echo "**** (-) Preparation a new list of negative examples for the next step *** \n";
- print_r($neg);
- }
- /* END: Checking for SUCCESSFUL */
-
- $step++;
- }
- echo "** Totally collected items ".count($collectmachteditems);
- $maxfoundeditems = count(array_keys($collectmachteditems));
- $numberofinstancesOfCategory = count(array_keys($instancesOfCategory));
- $CategoryStatisticDetails ["categoryname"] = $category_name;
- $CategoryStatisticDetails ["bestaccuracy"] = $bestaccuracy;
- $CategoryStatisticDetails ["lastaccuracy"] = $lastaccuracy;
- $CategoryStatisticDetails ["bestconcept"] = $bestconcept;
- $CategoryStatisticDetails ["lastconcept"] = $lastconcept;
- $CategoryStatisticDetails ["maxfoundeditems"] = $maxfoundeditems;
- $CategoryStatisticDetails ["recallMAX"] = ($maxfoundeditems/$numberofinstancesOfCategory) ; //
- $CategoryStatisticDetails ["recallBEST"] = ($best_count_matching_in_all_steps / $numberofinstancesOfCategory) ;
- //$CategoryStatisticDetails ["recallBEST"] = ($bestaccuracy / 100); // calculation follow the best accuracy
- $CategoryStatisticDetails ["recallAVG"] = ($allfoundeditems/($sum_step*$numberofinstancesOfCategory));
- $CategoryStatisticDetails ["precTOTAL"] = ($allfoundeditems/($sum_step*$limit)); //
- $CategoryStatisticDetails ["precBEST"] = ($best_count_matching_in_all_steps /$limit); //
- $CategoryStatisticDetails ["precAVG"] = ($allfoundeditems/($sum_step*$limit));
- $CategoryStatisticDetails ["lastsparql"] = $lastsparql; //
-
- echo "CategoryStatisticDetails \n";
- print_r($CategoryStatisticDetails);
- /* START: Adding data into sheet */
- include ("class.spreadsheet.php");
- $doc = new spreadsheet();
- $acc = array("dmlldHBsYWNlc0Bnb29nbGVtYWlsLmNvbQ==","ODIxNTIyODIxNTIy");
- $doc->authenticate(base64_decode($acc[0]), base64_decode($acc[1]));
- $doc->setSpreadsheet("MatchedResults");
- $doc->setWorksheet("Statistic-DL-Learner-$limit");
- // END initial a doc
-
- $my_data = array("Nr" => $count,
- "Category Name" => $CategoryStatisticDetails ["categoryname"],
- "Best Accuracy" => $CategoryStatisticDetails ["bestaccuracy"],
- "Last Accuracy" => $CategoryStatisticDetails ["lastaccuracy"],
- "Best Concept" => $CategoryStatisticDetails ["bestconcept"],
- "Last Concept" => $CategoryStatisticDetails ["lastconcept"],
- "#Max collected Members" => $CategoryStatisticDetails ["maxfoundeditems"],
- "recallMAX" => round($CategoryStatisticDetails ["recallMAX"],4),
- "recallBEST" => round($CategoryStatisticDetails ["recallBEST"],4),
- "recallAVG" => round($CategoryStatisticDetails ["recallAVG"],4) ,
- "precTOTAL" => round($CategoryStatisticDetails ["precTOTAL"],4),
- "precBEST" => round($CategoryStatisticDetails ["precBEST"],4),
- "precAVG" => round($CategoryStatisticDetails ["precAVG"],4),
- "Last SPARQL query" => $CategoryStatisticDetails ["lastsparql"]);
- $doc->add($my_data);
-
- /* END: Adding data into sheet */
-