Advertisement
Guest User

Python WIP

a guest
Oct 18th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # @darkyen - Abhishek Hingnikar  (10/ 18/ 2014)
  2. import nltk
  3. import experimental.utils
  4.  
  5. males = ["father", "uncle", "son", "brother"]
  6. females = ["mother", "aunty", "daugther", "sister"]
  7. levels = ["grand", "great", "great ", "great"]
  8. siblings = ["brother", "sister"]
  9. children = ["son", "daughter"]
  10. parents = ["mother", "father"]
  11. grandparents = ["grandpa", "grandma", "grandfather", "grandmother"]
  12. cousin = "cousin"
  13.  
  14.  
  15. # This comes from My utils
  16. # need to finish that
  17. # basically what it does is
  18. # look for the "lookup parameter"
  19. # and then convert into these direct relations as possible
  20. direct_relations = {
  21.     "brother": "- male",
  22.     "sister": "- female",
  23.     "daughter" : "> female",
  24.     "son": "> male",
  25.     "father": "< male",
  26.     "mother": "< female",
  27. }
  28.  
  29. indirect_relations = {
  30.     "cousin": "-- male | female",
  31.     "uncle" : "-< male",
  32.     "aunty" : "-< female",
  33.     "nephew": "-> male",
  34.     "neice": "-> female"
  35. }
  36.  
  37. class Person():
  38.     def __init__(self, gender):
  39.         self.gender = gender
  40.         self.parents = {
  41.                                  "mother": None,
  42.                                  "father": None
  43.                 }; // Only two parents
  44.         self.kids = {
  45.             "sons": [],
  46.             "daughters": []
  47.         };
  48.         self.siblings = []
  49.         self.husband = None
  50.         self.wife = None
  51.    
  52.     # deprecated
  53.     # Finds the spouse, helpful in finding stuff
  54.     def findSpouse(self):
  55.         the_chosen_one = None
  56.  
  57.         if len(self.kids["daughters"]) > 0:
  58.             the_chosen_one = self.kids["daughters"][0]
  59.         if len(self.kids["sons"]) > 0:
  60.             the_chosen_one = self.kids["sons"][0]
  61.         if the_chosen_one is None:
  62.             return None
  63.  
  64.         spouse = None
  65.  
  66.         if( self.gender is "male"):
  67.             spouse = the_chosen_one.parents["mother"]
  68.         if( self.gender is "female"):
  69.             spouse = the_chosen_one.parents["father"]
  70.  
  71.         return spouse          
  72.    
  73.     def addSpouse(self, spouse):
  74.         if relation in ["husband", "wife"]:
  75.             self[relation] = spouse
  76.             # Now the kids must be brothers and sisters :P
  77.             # and blah blah blah
  78.             # Okay this part of the code i am ashamed about completely
  79.             # unreadable
  80.             for x in range(0, len(self.kids["daughter"])):             
  81.                 for y in range(0, len(spouse.kids["daugthers"])):
  82.                     spouse.kids["daughters"][y].addSibling( self.kids["daughter"][x], "sister")
  83.                     self.addChild(spouse.kids["daughters"][y], "daughter")
  84.  
  85.                 for y in range(0, len(spouse.kids["sons"])):
  86.                     spouse.kids["sons"][y].addSibling( self.kids["daughter"][x], "sister")
  87.                     self.addChild(spouse.kids["sons"][y], "son")
  88.  
  89.             for x in range(0, len(self.kids["sons"])):             
  90.                 for y in range(0, len(spouse.kids["daugthers"])):
  91.                     spouse.kids["daughters"][y].addSibling( self.kids["sons"][x], "brother")
  92.                 for y in range(0, len(spouse.kids["sons"])):
  93.                     spouse.kids["sons"][y].addSibling( self.kids["sons"][x], "brother")
  94.  
  95.     # Adds child   
  96.     def addChild(self, child, relation):
  97.         if relation in ["son", "daughter"]:
  98.            
  99.             # Add brothers
  100.             for i in range(0, len( self.kids["sons"] )):
  101.                 self.kids["sons"][i].addSibling(child)
  102.            
  103.             # Add sisters
  104.             for i in range(0, len( self.kids["sons"] )):
  105.                 self.kids["sons"][i].addSibling(child)
  106.  
  107.  
  108.             self.kids[relation + "s"].push(child)
  109.            
  110.             rel = "father"
  111.             if self.gender is "female":
  112.                 rel = "mother"
  113.  
  114.             child.addParent(self, rel)
  115.  
  116.  
  117.  
  118.  
  119.     def addGrandChild(self, child, relation):
  120.         # Yes we know its grand child ... remove it
  121.         relation = relation.replace("grand", "")
  122.        
  123.         gender = "male",
  124.         rel = "son"
  125.        
  126.         if "maternal" in relation:
  127.             gender = "female"
  128.             rel = "daughter"
  129.  
  130.         parent =  Person(gender)
  131.         self.addChild(parent, rel)
  132.         parent.addChild(child, relation)
  133.  
  134.     # Adds parent
  135.     def addParent(self, parent, relation):
  136.         if relation in ["father", "mother"]:
  137.             self.parents[relation] = parent
  138.             rel = "son"
  139.  
  140.             if self.gender is "female":
  141.                 rel = "daughter"
  142.             parent.addChild(self, rel)
  143.  
  144.    
  145.     # Adds sibling
  146.     def addSibling(self, sibling):
  147.         sibling.parent["mother"] = self.parent["mother"] = self.parent["mother"] or sibling.parent["mother"]
  148.         sibling.parent["father"] = self.parent["father"] = self.parent["father"] or sibling.parent["father"]
  149.         self.siblings.push( sibling )
  150.         sibling.siblings.push( self )
  151.    
  152.     # Add Cousins. .. to achieve that add Parent and then add sibling to a parent
  153.     # Two virtual fathers are added here, this is ambigous
  154.     # Simple case not handling paternal and maternal cases
  155.     # Input : Maternal cousin brother
  156.     # Input : Cousin Sister
  157.     def addCousin(self, cousin, relation):
  158.         # Add an uncle
  159.         uncle =  Person("male")
  160.  
  161.         # Find out if the uncle is maternal or paternal
  162.         # assume paternal if not present
  163.         if "maternal" in relation:
  164.             # Add a sibling to mom
  165.             # or Add a mom and then add this cousin
  166.             if self.parents["mother"] is None:
  167.                 self.parents["mother"] =  Person("female")
  168.             self.parents["mother"].addSibling( uncle )
  169.         else:
  170.             if self.parents["father"] is None:
  171.                 self.parents["father"] =  Person("male")
  172.             self.parents["father"].addSibling( uncle )
  173.  
  174.         if "brother" in relation:
  175.             uncle.addChild( cousin, "son" )
  176.  
  177.         elif "sister" in relation:
  178.             uncle.addChild( cousin, "daughter" )
  179.        
  180.         else:
  181.             throw Error("Unknown relation")
  182.    
  183.    
  184.  
  185.  
  186. def feedData(sentence):
  187.     sentence = sentence.lower()
  188.     tokens = nltk.word_tokenize(sentence)
  189.     relation = ""
  190.     currentNode = None
  191.     meNode = None
  192.     tempNode = None
  193.  
  194.     for x in range(0, len(tokens)):
  195.         relation = tokens[i]
  196.        
  197.         if relation is "my":
  198.             meNode = tempNode =  Person("male")
  199.  
  200.         if relation in siblings:
  201.             gen = "male"
  202.             if relation is "sister":
  203.                 gen = "female"
  204.             tempNode =  Person(gen)
  205.             currentNode.addSibling(tempNode, relation)
  206.  
  207.         if relation in children:
  208.             gen = "male"
  209.             if relation is "daughter":
  210.                 gen = "female"
  211.             tempNode =  Person(gen)
  212.             currentNode.addSibling(tempNode, relation)
  213.        
  214.         if relation in parents:
  215.             gen = "male"
  216.             if relation is "mother":
  217.                 gen = "female"
  218.             tempNode =  Person(gen)
  219.             currentNode.addChild(tempNode, relation)
  220.         if relation in grandparents:
  221.             gen = "male"
  222.             if relation is "grandma" or relation is "grandmother":
  223.                 gen = "female"
  224.             tempNode =  Person(gen)
  225.             currentNode.addChild(tempNode, relation)
  226.  
  227.         if "cousin" in relation:
  228.             gen = "male"
  229.             if "sister" in relation:
  230.                 gen = "female"
  231.             tempNode =  Person(gen)
  232.             currentNode.addCousin(tempNode, relation)
  233.  
  234.         currentNode = tempNode
  235.  
  236.     return (meNode, currentNode)
  237.  
  238. #Finds relation in tuple(A, B)
  239. def digest(tree_tuple):
  240.     # Find the shortest path applicable and return it as an array
  241.     # Slightly modified version of the Graph traversal alogorithm
  242.     # This guy goes and checks only for applicable keys
  243.     relation_path = utils.NFA( tree_tuple[0], tree_tuple[1], looks_amongst = ["siblings", "kids.sons", "kids.daughters", "parent.mother", "parent.father"] )
  244.    
  245.     # when the list is returned visit each node
  246.     # check what condition / relation the other node has
  247.     # until you form a chain
  248.     # first find indirect relations
  249.     # then the left direct relations
  250.     # name them and then print the string
  251.     relation = utils.NamedRelation(
  252.         direct_relations=direct_relations,
  253.         indirect_relations= indirect_relations,
  254.         lookup_key= "gender",
  255.         relation_path)
  256.     return relation
  257.  
  258. relation = "My brother's wife's son's sister"
  259. tree = feedData(relation)
  260.  
  261. #output is My neice
  262. print digest(tree)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement