author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2016 23:43:29 +0200 | |
changeset 304 | 20071981ba2a |
parent 158 | 366509ae2f37 |
permissions | -rw-r--r-- |
28 | 1 |
<?php |
2 |
namespace CorpusParole\Services; |
|
3 |
||
4 |
use Cache; |
|
5 |
use CorpusParole\Services\LexvoResolverInterface; |
|
6 |
||
7 |
class LexvoResolver implements LexvoResolverInterface { |
|
8 |
||
9 |
public function __construct($sparqlClient) { |
|
10 |
$this->sparqlClient = $sparqlClient; |
|
11 |
} |
|
12 |
||
13 |
private function checkLexvoId($id) { |
|
14 |
$lexvoid = $id; |
|
15 |
if(strpos($id, config('corpusparole.lexvo_base_url')) !== 0) { |
|
16 |
$lexvoid = config('corpusparole.lexvo_base_url').$id; |
|
17 |
} |
|
18 |
$lexvoid = rtrim($lexvoid, '/'); |
|
19 |
if(preg_match("/^".preg_quote(config('corpusparole.lexvo_base_url'),"/")."[[:alpha:]]{3}$/", $lexvoid) !== 1) { |
|
20 |
throw new LexvoResolverException("the provided id \"$id\" is not a Lexvo id"); |
|
21 |
} |
|
22 |
return $lexvoid; |
|
23 |
} |
|
133 | 24 |
|
28 | 25 |
/** |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
26 |
* Get name from Lexvo id |
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
27 |
* @param string $id The id to resolve. Can be an url starting with http://lexvo.org/ |
28 | 28 |
* @return a string with the name |
29 |
*/ |
|
30 |
public function getName($id) { |
|
31 |
$res = $this->getNames([$id,]); |
|
32 |
assert(array_key_exists($id,$res), "the result must contains $id"); |
|
33 |
return $res[$id]; |
|
34 |
} |
|
35 |
||
36 |
/** |
|
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
37 |
* Get a list of names from an array of Lexvo ids. |
28 | 38 |
* @param array $ids The array of ids to resolve. |
304
20071981ba2a
add location and geonames resolvers and api
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
39 |
* Each id can be an url starting with http://lexvo.org/ |
28 | 40 |
* @return array key is id, value is the name |
41 |
*/ |
|
42 |
public function getNames(array $ids) { |
|
43 |
||
133 | 44 |
if(count($ids) > config('corpusparole.lexvo_max_ids')) { |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
45 |
return array_reduce( |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
46 |
array_map([$this, 'getNames'], array_chunk($ids, config('corpusparole.lexvo_max_ids'))), |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
47 |
'array_merge', |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
48 |
[] |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
49 |
); |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
133
diff
changeset
|
50 |
// throw new LexvoResolverException("Too manys ids provided"); |
133 | 51 |
} |
52 |
||
28 | 53 |
$lexvoids = array_map([$this, 'checkLexvoId'], $ids); |
54 |
$lexvoidsMap = array_combine($lexvoids, $ids); |
|
55 |
||
56 |
$results = []; |
|
57 |
$missingLexvoids = []; |
|
58 |
||
59 |
foreach ($lexvoidsMap as $lexvoid => $lexvoidSource) { |
|
60 |
$cachedValue = Cache::get("lexvo:$lexvoid"); |
|
61 |
if(is_null($cachedValue)) { |
|
62 |
array_push($missingLexvoids, $lexvoid); |
|
63 |
} else { |
|
64 |
$results[$lexvoidSource] = $cachedValue; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
if(count($missingLexvoids) == 0) { |
|
69 |
return $results; |
|
70 |
} |
|
71 |
||
72 |
$query = "SELECT ?s ?o WHERE {"; |
|
73 |
foreach ($missingLexvoids as $index => $lid) { |
|
74 |
if($index > 0) { |
|
75 |
$query .= " UNION "; |
|
76 |
} |
|
77 |
$query .= "{<$lid> rdfs:label ?o. ?s rdfs:label ?o FILTER(?s = <$lid> && (lang(?o) = \"fr\" || lang(?o) = \"en\"))}"; |
|
78 |
} |
|
79 |
$query .= "}"; |
|
80 |
||
81 |
||
82 |
$docs = $this->sparqlClient->query($query); |
|
83 |
||
84 |
$resultsRaw = []; |
|
85 |
||
86 |
foreach ($docs as $doc) { |
|
87 |
$lexvoid = $doc->s->getUri(); |
|
88 |
$lexvoname = $doc->o; |
|
89 |
||
90 |
$lang = $lexvoname->getLang(); |
|
91 |
$value = $lexvoname->getValue(); |
|
92 |
||
93 |
if(!array_key_exists($lexvoid, $resultsRaw) || |
|
94 |
($lang == "fr" && ($resultsRaw[$lexvoid]->getLang() == "en" || mb_strlen($resultsRaw[$lexvoid]->getValue()) > mb_strlen($value))) || |
|
95 |
($lang == "en" && $resultsRaw[$lexvoid]->getLang() == "en" && mb_strlen($resultsRaw[$lexvoid]->getValue()) > mb_strlen($value)) ) { |
|
96 |
$resultsRaw[$lexvoid] = $lexvoname; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
foreach ($missingLexvoids as $lexvoid) { |
|
101 |
$lexvoidSource = $lexvoidsMap[$lexvoid]; |
|
102 |
$missingValue = array_key_exists($lexvoid,$resultsRaw) && $resultsRaw[$lexvoid]?mb_strtolower($resultsRaw[$lexvoid]->getValue()):""; |
|
103 |
Cache::put("lexvo:$lexvoid", $missingValue, config('corpusparole.lexvo_cache_expiration')); |
|
104 |
$results[$lexvoidSource] = mb_strlen($missingValue)>0?$missingValue:null; |
|
105 |
} |
|
106 |
||
107 |
return $results; |
|
108 |
||
109 |
} |
|
110 |
||
111 |
} |