Skip to content
Snippets Groups Projects
Commit 9926ae68 authored by Gael PICOT's avatar Gael PICOT
Browse files

séparation search en Search, Result et Results

parent 6e85ac61
No related branches found
No related tags found
No related merge requests found
<?php
namespace uga\idoine\search;
use stdClass;
require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php';
/**
*
* Resultat d'une recherche sur une des divers API de recherche.
*
* @author Gaël PICOT
*
* iDOIne :
* Copyright (C) 2022 UGA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* Décrit un élément trouver
*/
class Result {
/**
* URL vers le résultat
*
* @var string
*/
private string $URL;
/**
* Le DOI trouvé
*
* @var string
*/
private string $DOI;
/**
* Liste des noms des auteurs
*
* @var array
*/
private array $authors;
/**
* Titre principal trouver
*
* @var string
*/
private string $title;
/**
* Journal de publication
*
* @var string
*/
private string $journal;
/**
* Date de publication
*
* @var string
*/
private string $publicationDate;
/**
* Avertissement
*
* @var string
*/
private string $warning;
/**
* Ressemblance avec le titre de la publication Hal en pourcentage.
*
* @var float
*/
private float $scorePercent = 0;
/**
* Constructeur
*
* @param stdClass $data
*/
public function __construct(stdClass $data) {
$this->URL = (isset($data->URL))?$data->URL:'';
$this->DOI = (isset($data->DOI))?$data->DOI:'';
$this->authors = (isset($data->authors))?$data->authors:[];
$this->title = (isset($data->title))?$data->title:'';
$this->publicationDate = (isset($data->publicationDate))?$data->publicationDate:'';
$this->journal = (isset($data->journal))?$data->journal:'';
$this->warning = (isset($data->warning))?$data->warning:'';
}
/**
* Accesseur pour le titre
*
* @return string
*/
public function getTitle():string {
return $this->title;
}
/**
* Revoie le résultat sous forme d'une string JSON.
*
* @return string
*/
public function toJSON(): string{
return json_encode($this->toArray());
}
/**
* Construi un tableau représentant le resultat.
*
* @return array
*/
public function toArray(): array{
$data = [
'URL' => $this->URL,
'DOI' => $this->DOI,
'authors' => $this->authors,
'title' => $this->title,
'journal' => $this->journal,
'publicationDate' => $this->publicationDate,
'scorePercent' => $this->scorePercent,
'warning' => $this->warning
];
return $data;
}
/**
* Modiffieur de $scorePercent
*
* @param float $scorePercent
* @return void
*/
public function setScorePercent(float $scorePercent) {
$this->scorePercent = $scorePercent;
}
}
<?php
namespace uga\idoine\search;
use stdClass;
require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php';
/**
*
* Ensemble de Resultats d'une recherche sur une des divers API de recherche.
*
* @author Gaël PICOT
*
* iDOIne :
* Copyright (C) 2022 UGA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* Décrit les résultats d'une recherche
*/
class Results {
/**
* Document Hal objet de la recherche
*
* @var string
*/
private string $halid;
/**
* Donnée extraite de Hal au sujet du document chercher
*
* @var stdClass|null
*/
private ?stdClass $halData = null;
/**
* Liste des résultats trouver
*
* @var array
*/
private array $resultList = [];
/**
* Meilleur score
*
* @var float
*/
private float $bestScore = 0;
/**
* Meilleur résultat trouver
*
* @var Result|null
*/
private ?Result $bestResult = null;
/**
* Meilleur score résultat en poursentage
*
* @var integer
*/
private int $bestScorePersent = 0;
/**
* Charge les donnée à partir de l'API Hal
*
* @param string $fl liste des champs cherché dans l'API HAl.
* @return stdClass
*/
public function getHalData(string $fl='title_s,journalTitle_s'): stdClass {
if($this->halData == null) {
$halQuery="https://api.archives-ouvertes.fr/search/?q=halId_s%3A".$this->halid."&fl=".$fl;
$this->halData = json_decode(file_get_contents($halQuery))->response->docs[0];
}
return $this->halData;
}
/**
* Accés au titres Hal
*
* @return array
*/
public function getHalTitles():array {
return $this->getHalData()->title_s;
}
/**
* Constructeur
*
* @param string $halid
*/
public function __construct(string $halid)
{
$this->halid = $halid;
}
/**
* Ajouté un résultat
*
* @param Result $newResult
* @return void
*/
public function addResult(Result $newResult) {
array_push($this->resultList, $newResult);
$percent = 0;
$score = similar_text(strtolower($this->getHalTitles()[0]), strtolower($newResult->getTitle()), $percent);
$newResult->setScorePercent($percent);
if($score > $this->bestScore||$this->bestScore==0) {
$this->bestResult = $newResult;
$this->bestScore = $score;
}
}
/**
* Construit une représentaion sous forme de tableau.
*
* @return array
*/
public function toArray(): array{
$data = [
'bestScorePercent' => (isset($this->bestScorePersent))?$this->bestScorePersent:0,
'resultList' => [],
];
if($this->bestResult != null) {
$data['bestResult'] = $this->bestResult->toArray();
}
foreach($this->resultList as $result){
array_push($data['resultList'], $result->toArray());
}
return $data;
}
/**
* Construit une représentaion au format JSON
*
* @return string
*/
public function toJSON(): string {
return json_encode($this->toArray());
}
}
\ No newline at end of file
......@@ -2,9 +2,16 @@
namespace uga\idoine\search;
use uga\hallib\OneDocQuery;
use uga\hallib\queryDefinition\LiteralElement;
use uga\hallib\search\SearchField;
use uga\hallib\search\SearchQuery;
require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php';
/**
*
* Commun au divers API de recherche
* Recherche d'un document Hal dans d'autre service
*
* @author Gaël PICOT
*
......@@ -26,14 +33,6 @@ namespace uga\idoine\search;
*
*/
use stdClass;
use uga\hallib\OneDocQuery;
use uga\hallib\queryDefinition\LiteralElement;
use uga\hallib\search\SearchField;
use uga\hallib\search\SearchQuery;
require_once dirname(__FILE__, 2).DIRECTORY_SEPARATOR.'vendor/autoload.php';
/**
* Recherche d'un document Hal dans d'autre service (CrossRef...)
*/
......@@ -201,240 +200,3 @@ class Search {
}
}
/**
* Décrit un élément trouver
*/
class Result {
/**
* URL vers le résultat
*
* @var string
*/
private string $URL;
/**
* Le DOI trouvé
*
* @var string
*/
private string $DOI;
/**
* Liste des noms des auteurs
*
* @var array
*/
private array $authors;
/**
* Titre principal trouver
*
* @var string
*/
private string $title;
/**
* Journal de publication
*
* @var string
*/
private string $journal;
/**
* Date de publication
*
* @var string
*/
private string $publicationDate;
/**
* Avertissement
*
* @var string
*/
private string $warning;
/**
* Ressemblance avec le titre de la publication Hal en pourcentage.
*
* @var float
*/
private float $scorePercent = 0;
/**
* Constructeur
*
* @param stdClass $data
*/
public function __construct(stdClass $data) {
$this->URL = (isset($data->URL))?$data->URL:'';
$this->DOI = (isset($data->DOI))?$data->DOI:'';
$this->authors = (isset($data->authors))?$data->authors:[];
$this->title = (isset($data->title))?$data->title:'';
$this->publicationDate = (isset($data->publicationDate))?$data->publicationDate:'';
$this->journal = (isset($data->journal))?$data->journal:'';
$this->warning = (isset($data->warning))?$data->warning:'';
}
/**
* Accesseur pour le titre
*
* @return string
*/
public function getTitle():string {
return $this->title;
}
/**
* Revoie le résultat sous forme d'une string JSON.
*
* @return string
*/
public function toJSON(): string{
return json_encode($this->toArray());
}
/**
* Construi un tableau représentant le resultat.
*
* @return array
*/
public function toArray(): array{
$data = [
'URL' => $this->URL,
'DOI' => $this->DOI,
'authors' => $this->authors,
'title' => $this->title,
'journal' => $this->journal,
'publicationDate' => $this->publicationDate,
'scorePercent' => $this->scorePercent,
'warning' => $this->warning
];
return $data;
}
/**
* Modiffieur de $scorePercent
*
* @param float $scorePercent
* @return void
*/
public function setScorePercent(float $scorePercent) {
$this->scorePercent = $scorePercent;
}
}
/**
* Décrit les résultats d'une recherche
*/
class Results {
/**
* Document Hal objet de la recherche
*
* @var string
*/
private string $halid;
/**
* Donnée extraite de Hal au sujet du document chercher
*
* @var stdClass|null
*/
private ?stdClass $halData = null;
/**
* Liste des résultats trouver
*
* @var array
*/
private array $resultList = [];
/**
* Meilleur score
*
* @var float
*/
private float $bestScore = 0;
/**
* Meilleur résultat trouver
*
* @var Result|null
*/
private ?Result $bestResult = null;
/**
* Meilleur score résultat en poursentage
*
* @var integer
*/
private int $bestScorePersent = 0;
/**
* Charge les donnée à partir de l'API Hal
*
* @param string $fl liste des champs cherché dans l'API HAl.
* @return stdClass
*/
public function getHalData(string $fl='title_s,journalTitle_s'): stdClass {
if($this->halData == null) {
$halQuery="https://api.archives-ouvertes.fr/search/?q=halId_s%3A".$this->halid."&fl=".$fl;
$this->halData = json_decode(file_get_contents($halQuery))->response->docs[0];
}
return $this->halData;
}
/**
* Accés au titres Hal
*
* @return array
*/
public function getHalTitles():array {
return $this->getHalData()->title_s;
}
/**
* Constructeur
*
* @param string $halid
*/
public function __construct(string $halid)
{
$this->halid = $halid;
}
/**
* Ajouté un résultat
*
* @param Result $newResult
* @return void
*/
public function addResult(Result $newResult) {
array_push($this->resultList, $newResult);
$percent = 0;
$score = similar_text(strtolower($this->getHalTitles()[0]), strtolower($newResult->getTitle()), $percent);
$newResult->setScorePercent($percent);
if($score > $this->bestScore||$this->bestScore==0) {
$this->bestResult = $newResult;
$this->bestScore = $score;
}
}
/**
* Construit une représentaion sous forme de tableau.
*
* @return array
*/
public function toArray(): array{
$data = [
'bestScorePercent' => (isset($this->bestScorePersent))?$this->bestScorePersent:0,
'resultList' => [],
];
if($this->bestResult != null) {
$data['bestResult'] = $this->bestResult->toArray();
}
foreach($this->resultList as $result){
array_push($data['resultList'], $result->toArray());
}
return $data;
}
/**
* Construit une représentaion au format JSON
*
* @return string
*/
public function toJSON(): string {
return json_encode($this->toArray());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment