def handleCommand(cmd, entities=gEntities, relations=gRelations): # respond to a command from the user cmd = string.lower(cmd) words = string.split(cmd) if words[0] == 'entity' or words[0] == 'e': entities[words[1]] = Entity(words[1]) elif words[0] == 'relation' or words[0] == 'r': trans = AskYesNo("Transitive?") opp = string.lower(raw_input("Opposite? ")) relations[words[1]] = Relation(words[1],trans) if opp: relations[opp] = Relation(opp,trans, \ relations[words[1]]) elif words[0] == 'list': print "Entities:", tostr(entities.keys()) print "Relations:", tostr(relations.keys()) else: agent = entities[words[0]] relation = relations[words[1]] if words[2][-1] == '?': object = entities[words[2][:-1]] handleQuestion( agent, relation, object ) else: object = entities[words[2]] handleStatement( agent, relation, object )
def create_network(movies, genres, ratings): facts = [] for movie_id in movies: movie_genres = get_genres_for_movie(int(tostr(movie_id))) m = get_movie_entity(tostr(movie_id), movies) for g in movie_genres: genre_entity = get_genre_entity(tostr(g), genres) if genre_entity != None: facts.append(Fact(m, belongsTo, genre_entity)) #movie_rating = get_rating_for_movie(int(tostr(movie_id))) #facts.append(Fact(m, has, get_rating_entity(tostr(movie_rating)))) return facts
def get_similar_movies(object): movies, genres, ratings = create_entities(df) create_network(movies, genres, ratings) liked_movies = [ x[0].tolist() for x in object.usr.getUsrHist() if x.tolist()[1] == 1 ] #print liked_movies for lm in liked_movies: lm_entity = get_movie_entity(str(lm), movies) if lm_entity != None: liked_genres = lm_entity.getObjects(belongsTo) #print tostr(liked_genres) for lg in liked_genres: lg_entity = get_genre_entity(tostr(lg), genres) similar_movies = [ tostr(sm) for sm in lg_entity.getObjects(whatBelongsTo) ] return similar_movies
# get the global "is-a" relationship isa = GetIsA() # inverse of "is-a" is "exampleOf" example = GetExampleOf() # declare some entities we want to store knowledge about thing = Entity("thing") animal = Entity("animal") bird = Entity("bird") fish = Entity("fish") minnow = Entity("minnow") trout = Entity("trout") ape = Entity("ape") # declare some facts: what's what? Fact(animal, isa, thing) Fact(ape, isa, animal) Fact(bird, isa, animal) Fact(fish, isa, animal) Fact(trout, isa, fish) Fact(minnow, isa, fish) # rint out some of the things we know (directly or by induction) print "trout is:", tostr(trout.objects(isa)) print "animal is:", tostr(animal.objects(isa)) print print "fish:", tostr(fish.objects(example)) print "fish:", tostr(fish.agents(isa)) print "animals:", tostr(animal.agents(isa))
def tobase64(x): """ python2/3 compatible conversion to urlsafe base64 encoding """ return tostr(base64.urlsafe_b64encode(tobytes(x))).replace('=', '')
bird = Entity("bird") fish = Entity("fish") minnow = Entity("minnow") trout = Entity("trout") ape = Entity("ape") # declare some facts: what's what? Fact(animal, isa, thing) Fact(ape, isa, animal) Fact(bird, isa, animal) Fact(fish, isa, animal) Fact(trout, isa, fish) Fact(minnow, isa, fish) # print out some of the things we know (directly or by induction) print "trout is:", tostr( trout.objects(isa) ) print "animal is:", tostr( animal.objects(isa) ) print print "fish:", tostr( fish.objects(example) ) print "fish:", tostr( fish.agents(isa) ) print "animals:", tostr( animal.agents(isa) ) print # declare size relationships biggerThan = Relation("bigger than", 1) smallerThan = Relation("smaller than", 1, biggerThan) # declare a couple facts Fact( minnow, smallerThan, trout ) Fact( trout, smallerThan, ape )
from semnet import * from tostr import tostr import string # get the global "is-a" relationship isa = GetIsA() # inverse of "is-a" is "exampleOf" example = GetExampleOf() # declare some entities we want to store knowledge about thing = Entity("thing") animal = Entity("animal") bird = Entity("bird") fish = Entity("fish") minnow = Entity("minnow") trout = Entity("trout") ape = Entity("ape") # declare some facts: what's what? Fact(animal, isa, thing) Fact(ape, isa, animal) Fact(bird, isa, animal) Fact(fish, isa, animal) Fact(trout, isa, fish) Fact(minnow, isa, fish) # print out some of the things we know (directly or by induction) print "trout is:", tostr( trout.objects(isa) ) print "animal is:", tostr( animal.objects(isa) ) print print "fish:", tostr( fish.objects(example) ) print "fish:", tostr( fish.agents(isa) ) print "animals:", tostr( animal.agents(isa) ) print
from semnet import *
def get_movie_entity(movie_id, movie_entities): for m in movie_entities: if tostr(m) == movie_id: return m
def get_genre_entity(genre, genre_entities): for g in genre_entities: if tostr(g) == genre: return g