package org.studiorailgun.ai.linguistics;

import java.util.List;
import java.util.stream.Collectors;

import org.studiorailgun.Globals;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.query.InstanceQuery;
import org.studiorailgun.ai.philosophy.ConceptQuery;

/**
 * Queries related to proper nouns
 */
public class ProperNounQuery {
    
    /**
     * Tries to locate a proper noun
     * @param properNoun The proper noun
     * @return The node representing the proper noun
     */
    public static Node getProperNoun(String properNoun){
        Node rVal = null;
        List<Node> nodes = Globals.web.getNodesList().stream().filter(node -> node.getName().equals(properNoun)).collect(Collectors.toList());
        if(nodes.size() < 1){
            //create a name node to represent this name
            rVal = Globals.web.createNode(properNoun);
            //make this an instance of a name (it is a proper noun, by definition it is a name)
            Node nameConcept = ConceptQuery.getConcept("name");
            InstanceQuery.setInstance(nameConcept, rVal);
        } else if(nodes.size() == 1){
            rVal = nodes.get(0);
        } else {
            throw new Error("More than one proper match found! " + nodes.size());
        }
        return rVal;
    }

}
