learnable quality, possession working, web fixes
All checks were successful
studiorailgun/trpg/pipeline/head This commit looks good

This commit is contained in:
austin 2024-12-31 18:11:13 -05:00
parent 05122a844e
commit 0f5abe384f
9 changed files with 131 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import org.studiorailgun.ai.conversation.tracking.Conversation;
import org.studiorailgun.ai.conversation.tracking.Quote;
import org.studiorailgun.ai.conversation.tracking.Sentence;
import org.studiorailgun.ai.conversation.web.ArgumentQuery;
import org.studiorailgun.ai.conversation.web.MetaQuery;
import org.studiorailgun.ai.knowledge.Node;
import edu.stanford.nlp.ling.IndexedWord;
@ -101,6 +102,15 @@ public class TransferEval {
throw new Error("Error querying object!");
}
//make an equivalence link here if it doesn't exist already
if(MetaQuery.isLearnable(subjectNode)){
} else if(MetaQuery.isLearnable(objectNode)){
} else {
String message = "Information already stored?\n" +
sentence.getGraph();
throw new Error(message);
}
//for the moment, assume the other noun is a concept

View File

@ -10,6 +10,7 @@ import org.studiorailgun.ai.conversation.tracking.Quote;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.Relation;
import org.studiorailgun.ai.knowledge.query.InstanceQuery;
import org.studiorailgun.ai.knowledge.query.PossessionQuery;
import org.studiorailgun.ai.knowledge.types.RelationTypes;
import org.studiorailgun.ai.linguistics.ProperNounQuery;
import org.studiorailgun.ai.philosophy.ConceptQuery;
@ -55,7 +56,7 @@ public class ArgumentQuery {
* @return The node that represents the possession modifier applied to the root node
*/
private static Node getPossessionModified(Quote quote, Node rootNode, Argument argument){
Node rVal = rootNode;
Node speaker = QuoteQuery.getSpeaker(quote.getNode());
String possessionModifier = argument.getPossessiveModifier().originalText().toLowerCase();
switch(possessionModifier){
case "my": {
@ -67,19 +68,23 @@ public class ArgumentQuery {
}).collect(Collectors.toList());
if(possessedInstances.size() == 1){
//already exists, return it
rVal = possessedInstances.get(0);
return possessedInstances.get(0);
} else if(possessedInstances.size() > 1) {
//already exists, return it
throw new Error("Can't currently handle plural instances!");
} else {
//does not already exist, see if we can generate a node to encapsulate it
Node newInstance = Globals.web.createNode(rootNode.getName() + "(inst)");
InstanceQuery.setInstance(rootNode, newInstance); // ie (inst) <--instanceOf-- "name"
MetaQuery.flagAsLearnable(newInstance); // ie (inst) <--qualityOf-- "learnable"
PossessionQuery.setPossessor(speaker, newInstance); // ie (inst) <--possessionOf-- (person instance)
return newInstance;
}
}
} break;
default: {
throw new Error("Unsupported possession modifier! " + possessionModifier);
}
}
return rVal;
}
}

View File

@ -0,0 +1,43 @@
package org.studiorailgun.ai.conversation.web;
import java.util.List;
import java.util.stream.Collectors;
import org.studiorailgun.Globals;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.Relation;
import org.studiorailgun.ai.knowledge.types.RelationTypes;
/**
* Queries about meta information about nodes
*/
public class MetaQuery {
/**
* Flags a node as learnable
* @param node The node
*/
public static void flagAsLearnable(Node node){
Node learnable = Globals.web.getAnchors().getLearnableNode();
Globals.web.createRelation(RelationTypes.QUALITY_OF, learnable, node);
}
/**
* Checks if this is a learnable node
* @param node The node
* @return true if it is learnable, false otherwise
*/
public static boolean isLearnable(Node node){
Node learnableNode = Globals.web.getAnchors().getLearnableNode();
List<Relation> relations = Globals.web.getRelationsOfChildNode(node);
List<Node> discoveredLearnable = relations.stream()
.filter(relation -> relation.getName().equals(RelationTypes.QUALITY_OF))
.map(relation -> relation.getParent())
.filter(parentNode -> parentNode.equals(learnableNode))
.collect(Collectors.toList());
return discoveredLearnable.size() > 0;
}
}

View File

@ -5,7 +5,6 @@ import java.util.stream.Collectors;
import org.studiorailgun.Globals;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.Relation;
import org.studiorailgun.ai.knowledge.types.RelationTypes;
/**
@ -22,6 +21,24 @@ public class QuoteQuery {
Globals.web.createRelation(RelationTypes.QUOTE_OF, speaker, quote);
}
/**
* Gets the speaker of a quote
* @param quote The quote
* @return The speaker of the quote
*/
public static Node getSpeaker(Node quote){
List<Node> speaker = Globals.web.getRelationsOfChildNode(quote).stream()
.filter(relation -> relation.getName().equals(RelationTypes.QUOTE_OF))
.map(relation -> relation.getParent())
.collect(Collectors.toList());
if(speaker.size() == 0){
throw new Error("No speaker defined!");
} else if(speaker.size() > 1){
throw new Error("Unhandled number of speakers! " + speaker.size());
}
return speaker.get(0);
}
/**
* Assigns a conversation to a quote
* @param conversation The conversation
@ -31,18 +48,4 @@ public class QuoteQuery {
Globals.web.createRelation(RelationTypes.MEMBER_OF, conversation, quote);
}
/**
* Gets the speaker of a quote
* @param quote The quote
* @return The speaker of the quote
*/
public static Node getSpeaker(Node quote){
List<Relation> relations = Globals.web.getRelationsOfChildNode(quote);
List<Node> speakers = relations.stream().filter(relation -> relation.getName().equals(RelationTypes.QUOTE_OF)).map(relation -> relation.getParent()).collect(Collectors.toList());
if(speakers.size() != 1){
throw new Error("Invalid number of speakers! " + speakers.size());
}
return speakers.get(0);
}
}

View File

@ -364,6 +364,34 @@ public class KnowledgeWeb {
public Relation createRelation(String name, Node parent, Node child){
Relation relation = new Relation(name, parent, child);
this.relations.put(relation.getId(), relation);
if(typeRelationLookup.containsKey(relation.getName())){
typeRelationLookup.get(relation.getName()).add(relation.getId());
} else {
LinkedList<Integer> newList = new LinkedList<Integer>();
newList.add(relation.getId());
typeRelationLookup.put(relation.getName(),newList);
}
//add to child tracking
List<Relation> childRelations = this.childRelations.get(child);
if(childRelations == null){
childRelations = new LinkedList<Relation>();
childRelations.add(relation);
this.childRelations.put(child, childRelations);
} else {
childRelations.add(relation);
}
//add to parent tracking
List<Relation> parentRelations = this.parentRelations.get(child);
if(parentRelations == null){
parentRelations = new LinkedList<Relation>();
parentRelations.add(relation);
this.parentRelations.put(child, parentRelations);
} else {
parentRelations.add(relation);
}
return relation;
}

View File

@ -24,4 +24,13 @@ public class InstanceQuery {
return relations.stream().filter(relation -> relation.getName().equals(RelationTypes.INSTANCE_OF)).map(relation -> relation.getChild()).collect(Collectors.toList());
}
/**
* Creates an instance relationship
* @param concept The concept
* @param instance The instance
*/
public static void setInstance(Node concept, Node instance){
Globals.web.createRelation(RelationTypes.INSTANCE_OF, concept, instance);
}
}

View File

@ -34,4 +34,16 @@ public class PossessionQuery {
return nodes.get(0);
}
/**
* Sets the node that owns the possession
* @param possessor The owner
* @param possession The possession
*/
public static void setPossessor(Node possessor, Node possession){
if(possessor == null || possession == null){
throw new Error("Undefined arguments " + possessor + " " + possession);
}
Globals.web.createRelation(RelationTypes.POSSESSION_OF, possessor, possession);
}
}

View File

@ -10,7 +10,6 @@ public class RelationTypes {
*/
public static final String INSTANCE_OF = "instanceOf";
/**
* A possession relationship
*/

View File

@ -24,6 +24,8 @@ public class TransferEvaluationTests {
Quote input = new Quote("My name is John.");
NLPParser.parse(input);
Globals.conversation.addQuote(ConversationQuery.getOtherParticipant(), input);
TransferEval.evaluate(Globals.conversation, input, input.getSentences().get(0));
//search for the name being assigned in the knowledge web