Fix possessor noun synthesis
All checks were successful
studiorailgun/trpg/pipeline/head This commit looks good

This commit is contained in:
austin 2024-12-30 11:51:58 -05:00
parent 664ea1420e
commit cf3ede4882
7 changed files with 126 additions and 21 deletions

View File

@ -8,7 +8,6 @@ sitting in a tavern by a fireplace
Respond to "What color is your hat?"
- determine what is being queried for
- respond with the correct information

View File

@ -1,6 +1,9 @@
package org.studiorailgun.conversation.synthesis;
import org.studiorailgun.Globals;
import org.studiorailgun.knowledge.Node;
import org.studiorailgun.knowledge.query.InstanceQuery;
import org.studiorailgun.knowledge.query.PossessionQuery;
/**
* Synthesizes text for a noun stack
@ -15,6 +18,18 @@ public class NounStackSynthesizer {
public static String synthesize(Node node, boolean possessive){
String rVal = "";
Node mostSpecificConcept = InstanceQuery.getMostSpecificConcept(node);
rVal = mostSpecificConcept.getName();
if(possessive){
rVal = rVal.toLowerCase();
Node possessor = PossessionQuery.getPossessor(node);
if(possessor == Globals.web.getAnchors().getSelfNode()){
rVal = "My " + rVal;
}
}
return rVal;
}

View File

@ -5,7 +5,6 @@ import java.util.List;
import org.studiorailgun.conversation.evaluators.goal.GoalData;
import org.studiorailgun.conversation.evaluators.greet.GreetingData;
import org.studiorailgun.conversation.evaluators.query.QueryData;
import org.studiorailgun.knowledge.KnowledgeWeb;
import org.studiorailgun.knowledge.Node;

View File

@ -166,24 +166,7 @@ public class KnowledgeWeb {
if(rVal.anchors == null){
rVal.anchors = new AnchorNodes();
} else {
if(rVal.anchors.getSelf() != null){
int oldId = rVal.anchors.getSelf();
String lookupId = "node-" + rVal.getTag() + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
rVal.anchors.setSelf(newNodeId);
}
if(rVal.anchors.getConcept() != null){
int oldId = rVal.anchors.getConcept();
String lookupId = "node-" + rVal.getTag() + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
rVal.anchors.setConcept(newNodeId);
}
if(rVal.anchors.getQualia() != null){
int oldId = rVal.anchors.getQualia();
String lookupId = "node-" + rVal.getTag() + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
rVal.anchors.setQualia(newNodeId);
}
rVal.anchors.updateReferences(crossRelationLookupMap, rVal.getTag());
}
//parse children
if(rVal.getChildren() != null && rVal.getChildren().size() > 0){

View File

@ -1,5 +1,7 @@
package org.studiorailgun.knowledge.anchor;
import java.util.Map;
import org.studiorailgun.Globals;
import org.studiorailgun.knowledge.Node;
@ -23,6 +25,11 @@ public class AnchorNodes {
*/
Integer qualia;
/**
* The concept of a name
*/
Integer name;
/**
* Gets the self
* @return The self node
@ -104,6 +111,33 @@ public class AnchorNodes {
return qualia;
}
/**
* Gets the idea of a name
* @return The idea of a name
*/
public Node getNameNode() {
if(name == null){
return null;
}
return Globals.web.getNode(name);
}
/**
* Sets the name node
* @param id The name node
*/
public void setName(int id){
this.name = id;
}
/**
* Gets the id for the name node
* @return The id for the name node
*/
public Integer getName(){
return name;
}
/**
* Copies anchor nodes from the child to the parent
* @param parentAnchors The parent anchors
@ -119,6 +153,41 @@ public class AnchorNodes {
if(childAnchors.getQualia() != null){
parentAnchors.setQualia(childAnchors.getQualia());
}
if(childAnchors.getName() != null){
parentAnchors.setName(childAnchors.getName());
}
}
/**
* Updates the IDs for the anchor nodes based on the newly assigned IDs after loading
* @param crossRelationLookupMap The crossrelational lookup map
* @param tag The tag for the current web
*/
public void updateReferences(Map<String,Integer> crossRelationLookupMap, String tag){
if(this.getSelf() != null){
int oldId = this.getSelf();
String lookupId = "node-" + tag + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
this.setSelf(newNodeId);
}
if(this.getConcept() != null){
int oldId = this.getConcept();
String lookupId = "node-" + tag + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
this.setConcept(newNodeId);
}
if(this.getQualia() != null){
int oldId = this.getQualia();
String lookupId = "node-" + tag + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
this.setQualia(newNodeId);
}
if(this.getName() != null){
int oldId = this.getName();
String lookupId = "node-" + tag + oldId;
int newNodeId = crossRelationLookupMap.get(lookupId);
this.setName(newNodeId);
}
}
/**

View File

@ -4,7 +4,6 @@ import java.util.List;
import java.util.stream.Collectors;
import org.studiorailgun.Globals;
import org.studiorailgun.knowledge.KnowledgeWeb;
import org.studiorailgun.knowledge.Node;
import org.studiorailgun.knowledge.Relation;
import org.studiorailgun.knowledge.query.filter.NameQueryFilter;
@ -38,4 +37,32 @@ public class InstanceQuery {
return NameQueryFilter.withNodeName(concepts, conceptName);
}
/**
* Gets the most specific concept that the child is an instance of
* @param child The child
* @return The most specific concept that the child is an instance of
*/
public static Node getMostSpecificConcept(Node child){
//get all concepts
Node concept = Globals.web.getAnchors().getConceptNode();
List<Node> concepts = InstanceQuery.getInstances(concept);
//get the objects the child is an instance of
List<Relation> relations = Globals.web.getRelationsOfChildNode(child);
//filter to most specific concept
List<Node> conceptParents = relations.stream()
.filter(relation -> relation.getName().equals(RelationTypes.INSTANCE_OF))
.filter(relation -> concepts.contains(relation.getParent()))
.map(relation -> relation.getParent()).collect(Collectors.toList());
if(conceptParents.size() < 1){
throw new Error("Node somehow has no concept that it is an instance of! " + concept.getName());
}
//TODO: get most specific -- defaulting to first currently
return conceptParents.get(0);
}
}

View File

@ -21,4 +21,17 @@ public class PossessionQuery {
return Globals.web.getRelationsOfParentNode(parent).stream().filter(relation -> relation.getName().equals(RelationTypes.POSSESSION_OF)).map(relation -> relation.getChild()).collect(Collectors.toList());
}
/**
* Gets the node that possesses the child
* @param child The child
* @return The possessor's node
*/
public static Node getPossessor(Node child){
List<Node> nodes = Globals.web.getRelationsOfChildNode(child).stream().filter(relation -> relation.getName().equals(RelationTypes.POSSESSION_OF)).map(relation -> relation.getParent()).collect(Collectors.toList());
if(nodes.size() != 1){
throw new Error("Not just one possessor! " + nodes.size() + " " + child.getName());
}
return nodes.get(0);
}
}