Search in sources :

Example 11 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class OntologyTagServiceImpl method asTag.

private <SubjectType> SemanticTag<SubjectType, OntologyTerm, Ontology> asTag(SubjectType subjectType, Entity tagEntity) {
    String identifier = tagEntity.getString(TagMetadata.ID);
    Relation relation = asRelation(tagEntity);
    Ontology ontology = asOntology(tagEntity);
    OntologyTerm ontologyTerm = asOntologyTerm(tagEntity);
    if (relation == null || ontologyTerm == null) {
        return null;
    }
    return new SemanticTag<>(identifier, subjectType, relation, ontologyTerm, ontology);
}
Also used : Relation(org.molgenis.data.semantic.Relation) Ontology(org.molgenis.ontology.core.model.Ontology) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) SemanticTag(org.molgenis.data.semantic.SemanticTag)

Example 12 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class OntologyTagServiceImpl method addAttributeTag.

@Override
public OntologyTag addAttributeTag(String entity, String attribute, String relationIRI, List<String> ontologyTermIRIs) {
    boolean added = false;
    Entity attributeEntity = findAttributeEntity(entity, attribute);
    Tag tag = new Tag(tagMetadata);
    Stream<OntologyTerm> terms = ontologyTermIRIs.stream().map(ontologyService::getOntologyTerm);
    OntologyTerm combinedOntologyTerm = OntologyTerm.and(terms.toArray(OntologyTerm[]::new));
    Relation relation = Relation.forIRI(relationIRI);
    tag.setId(idGenerator.generateId());
    tag.setCodeSystem(null);
    tag.setRelationIri(relation.getIRI());
    tag.setRelationLabel(relation.getLabel());
    tag.setLabel(combinedOntologyTerm.getLabel());
    tag.setObjectIri(combinedOntologyTerm.getIRI());
    dataService.add(TAG, tag);
    Map<String, Entity> tags = Maps.newHashMap();
    for (Entity attrTag : attributeEntity.getEntities(AttributeMetadata.TAGS)) {
        tags.put(attrTag.get(TagMetadata.OBJECT_IRI).toString(), attrTag);
    }
    if (!tags.containsKey(tag.get(TagMetadata.OBJECT_IRI).toString())) {
        tags.put(tag.get(TagMetadata.OBJECT_IRI).toString(), tag);
        added = true;
    }
    attributeEntity.set(AttributeMetadata.TAGS, tags.values());
    dataService.update(ATTRIBUTE_META_DATA, attributeEntity);
    updateEntityTypeEntityWithNewAttributeEntity(entity, attribute, attributeEntity);
    return added ? OntologyTag.create(combinedOntologyTerm, relation) : null;
}
Also used : Entity(org.molgenis.data.Entity) Relation(org.molgenis.data.semantic.Relation) SemanticTag(org.molgenis.data.semantic.SemanticTag) OntologyTag(org.molgenis.semanticsearch.semantic.OntologyTag) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 13 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class SemanticSearchServiceImpl method findTags.

@Override
public Map<Attribute, Hit<OntologyTerm>> findTags(String entity, List<String> ontologyIds) {
    Map<Attribute, Hit<OntologyTerm>> result = new LinkedHashMap<>();
    EntityType emd = metaDataService.getEntityType(entity);
    for (Attribute amd : emd.getAtomicAttributes()) {
        Hit<OntologyTerm> tag = findTags(amd, ontologyIds);
        if (tag != null) {
            result.put(amd, tag);
        }
    }
    return result;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Hit(org.molgenis.semanticsearch.semantic.Hit) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm)

Example 14 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class SemanticSearchServiceImpl method bestMatchingSynonym.

/**
 * Computes the best matching synonym which is closest to a set of search terms.<br/>
 * Will stem the {@link OntologyTerm} 's synonyms and the search terms, and then compute the maximum
 * {@link StringDistance} between them. 0 means disjunct, 1 means identical
 *
 * @param ontologyTerm the {@link OntologyTerm}
 * @param searchTerms  the search terms
 * @return the maximum {@link StringDistance} between the ontologyterm and the search terms
 */
public Hit<String> bestMatchingSynonym(OntologyTerm ontologyTerm, Set<String> searchTerms) {
    Stemmer stemmer = new Stemmer();
    Optional<Hit<String>> bestSynonym = ontologyTerm.getSynonyms().stream().map(synonym -> Hit.create(synonym, distanceFrom(synonym, searchTerms, stemmer))).max(Comparator.naturalOrder());
    return bestSynonym.get();
}
Also used : NGramDistanceAlgorithm(org.molgenis.semanticsearch.string.NGramDistanceAlgorithm) java.util(java.util) StringDistance(org.apache.lucene.search.spell.StringDistance) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) SemanticSearchService(org.molgenis.semanticsearch.service.SemanticSearchService) QueryImpl(org.molgenis.data.support.QueryImpl) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) MetaDataService(org.molgenis.data.meta.MetaDataService) Lists(com.google.common.collect.Lists) FluentIterable(com.google.common.collect.FluentIterable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Objects.requireNonNull(java.util.Objects.requireNonNull) AttributeMetadata(org.molgenis.data.meta.model.AttributeMetadata) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) OntologyService(org.molgenis.ontology.core.service.OntologyService) Splitter(com.google.common.base.Splitter) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) Hit(org.molgenis.semanticsearch.semantic.Hit) Stemmer(org.molgenis.semanticsearch.string.Stemmer) Explanation(org.apache.lucene.search.Explanation) Logger(org.slf4j.Logger) ElasticSearchExplainService(org.molgenis.semanticsearch.explain.service.ElasticSearchExplainService) ExplainedQueryString(org.molgenis.semanticsearch.explain.bean.ExplainedQueryString) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Ontology(org.molgenis.ontology.core.model.Ontology) Stream(java.util.stream.Stream) Ordering(com.google.common.collect.Ordering) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) DataService(org.molgenis.data.DataService) Query(org.molgenis.data.Query) QueryRule(org.molgenis.data.QueryRule) Joiner(com.google.common.base.Joiner) Entity(org.molgenis.data.Entity) Hit(org.molgenis.semanticsearch.semantic.Hit) Stemmer(org.molgenis.semanticsearch.string.Stemmer)

Example 15 with OntologyTerm

use of org.molgenis.ontology.core.model.OntologyTerm in project molgenis by molgenis.

the class SemanticSearchServiceHelperTest method testCollectQueryTermsFromOntologyTerm.

@Test
public void testCollectQueryTermsFromOntologyTerm() {
    // Case 1
    OntologyTerm ontologyTerm1 = OntologyTerm.create("http://onto/standingheight", "Standing height", "Description is not used", singletonList("body_length"));
    List<String> actual_1 = semanticSearchServiceHelper.parseOntologyTermQueries(ontologyTerm1);
    assertEquals(actual_1, asList("length body", "standing height"));
    // Case 2
    OntologyTerm ontologyTerm2 = OntologyTerm.create("http://onto/standingheight", "height", "Description is not used", emptyList());
    OntologyTerm ontologyTerm3 = OntologyTerm.create("http://onto/standingheight-children", "length", singletonList("body_length"));
    when(ontologyService.getChildren(ontologyTerm2)).thenReturn(singletonList(ontologyTerm3));
    when(ontologyService.getOntologyTermDistance(ontologyTerm2, ontologyTerm3)).thenReturn(1);
    List<String> actual_2 = semanticSearchServiceHelper.parseOntologyTermQueries(ontologyTerm2);
    assertEquals(actual_2, asList("height", "length^0.5 body^0.5", "length^0.5"));
}
Also used : OntologyTerm(org.molgenis.ontology.core.model.OntologyTerm) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

OntologyTerm (org.molgenis.ontology.core.model.OntologyTerm)30 Test (org.testng.annotations.Test)13 Relation (org.molgenis.data.semantic.Relation)9 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)8 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)8 Attribute (org.molgenis.data.meta.model.Attribute)7 EntityType (org.molgenis.data.meta.model.EntityType)7 Ontology (org.molgenis.ontology.core.model.Ontology)7 QueryRule (org.molgenis.data.QueryRule)6 Entity (org.molgenis.data.Entity)5 Sets (com.google.common.collect.Sets)3 java.util (java.util)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang3.StringUtils)3 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)3 DataService (org.molgenis.data.DataService)3 Operator (org.molgenis.data.QueryRule.Operator)3 AttributeMetadata (org.molgenis.data.meta.model.AttributeMetadata)3 SemanticTag (org.molgenis.data.semantic.SemanticTag)3