Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.44 KB | None | 0 0
  1. import json
  2. from os.path import join, dirname
  3.  
  4. __authors__ = ["jarbas", "heinzschmidt"]
  5.  
  6. class ConceptNode():
  7.     def __init__(self, name, data={}, parent_concepts={},
  8.         child_concepts={}, synonims=[], antonims=[]):
  9.         self.name = name
  10.         self.synonims = synonims
  11.         self.antonims = antonims
  12.         self.parent_concepts = parent_concepts
  13.         self.child_concepts = child_concepts
  14.         self.data = {}
  15.  
  16.     def add_synonim(self, synonim):
  17.         if synonim not in self.synonims:
  18.             self.synonims.append(synonim)
  19.  
  20.     def add_data(self, key, data={}):
  21.         if key in self.data:
  22.             self.data[key] = data
  23.         else:
  24.             self.data.setdefault(key, data)
  25.  
  26.     def add_parent(self, parent_name, gen = 1, update = True):
  27.  
  28.         # a node cannot be a parent  of itself
  29.         if parent_name == self.name:
  30.             return
  31.         # a node cannot be a parent and a child (would it make sense in some corner case?)
  32.         if parent_name in self.child_concepts:
  33.             return
  34.  
  35.         if parent_name not in self.parent_concepts:
  36.             self.parent_concepts.setdefault(parent_name, gen)
  37.         elif parent_name in self.parent_concepts and update:
  38.             self.parent_concepts[parent_name] = gen
  39.  
  40.  
  41.  
  42.     def add_child(self, child_name, gen=1, update = True):
  43.         # a node cannot be a child of itself
  44.         if child_name == self.name:
  45.             return
  46.  
  47.         if child_name in self.parent_concepts:
  48.             return
  49.  
  50.         if child_name not in self.child_concepts:
  51.             self.child_concepts.setdefault(child_name, gen)
  52.         elif child_name in self.child_concepts and update:
  53.             self.child_concepts[child_name]=gen
  54.  
  55.     def remove_synonim(self, synonim):
  56.         i = 0
  57.         for name in self.synonims:
  58.             if name == synonim:
  59.                 self.child_concepts.pop(i)
  60.                 return
  61.             i += 1
  62.  
  63.     def remove_data(self, key):
  64.         self.data.pop(key)
  65.  
  66.     def remove_parent(self, parent_name):
  67.         self.parent_concepts.pop(parent_name)
  68.  
  69.     def remove_child(self, child_name):
  70.         self.child_concepts.pop(child_name)
  71.  
  72.  
  73. class ConceptCreator():
  74.     def __init__(self, logger,  concepts = {}):
  75.         self.concepts = concepts
  76.         self.logger = logger
  77.  
  78.     def add_concept(self, concept_name, concept):
  79.         if concept_name in self.concepts:
  80.             #  merge fields
  81.             for parent in concept.parent_concepts:
  82.                 if parent not in self.get_parents(concept_name):
  83.                     self.concepts[concept_name].add_parent(parent, gen= concept.parent_concepts[parent])
  84.             for child in concept.child_concepts:
  85.                 if child not in self.get_childs(concept_name):
  86.                     self.concepts[concept_name].add_child(child, gen= concept.child_concepts[child])
  87.             for antonim in concept.antonims:
  88.                 if antonim not in self.concepts[concept_name].antonims:
  89.                     self.concepts[concept_name].antonims.add_antonim(antonim)
  90.             for synonim in concept.synonims:
  91.                 if synonim not in self.concepts[concept_name].synonims:
  92.                     self.concepts[concept_name].synonims.add_synonim(synonim)
  93.         else:
  94.             self.concepts.setdefault(concept_name, concept)
  95.  
  96.     def remove_concept(self, concept_name):
  97.         self.concepts.pop(concept_name)
  98.  
  99.     def get_childs(self, concept_name):
  100.         return self.concepts[concept_name].child_concepts
  101.  
  102.     def get_parents(self, concept_name):
  103.         return self.concepts[concept_name].parent_concepts
  104.  
  105.     def create_concept(self, new_concept_name, data={},
  106.                        child_concepts={}, parent_concepts={}, synonims=[], antonims=[], gen = 1):
  107.  
  108.         self.logger.info(" ")
  109.         self.logger.info("processing concept " + new_concept_name)
  110.  
  111.         # handle new concept
  112.         if new_concept_name not in self.concepts:
  113.             self.logger.info("creating concept node for: " + new_concept_name)
  114.             concept = ConceptNode(new_concept_name, data, parent_concepts, child_concepts, synonims, antonims)
  115.             self.add_concept(new_concept_name, concept)
  116.  
  117.         # handle parent concepts
  118.         for concept_name in dict(parent_concepts):
  119.             self.logger.info("checking if parent node exists: " + concept_name)
  120.  
  121.             # create parent if it doesnt exist
  122.             if concept_name not in self.concepts:
  123.                 self.logger.info("creating node: " + concept_name +" with child: " + new_concept_name)
  124.                 concept = ConceptNode(concept_name, child_concepts={new_concept_name: gen})
  125.                 self.add_concept(concept_name, concept)
  126.  
  127.         # handle child concepts
  128.         for concept_name in child_concepts:
  129.             self.logger.info("checking if child node exists: " + concept_name)
  130.             # create child if it doesnt exist
  131.             if concept_name not in self.concepts:
  132.                 self.logger.info("creating node: " + concept_name + " with parent: " + new_concept_name)
  133.                 concept = ConceptNode(concept_name, parent_concepts={new_concept_name: gen})
  134.                 self.add_concept(concept_name, concept)
  135.  
  136.         self.next_gen_parents(new_concept_name, parent_concepts)
  137.         self.next_gen_childs(new_concept_name, child_concepts)
  138.  
  139.     def next_gen_parents(self, concept_name, concept_parents=None, current_gen=0, gen_depth=2, root_concept=None):
  140.  
  141.         if root_concept is None:
  142.             root_concept = concept_name
  143.  
  144.         if concept_parents is None:
  145.             concept_parents = self.get_parents(concept_name)
  146.  
  147.         # grandparents = get parents of parents
  148.         self.logger.info("Getting parents of: " + concept_name)
  149.         grandparents = {}
  150.         parents = concept_parents
  151.         for parent in parents:
  152.             grandparents.setdefault(parent, parents[parent])
  153.             self.logger.info("Detected parent: " + parent + " from generation: " + str(parents[parent]))
  154.  
  155.         # increase all gens +1
  156.         for grand_parent in grandparents:
  157.             parent_gen = grandparents[grand_parent] + current_gen
  158.             # add each grandparent to concept_name
  159.             self.logger.info("Adding parent: " + grand_parent + " from generation: " + str(parent_gen))
  160.             self.concepts[root_concept].add_parent(grand_parent, parent_gen)
  161.  
  162.         # prepare next gen
  163.         self.logger.info("Preparing for next_generation")
  164.         current_gen += 1
  165.         # get great_gandparents
  166.         for parent in grandparents:
  167.             self.logger.info("check: " + parent + " for grandparents")
  168.             try:
  169.                 parents = self.get_parents(parent)
  170.                 for parent in parents:
  171.                     if parent not in self.get_parents(root_concept):
  172.                         self.logger.info("grand_parent of " + root_concept + " detected: " + parent)
  173.                         gen = parents[parent] + current_gen
  174.                         self.logger.info("Processing grandparent: " + parent)
  175.                         # go to childs of grandparent and add gandparent as parent
  176.                         self.logger.info("Adding grandparent to grandchild: " + root_concept + " gen: " + str(gen))
  177.                         self.concepts[root_concept].add_parent(parent, gen)
  178.                         self.next_gen_parents(parent, self.get_parents(parent), gen, root_concept=root_concept)
  179.             except:
  180.                 pass
  181.  
  182.     def next_gen_childs(self, concept_name, concept_childs = None, current_gen=0, gen_depth=2, root_concept=None):
  183.  
  184.         if root_concept is None:
  185.             root_concept = concept_name
  186.  
  187.         if concept_childs is None:
  188.             concept_childs = self.get_childs(concept_name)
  189.  
  190.         # grandchild = get childs of childs
  191.         self.logger.info("Getting childs of: " + concept_name)
  192.         grandchilds = {}
  193.         childs = concept_childs
  194.         for child in childs:
  195.             grandchilds.setdefault(child, childs[child])
  196.             self.logger.info("Detected child: " + child + " from generation: " + str(childs[child]))
  197.         # increase all gens +1
  198.         for grand_child in grandchilds:
  199.             gen = grandchilds[grand_child] + current_gen
  200.             # add each grandchild to concept_name
  201.             self.logger.info("Adding child: " + grand_child + " from generation: " + str(gen))
  202.             self.concepts[root_concept].add_child(grand_child, gen, False)
  203.  
  204.         # prepare next gen
  205.         self.logger.info("Preparing for next_generation")
  206.         current_gen += 1
  207.         # get great grand_children
  208.         for child in grandchilds:
  209.             self.logger.info("check: " + child + " for grandchilds")
  210.             try:
  211.                 childs = self.get_childs(child)
  212.                 for child in childs:
  213.                     if child not in self.get_childs(root_concept):
  214.                         self.logger.info("grand_child of " + root_concept + " detected: " + child)
  215.                         gen = childs[child] + current_gen
  216.                         self.logger.info("Processing grandchild: " + child)
  217.                         self.logger.info("Adding grandchild to grandparent: " + root_concept + " gen: " + str(gen))
  218.                         self.concepts[root_concept].add_parent(child, gen)
  219.                         self.next_gen_childs(child, self.get_parents(child), gen, root_concept=root_concept)
  220.             except:
  221.                 pass
  222.  
  223.  
  224. class ConceptStorage():
  225.  
  226.     _dataStorageType = ""
  227.     _dataStorageUser = ""
  228.     _dataStoragePass = ""
  229.     _dataStorageDB = ""
  230.     _dataConnection = None
  231.     _dataConnStatus = 0
  232.     _dataJSON = None
  233.     _storagepath = ""
  234.  
  235.     def __init__(self, storagepath, storagetype="json", database="lilacstorage.db"):
  236.         self._storagepath = storagepath
  237.         self._dataStorageType = storagetype
  238.         self._dataStorageDB = database
  239.         self.datastore_connect()
  240.  
  241.     def datastore_connect(self):
  242.         if(self._dataStorageType == "sqllite3"):
  243.             """try:
  244.                self._dataConnection = sqllite3.connect(self._dataStorageDB)
  245.                self._dataConnStatus = 1
  246.            except Exception as sqlerr:
  247.                # log something
  248.                print(("Database connection failed" + str(sqlerr)))
  249.                self._dataConnStatus = 0
  250.            """
  251.         elif(self._dataStorageType == "json"):
  252.             with open(self._storagepath + self._dataStorageDB)\
  253.              as datastore:
  254.                 self._dataJSON = json.load(datastore)
  255.  
  256.             if(self._dataJSON):
  257.                 self._dataConnStatus = 1
  258.             else:
  259.                 self._dataConnStatus = 0
  260.  
  261.     def getNodeDataDictionary(self, conceptname="cow"):
  262.         returnVal = {}
  263.         if(self._dataConnStatus == 1):
  264.             for p in self._dataJSON[conceptname]:
  265.                 returnVal["data_dict"] = str(p["data_dict"])
  266.         return returnVal
  267.  
  268.     def getNodeParent(self, conceptname="cow", generation=None):
  269.         returnVal = {}
  270.         if(self._dataConnStatus == 1):
  271.             for node in self._dataJSON[conceptname]:
  272.                 if(generation is None):
  273.                     for parent in node["parents"]:
  274.                         returnVal = parent
  275.                 elif(generation <= len(node["parents"])):
  276.                     for parent in node["parents"]:
  277.                         if parent[str(generation)]:
  278.                             returnVal = parent[str(generation)]
  279.         return returnVal
  280.  
  281.     def getNodeChildren(self, conceptname="cow", generation=None):
  282.         returnVal = {}
  283.         if(self._dataConnStatus == 1):
  284.             for node in self._dataJSON[conceptname]:
  285.                 if(generation is None):
  286.                     for child in node["children"]:
  287.                         returnVal = child
  288.                 elif(generation <= len(node["children"])):
  289.                     for child in node["children"]:
  290.                         if child[str(generation)]:
  291.                             returnVal = child[str(generation)]
  292.         return returnVal
  293.  
  294.     def getNodeSynonymn(self, conceptname="cow", generation=None):
  295.         returnVal = {}
  296.         if(self._dataConnStatus == 1):
  297.             for node in self._dataJSON[conceptname]:
  298.                 if(generation is None):
  299.                     for synonymn in node["synonymns"]:
  300.                         returnVal = synonymn
  301.                 elif(generation <= len(node["synonymns"])):
  302.                     for synonymn in node["synonymns"]:
  303.                         if synonymn[str(generation)]:
  304.                             returnVal = synonymn[str(generation)]
  305.             return returnVal
  306.  
  307.     def getNodeAntonymn(self, conceptname="cow", generation=None):
  308.         returnVal = {}
  309.         if(self._dataConnStatus == 1):
  310.             for node in self._dataJSON[conceptname]:
  311.                 if(generation is None):
  312.                     for synonymn in node["antonymns"]:
  313.                         returnVal = synonymn
  314.                 elif(generation <= len(node["antonymns"])):
  315.                     for synonymn in node["antonymns"]:
  316.                         if synonymn[str(generation)]:
  317.                             returnVal = synonymn[str(generation)]
  318.             return returnVal
  319.  
  320.     def getNodeLastUpdate(self, conceptname="cow"):
  321.         returnVal = {}
  322.         if(self._dataConnStatus == 1):
  323.             for p in self._dataJSON[conceptname]:
  324.                 returnVal["last_update"] = str(p["last_update"])
  325.         return returnVal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement