All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.cmu.sv.database.Ontology Maven / Gradle / Ivy

Go to download

A library that allows rapid prototyping of dialog systems (language understanding, discourse modelling, dialog management, language generation).

There is a newer version: 0.7.0
Show newest version
package edu.cmu.sv.database;

import edu.cmu.sv.domain.OntologyRegistry;
import edu.cmu.sv.domain.ontology.*;
import edu.cmu.sv.domain.yoda_skeleton.YodaSkeletonOntologyRegistry;
import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.*;

/**
 * Created by David Cohen on 9/22/14.
 */
public class Ontology {
    public static Set roles = new HashSet<>();
    public static Set verbs = new HashSet<>();
    public static Set nouns = new HashSet<>();
    public static Set qualities = new HashSet<>();
    public static Set qualityDegrees = new HashSet<>();

    public static Map roleNameMap = new HashMap<>();
    public static Map verbNameMap = new HashMap<>();
    public static Map nounNameMap = new HashMap<>();
    public static Map qualityNameMap = new HashMap<>();
    public static Map qualityDegreeNameMap = new HashMap<>();
    public static Map thingNameMap = new HashMap<>();

    public static Map> qualitiesForClass = new HashMap<>();

    public static void loadOntologyRegistry(OntologyRegistry registry){
        verbs.addAll(registry.getVerbs());
        nouns.addAll(registry.getNouns());
        qualityDegrees.addAll(registry.getQualityDegrees());
        qualities.addAll(registry.getQualities());
        roles.addAll(registry.getRoles());
    }


    public static void finalizeOntology() {

        // create has-quality roles and inverse has-quality roles
        for (Quality qualityClass : qualities){
            Role hasQualityRole = new Role("Has"+qualityClass.name, true, false);
            hasQualityRole.getDomain().addAll(Arrays.asList(qualityClass.firstArgumentClassConstraint));
            hasQualityRole.getRange().addAll(Arrays.asList(qualityClass.secondArgumentClassConstraint));
            roles.add(hasQualityRole);

            if (qualityClass.secondArgumentClassConstraint!=null) {
                Role inverseHasQualityRole = new Role("InverseHas" + qualityClass.name, true, true);
                inverseHasQualityRole.getRange().addAll(Arrays.asList(qualityClass.firstArgumentClassConstraint));
                inverseHasQualityRole.getDomain().addAll(Arrays.asList(qualityClass.secondArgumentClassConstraint));
                roles.add(inverseHasQualityRole);
            }
        }

        // finalize Roles
        for (Role role : roles){
            role.getDomain().addAll(Arrays.asList(YodaSkeletonOntologyRegistry.unknownThingWithRoles));
        }

        // populate name maps
        verbs.forEach(x -> verbNameMap.put(x.name, x));
        roles.forEach(x -> roleNameMap.put(x.name, x));
        nouns.forEach(x -> nounNameMap.put(x.name, x));
        qualities.forEach(x -> qualityNameMap.put(x.name, x));
        qualityDegrees.forEach(x -> qualityDegreeNameMap.put(x.name, x));

        verbs.forEach(x -> thingNameMap.put(x.name, x));
        roles.forEach(x -> thingNameMap.put(x.name, x));
        nouns.forEach(x -> thingNameMap.put(x.name, x));
        qualityDegrees.forEach(x -> thingNameMap.put(x.name, x));
        qualities.forEach(x -> thingNameMap.put(x.name, x));

        // register qualities for class
        nouns.stream().forEach(x -> qualitiesForClass.put(x, new HashSet<>()));
        for (Quality quality : qualities){
            for (Noun noun: nouns){
                if (nounInherits(quality.firstArgumentClassConstraint, noun))
                    qualitiesForClass.get(noun).add(quality);
            }
        }
    }

    public static boolean nounInherits(Noun parent, Noun child){
        if (parent==child)
            return true;
        Noun directParent = child.directParent;
        while (directParent != null){
            if (directParent==parent)
                return true;
            directParent = directParent.directParent;
        }
        return false;
    }

    // return a number from 0 to 1 which estimates the likelihood that some linguistic constraint
    // would be used to refer to the filler of a role which has some ontological constraint
    public static double semanticConstraintMatch(Noun linguisticConstraint, Noun ontologicalConstraint){
        if (linguisticConstraint==null || ontologicalConstraint==null)
            return 0.0;
        else if (linguisticConstraint==ontologicalConstraint)
            return 1.0;
        else
            return Double.max(.75*semanticConstraintMatch(linguisticConstraint.directParent, ontologicalConstraint),
                    .75*semanticConstraintMatch(linguisticConstraint, ontologicalConstraint.directParent));
    }

    /*
    * Returns the HasQualityRole, and the set of QualityDegree instances
    * corresponding to a given quality class
    * */
    public static Pair> qualityDescriptors(Quality quality){
        Role qualityRole = roleNameMap.get("Has"+quality.name);
        Set qualityDegrees = new HashSet<>(quality.getQualityDegrees());
        return new ImmutablePair<>(qualityRole, qualityDegrees);
    }

    public static boolean inDomain(Role roleInstance, Object subjectClass){
        if (subjectClass instanceof Verb){
            return roleInstance.getDomain().contains(subjectClass);
        } else if (subjectClass instanceof Noun){
            return roleInstance.getDomain().stream().filter(x -> x instanceof Noun).
                    anyMatch(x -> nounInherits((Noun) x, (Noun) subjectClass));
        }
        throw new ValueException("Ontology.inDomain. Can't handle this request:" + roleInstance.name + ", "+subjectClass.getClass().getSimpleName());
    }


    public static boolean inRange(Role roleInstance, Object subjectClass){
        if (subjectClass instanceof Verb){
            return roleInstance.getRange().contains(subjectClass);
        } else if (subjectClass instanceof Noun){
            return roleInstance.getRange().stream().filter(x -> x instanceof Noun).
                    anyMatch(x -> nounInherits((Noun) x, (Noun) subjectClass));
        }
        throw new ValueException("Ontology.inRange. Can't handle this request:" + roleInstance.name + ", "+subjectClass.getClass().getSimpleName());
    }

    public static String webResourceWrap(String URI){
        return  "{\"class\": \""+ YodaSkeletonOntologyRegistry.webResource.name+"\", \"HasURI\":\""+URI+"\"}";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy