Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # @darkyen - Abhishek Hingnikar (10/ 18/ 2014)
- import nltk
- import experimental.utils
- males = ["father", "uncle", "son", "brother"]
- females = ["mother", "aunty", "daugther", "sister"]
- levels = ["grand", "great", "great ", "great"]
- siblings = ["brother", "sister"]
- children = ["son", "daughter"]
- parents = ["mother", "father"]
- grandparents = ["grandpa", "grandma", "grandfather", "grandmother"]
- cousin = "cousin"
- # This comes from My utils
- # need to finish that
- # basically what it does is
- # look for the "lookup parameter"
- # and then convert into these direct relations as possible
- direct_relations = {
- "brother": "- male",
- "sister": "- female",
- "daughter" : "> female",
- "son": "> male",
- "father": "< male",
- "mother": "< female",
- }
- indirect_relations = {
- "cousin": "-- male | female",
- "uncle" : "-< male",
- "aunty" : "-< female",
- "nephew": "-> male",
- "neice": "-> female"
- }
- class Person():
- def __init__(self, gender):
- self.gender = gender
- self.parents = {
- "mother": None,
- "father": None
- }; // Only two parents
- self.kids = {
- "sons": [],
- "daughters": []
- };
- self.siblings = []
- self.husband = None
- self.wife = None
- # deprecated
- # Finds the spouse, helpful in finding stuff
- def findSpouse(self):
- the_chosen_one = None
- if len(self.kids["daughters"]) > 0:
- the_chosen_one = self.kids["daughters"][0]
- if len(self.kids["sons"]) > 0:
- the_chosen_one = self.kids["sons"][0]
- if the_chosen_one is None:
- return None
- spouse = None
- if( self.gender is "male"):
- spouse = the_chosen_one.parents["mother"]
- if( self.gender is "female"):
- spouse = the_chosen_one.parents["father"]
- return spouse
- def addSpouse(self, spouse):
- if relation in ["husband", "wife"]:
- self[relation] = spouse
- # Now the kids must be brothers and sisters :P
- # and blah blah blah
- # Okay this part of the code i am ashamed about completely
- # unreadable
- for x in range(0, len(self.kids["daughter"])):
- for y in range(0, len(spouse.kids["daugthers"])):
- spouse.kids["daughters"][y].addSibling( self.kids["daughter"][x], "sister")
- self.addChild(spouse.kids["daughters"][y], "daughter")
- for y in range(0, len(spouse.kids["sons"])):
- spouse.kids["sons"][y].addSibling( self.kids["daughter"][x], "sister")
- self.addChild(spouse.kids["sons"][y], "son")
- for x in range(0, len(self.kids["sons"])):
- for y in range(0, len(spouse.kids["daugthers"])):
- spouse.kids["daughters"][y].addSibling( self.kids["sons"][x], "brother")
- for y in range(0, len(spouse.kids["sons"])):
- spouse.kids["sons"][y].addSibling( self.kids["sons"][x], "brother")
- # Adds child
- def addChild(self, child, relation):
- if relation in ["son", "daughter"]:
- # Add brothers
- for i in range(0, len( self.kids["sons"] )):
- self.kids["sons"][i].addSibling(child)
- # Add sisters
- for i in range(0, len( self.kids["sons"] )):
- self.kids["sons"][i].addSibling(child)
- self.kids[relation + "s"].push(child)
- rel = "father"
- if self.gender is "female":
- rel = "mother"
- child.addParent(self, rel)
- def addGrandChild(self, child, relation):
- # Yes we know its grand child ... remove it
- relation = relation.replace("grand", "")
- gender = "male",
- rel = "son"
- if "maternal" in relation:
- gender = "female"
- rel = "daughter"
- parent = Person(gender)
- self.addChild(parent, rel)
- parent.addChild(child, relation)
- # Adds parent
- def addParent(self, parent, relation):
- if relation in ["father", "mother"]:
- self.parents[relation] = parent
- rel = "son"
- if self.gender is "female":
- rel = "daughter"
- parent.addChild(self, rel)
- # Adds sibling
- def addSibling(self, sibling):
- sibling.parent["mother"] = self.parent["mother"] = self.parent["mother"] or sibling.parent["mother"]
- sibling.parent["father"] = self.parent["father"] = self.parent["father"] or sibling.parent["father"]
- self.siblings.push( sibling )
- sibling.siblings.push( self )
- # Add Cousins. .. to achieve that add Parent and then add sibling to a parent
- # Two virtual fathers are added here, this is ambigous
- # Simple case not handling paternal and maternal cases
- # Input : Maternal cousin brother
- # Input : Cousin Sister
- def addCousin(self, cousin, relation):
- # Add an uncle
- uncle = Person("male")
- # Find out if the uncle is maternal or paternal
- # assume paternal if not present
- if "maternal" in relation:
- # Add a sibling to mom
- # or Add a mom and then add this cousin
- if self.parents["mother"] is None:
- self.parents["mother"] = Person("female")
- self.parents["mother"].addSibling( uncle )
- else:
- if self.parents["father"] is None:
- self.parents["father"] = Person("male")
- self.parents["father"].addSibling( uncle )
- if "brother" in relation:
- uncle.addChild( cousin, "son" )
- elif "sister" in relation:
- uncle.addChild( cousin, "daughter" )
- else:
- throw Error("Unknown relation")
- def feedData(sentence):
- sentence = sentence.lower()
- tokens = nltk.word_tokenize(sentence)
- relation = ""
- currentNode = None
- meNode = None
- tempNode = None
- for x in range(0, len(tokens)):
- relation = tokens[i]
- if relation is "my":
- meNode = tempNode = Person("male")
- if relation in siblings:
- gen = "male"
- if relation is "sister":
- gen = "female"
- tempNode = Person(gen)
- currentNode.addSibling(tempNode, relation)
- if relation in children:
- gen = "male"
- if relation is "daughter":
- gen = "female"
- tempNode = Person(gen)
- currentNode.addSibling(tempNode, relation)
- if relation in parents:
- gen = "male"
- if relation is "mother":
- gen = "female"
- tempNode = Person(gen)
- currentNode.addChild(tempNode, relation)
- if relation in grandparents:
- gen = "male"
- if relation is "grandma" or relation is "grandmother":
- gen = "female"
- tempNode = Person(gen)
- currentNode.addChild(tempNode, relation)
- if "cousin" in relation:
- gen = "male"
- if "sister" in relation:
- gen = "female"
- tempNode = Person(gen)
- currentNode.addCousin(tempNode, relation)
- currentNode = tempNode
- return (meNode, currentNode)
- #Finds relation in tuple(A, B)
- def digest(tree_tuple):
- # Find the shortest path applicable and return it as an array
- # Slightly modified version of the Graph traversal alogorithm
- # This guy goes and checks only for applicable keys
- relation_path = utils.NFA( tree_tuple[0], tree_tuple[1], looks_amongst = ["siblings", "kids.sons", "kids.daughters", "parent.mother", "parent.father"] )
- # when the list is returned visit each node
- # check what condition / relation the other node has
- # until you form a chain
- # first find indirect relations
- # then the left direct relations
- # name them and then print the string
- relation = utils.NamedRelation(
- direct_relations=direct_relations,
- indirect_relations= indirect_relations,
- lookup_key= "gender",
- relation_path)
- return relation
- relation = "My brother's wife's son's sister"
- tree = feedData(relation)
- #output is My neice
- print digest(tree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement