PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/learn_one_category_5.php

https://bitbucket.org/wormsbee/evalnke
PHP | 184 lines | 128 code | 37 blank | 19 comment | 9 complexity | 1f39ab74dd3591ea74dbc97264d2c664 MD5 | raw file
  1. #!/usr/bin/php
  2. <?php
  3. $folder = $argv[1] ;
  4. $count = $argv[2] ;
  5. $limit = $argv[3] ;
  6. include("functions.php");
  7. echo "working on folder '".$folder."' \n\n";
  8. $dllearnerurl = "http://139.18.2.57:8888/interfaces/rest" ;
  9. $ListInstancesOfCategory = file_array_outputer($folder."/instances.list"); // list of relevant examples (+)
  10. $rand = 5;
  11. $pos = get_random_uri($ListInstancesOfCategory, $rand);
  12. //$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");
  13. var_dump($pos);
  14. $ListInstancesOfCategorysSister = file_array_outputer($folder."/instances_catsister.list"); // list of non-relevant examples (-)
  15. $neg = get_random_uri($ListInstancesOfCategorysSister, $rand);
  16. //$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");
  17. var_dump($neg);
  18. $allinstances = file($folder."/instances.list");
  19. if(count($allinstances)!= 100 ){
  20. echo "ERROR count was not 100: ". count($allinstances)."\n";
  21. };
  22. $step = 1;
  23. $sum_step = 5; // LOOP counter
  24. $maxfoundeditems = 0;
  25. $collectmachteditems = array();
  26. $allfoundeditems = 0; // collection all founded instances
  27. $lastsparql = "";
  28. $instancesOfCategory = file_array_outputer($folder."/instances.list");
  29. $index_position = 4;
  30. $category_name = substr($folder,$index_position);
  31. // INITIAL VARIABLES TO COLLECT DATA
  32. $bestaccuracy = 0;
  33. $lastaccuracy = 0;
  34. $bestconcept = 0;
  35. $lastconcept = "";
  36. $lastsparql = "";
  37. $best_count_matching_in_all_steps = 0; // collection the best number of positive results in any steps
  38. while ($step <= $sum_step) {
  39. $count_matching = 0;
  40. $learned_results = learn($dllearnerurl, $limit, $pos, $neg);
  41. echo "######################## RESULTS after ".$step.".learning ######################## \n\n";
  42. print_r($learned_results);
  43. echo "######################## [END] RESULTS after ".$step.".learning ######################## \n\n";
  44. // STORE ALL RESULTS AFTER LEARNING IN AN ARRAY
  45. $SPARQL["success"] = $learned_results ["learningresult"]["success"];
  46. $SPARQL["accuracy"] = $learned_results ["learningresult"]["accuracy"];
  47. $SPARQL["concept"] = $learned_results ["learningresult"]["manchester"];
  48. $SPARQL["sparql"] = $learned_results ["learningresult"]["sparql"];
  49. /* Checking for SUCCESSFUL */
  50. if($SPARQL["success"] == '1') {
  51. /** collecting the best accuracy & his concept */
  52. if ($bestaccuracy < $SPARQL["accuracy"]) {
  53. $bestaccuracy = $SPARQL["accuracy"];
  54. $bestconcept = $SPARQL["concept"];
  55. }
  56. /** collecting the last accuracy & his concept */
  57. $lastaccuracy = $SPARQL["accuracy"];
  58. $lastconcept = $SPARQL["concept"];
  59. $lastsparql = $SPARQL["sparql"];
  60. /* get URI over live.dbpedia.org endpoint within the received query */
  61. $defaultgraphURI = 'http://dbpedia.org';
  62. $sparqlQueryString = $SPARQL["sparql"];
  63. $sparqlQueryString = str_replace("SELECT", "SELECT DISTINCT", $sparqlQueryString);
  64. $ep = "http://hanne.aksw.org:8892/sparql?query=";
  65. /* EXECUTE THE RETURNED SPARQL QUERY */
  66. $ListOfResultbyLearning = executeSparqlQuery_json ($sparqlQueryString, "subject");
  67. $ListOfResultPosbyLearning = array ();
  68. echo "============= results from executing the sparql query ============= \n";
  69. foreach($ListOfResultbyLearning as $ins_uri) {
  70. foreach($ListInstancesOfCategory as $ins)
  71. {
  72. if($ins_uri === $ins) {
  73. $collectmachteditems[$ins] = 1 ;
  74. $ListOfResultPosbyLearning [] = $ins; // collect all positive results
  75. $allfoundeditems++;
  76. $count_matching++;
  77. }
  78. }
  79. }
  80. echo "ListOfResultbyLearning \n";
  81. print_r($ListOfResultbyLearning);
  82. // Compare two arrays: ListOfResultbyLearning and ListOfResultPosbyLearning
  83. // to get an new arrays: ListOfResultNegbyLearning
  84. $ListOfResultNegbyLearning = array_diff($ListOfResultbyLearning,$ListOfResultPosbyLearning); // collect all negative results
  85. echo "* Total Matching: ".$count_matching."\n";
  86. /** Compare to get the best number of matching results */
  87. if($best_count_matching_in_all_steps < $count_matching) {
  88. $best_count_matching_in_all_steps = $count_matching;
  89. }
  90. echo "Positive examples \n\n";
  91. print_r($ListOfResultPosbyLearning); // List of the returned items which are matched agains the items of the specified category
  92. $pos = array_merge(get_random_uri($ListOfResultPosbyLearning, 5),$pos);
  93. echo "**** (+) Preparation a new list of postive examples for the next step **** \n";
  94. print_r($pos);
  95. echo "Negative examples \n\n";
  96. print_r($ListOfResultNegbyLearning); // List of the returned items which are matched agains the items of the specified category
  97. // initial a new negative example list
  98. $neg = array_merge(get_random_uri($ListOfResultNegbyLearning, 5),$neg);
  99. echo "**** (-) Preparation a new list of negative examples for the next step *** \n";
  100. print_r($neg);
  101. }
  102. /* END: Checking for SUCCESSFUL */
  103. $step++;
  104. }
  105. echo "** Totally collected items ".count($collectmachteditems);
  106. $maxfoundeditems = count(array_keys($collectmachteditems));
  107. $numberofinstancesOfCategory = count(array_keys($instancesOfCategory));
  108. $CategoryStatisticDetails ["categoryname"] = $category_name;
  109. $CategoryStatisticDetails ["bestaccuracy"] = $bestaccuracy;
  110. $CategoryStatisticDetails ["lastaccuracy"] = $lastaccuracy;
  111. $CategoryStatisticDetails ["bestconcept"] = $bestconcept;
  112. $CategoryStatisticDetails ["lastconcept"] = $lastconcept;
  113. $CategoryStatisticDetails ["maxfoundeditems"] = $maxfoundeditems;
  114. $CategoryStatisticDetails ["recallMAX"] = ($maxfoundeditems/$numberofinstancesOfCategory) ; //
  115. $CategoryStatisticDetails ["recallBEST"] = ($best_count_matching_in_all_steps / $numberofinstancesOfCategory) ;
  116. //$CategoryStatisticDetails ["recallBEST"] = ($bestaccuracy / 100); // calculation follow the best accuracy
  117. $CategoryStatisticDetails ["recallAVG"] = ($allfoundeditems/($sum_step*$numberofinstancesOfCategory));
  118. $CategoryStatisticDetails ["precTOTAL"] = ($allfoundeditems/($sum_step*$limit)); //
  119. $CategoryStatisticDetails ["precBEST"] = ($best_count_matching_in_all_steps /$limit); //
  120. $CategoryStatisticDetails ["precAVG"] = ($allfoundeditems/($sum_step*$limit));
  121. $CategoryStatisticDetails ["lastsparql"] = $lastsparql; //
  122. echo "CategoryStatisticDetails \n";
  123. print_r($CategoryStatisticDetails);
  124. /* START: Adding data into sheet */
  125. include ("class.spreadsheet.php");
  126. $doc = new spreadsheet();
  127. $acc = array("dmlldHBsYWNlc0Bnb29nbGVtYWlsLmNvbQ==","ODIxNTIyODIxNTIy");
  128. $doc->authenticate(base64_decode($acc[0]), base64_decode($acc[1]));
  129. $doc->setSpreadsheet("MatchedResults");
  130. $doc->setWorksheet("Statistic-DL-Learner-$limit");
  131. // END initial a doc
  132. $my_data = array("Nr" => $count,
  133. "Category Name" => $CategoryStatisticDetails ["categoryname"],
  134. "Best Accuracy" => $CategoryStatisticDetails ["bestaccuracy"],
  135. "Last Accuracy" => $CategoryStatisticDetails ["lastaccuracy"],
  136. "Best Concept" => $CategoryStatisticDetails ["bestconcept"],
  137. "Last Concept" => $CategoryStatisticDetails ["lastconcept"],
  138. "#Max collected Members" => $CategoryStatisticDetails ["maxfoundeditems"],
  139. "recallMAX" => round($CategoryStatisticDetails ["recallMAX"],4),
  140. "recallBEST" => round($CategoryStatisticDetails ["recallBEST"],4),
  141. "recallAVG" => round($CategoryStatisticDetails ["recallAVG"],4) ,
  142. "precTOTAL" => round($CategoryStatisticDetails ["precTOTAL"],4),
  143. "precBEST" => round($CategoryStatisticDetails ["precBEST"],4),
  144. "precAVG" => round($CategoryStatisticDetails ["precAVG"],4),
  145. "Last SPARQL query" => $CategoryStatisticDetails ["lastsparql"]);
  146. $doc->add($my_data);
  147. /* END: Adding data into sheet */