Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve guessVocabularyFromURI #870

Merged
merged 2 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions model/Concept.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ public function getProperties()
// shortening property labels if possible
$prop = $sprop = EasyRdf\RdfNamespace::shorten($prop);
} else {
// EasyRdf requires full URIs to be in angle brackets
$sprop = "<$prop>";
}
// EasyRdf requires full URIs to be in angle brackets

if (!in_array($prop, $this->DELETED_PROPERTIES) || ($this->isGroup() === false && $prop === 'skos:member')) {
// retrieve property label and super properties from the current vocabulary first
Expand Down Expand Up @@ -596,13 +596,19 @@ public function getProperties()
}

if (isset($ret[$prop])) {
// checking if the property value is not in the current vocabulary
$exvoc = $this->model->guessVocabularyFromURI($val->getUri(), $this->vocab->getId());
if ($exvoc && $exvoc->getId() !== $this->vocab->getId()) {
$ret[$prop]->addValue(new ConceptMappingPropertyValue($this->model, $this->vocab, $val, $this->resource, $prop, $this->clang), $this->clang);
continue;
// create a ConceptPropertyValue first, assuming the resource exists in current vocabulary
$value = new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang);
// check whether we know the label of the resource
$label = $value->getLabel('', 'null');
if ($label === null) {
// we don't know a label for the resource
// checking if the property value is not in the current vocabulary
$exvoc = $this->model->guessVocabularyFromURI($val->getUri(), $this->vocab->getId());
if ($exvoc && $exvoc->getId() !== $this->vocab->getId()) {
$value = new ConceptMappingPropertyValue($this->model, $this->vocab, $val, $this->resource, $prop, $this->clang);
}
}
$ret[$prop]->addValue(new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang), $this->clang);
$ret[$prop]->addValue($value, $this->clang);
}

}
Expand Down
12 changes: 8 additions & 4 deletions model/ConceptPropertyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getLang()
return $this->getEnvLang();
}

public function getLabel($lang = '')
public function getLabel($lang = '', $fallback = 'uri')
{
if ($this->clang) {
$lang = $this->clang;
Expand Down Expand Up @@ -68,9 +68,13 @@ public function getLabel($lang = '')
} elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language
return $this->resource->getLiteral('rdf:value');
}
// uri if no label is found
$label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
return $label;

if ($fallback == 'uri') {
// return uri if no label is found
$label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
return $label;
}
return null;
}

public function getType()
Expand Down
20 changes: 14 additions & 6 deletions model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,13 @@ public function searchConcepts($params)

$hit['voc'] = $hitvoc;

// if uri is a external vocab uri that is included in the current vocab
$realvoc = $this->guessVocabularyFromURI($hit['uri'], $voc !== null ? $voc->getId() : null);
if ($realvoc !== $hitvoc) {
unset($hit['localname']);
$hit['exvocab'] = ($realvoc !== null) ? $realvoc->getId() : "???";
if (!$hitvoc->containsURI($hit['uri'])) {
// if uri is a external vocab uri that is included in the current vocab
$realvoc = $this->guessVocabularyFromURI($hit['uri'], $voc !== null ? $voc->getId() : null);
if ($realvoc !== $hitvoc) {
unset($hit['localname']);
$hit['exvocab'] = ($realvoc !== null) ? $realvoc->getId() : "???";
}
}

$ret[] = $hit;
Expand Down Expand Up @@ -468,7 +470,13 @@ private function disambiguateVocabulary($vocabs, $uri, $preferredVocabId = null)
if($preferredVocabId != null) {
foreach ($vocabs as $vocab) {
if($vocab->getId() == $preferredVocabId) {
return $vocab;
// double check that a label exists in the preferred vocabulary
if ($vocab->getConceptLabel($uri, null) !== null) {
return $vocab;
} else {
// not found in preferred vocabulary, fall back to next method
break;
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions model/Vocabulary.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ public function getUriSpace()
return $this->urispace;
}

/**
* Return true if the URI is within the URI space of this vocabulary.
*
* @param string full URI of concept
* @return bool true if URI is within URI namespace, false otherwise
*/

public function containsURI($uri)
{
return (strpos($uri, $this->getUriSpace()) === 0);
}

/**
* Get the full URI of a concept in a vocabulary. If the passed local
* name is already a full URI, return it unchanged.
Expand Down