def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'TS02', 'TS05', 'TS08')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))

                total_publications = len(informations)
                k = int(param.t0_) - max(timesofLinks)
                decayfunction02 = (1 - 0.2)**k
                decayfunction05 = (1 - 0.5)**k
                decayfunction08 = (1 - 0.8)**k

                pdb.insert(
                    str(node) + ';' + str(other), node, other,
                    (total_publications * decayfunction02),
                    (total_publications * decayfunction05),
                    (total_publications * decayfunction08))

    pdb.commit()
    return pdb
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2','TS02','TS05','TS08', 'JC')
    pdb.create_index('pair')
    
    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(edge for n1, n2, edge in graph.edges([node, other], data=True) if ((n1 == node and n2 == other) or (n1 == other and n2 == node)) )
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))
        
                
                bagNode1 = list(eval(edge['keywords']) for n1, n2, edge in graph.edges([node], data=True) if (n1 != other and n2 != other)  )
                bagNode2 = list(eval(edge['keywords']) for n1, n2, edge in graph.edges([other], data=True) if (n1 != node and n2 != node)  )
                
                
                total_publications = len(informations)   
                k =  int(param.t0_)  - max(timesofLinks)
                decayfunction02 = (1 - 0.2) ** k
                decayfunction05 = (1 - 0.5) ** k
                decayfunction08 = (1 - 0.8) ** k
                
                pdb.insert(str(node) + ';' + str(other),node,other,(total_publications * decayfunction02) , (total_publications * decayfunction05) , (total_publications * decayfunction08), get_jacard_domain(bagNode1, bagNode2) ) 
                 
    pdb.commit()            
    return pdb
Exemplo n.º 3
0
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'WCNFI','WAAFI')
    pdb.create_index('node1', 'node2')
                
    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element+1
        FormatingDataSets.printProgressofEvents(element, qtyofNodesToProcess, "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        WCNFI = 0;
        WAAFI = 0;
        
        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            WCNFI = WCNFI + item['WCN'];
            WAAFI = WAAFI + item['WAA'];
        pdb.insert(str(pair[0]), str(pair[1]), WCNFI, WAAFI )   
    pdb.commit()
    return pdb;
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2','FTI01','FTI02','FTI03','FTI04','FTI05','FTI06','FTI07','FTI08','FTI09')
    pdb.create_index('pair')
    
    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(edge for n1, n2, edge in graph.edges([node, other], data=True) if ((n1 == node and n2 == other) or (n1 == other and n2 == node)) )
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))
                
                total_publications = len(informations)   
                k =  int(param.t0_)  - max(timesofLinks)
                FTI01 = total_publications * (0.1**k)
                FTI02 = total_publications * (0.2**k)
                FTI03 = total_publications * (0.3**k)
                FTI04 = total_publications * (0.4**k)
                FTI05 = total_publications * (0.5**k)
                FTI06 = total_publications * (0.6**k)
                FTI07 = total_publications * (0.7**k)
                FTI08 = total_publications * (0.8**k)
                FTI09 = total_publications * (0.9**k)
                 
                
 
                pdb.insert(str(node) + ';' + str(other),node,other, FTI01, FTI02, FTI03, FTI04, FTI05, FTI06, FTI07, FTI08, FTI09 ) 
                 
    pdb.commit()            
    return pdb
Exemplo n.º 5
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'FI')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))

                total_publications = len(informations)
                bagNode1 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([node], data=True)
                    if (n1 != other and n2 != other))
                bagNode2 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([other], data=True)
                    if (n1 != node and n2 != node))
                jc = get_jacard_domain(bagNode1, bagNode2)

                pdb.insert(
                    str(node) + ';' + str(other), node, other,
                    (total_publications * (jc + 1)))

    pdb.commit()
    return pdb
Exemplo n.º 6
0
def open_db():
  db = Base(DB_FILE)
  db.create('abbr', 'name', 'available', 'volume', 'buy', 'sell', 'date',
            mode="open")
  if not db.exists():
    raise Exception('Database error')
  db.create_index('abbr')
  return db
Exemplo n.º 7
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'FTI01', 'FTI02', 'FTI03', 'FTI04',
               'FTI05', 'FTI06', 'FTI07', 'FTI08', 'FTI09')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))

                total_publications = len(informations)
                k = int(param.t0_) - max(timesofLinks)

                decayfunction = (0.8)**k
                bagNode1 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([node], data=True)
                    if (n1 != other and n2 != other))
                bagNode2 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([other], data=True)
                    if (n1 != node and n2 != node))

                jc = get_jacard_domain(bagNode1, bagNode2)

                FTI01 = total_publications * (decayfunction * (1 / (0.1**jc)))
                FTI02 = total_publications * (decayfunction * (1 / (0.2**jc)))
                FTI03 = total_publications * (decayfunction * (1 / (0.3**jc)))
                FTI04 = total_publications * (decayfunction * (1 / (0.4**jc)))
                FTI05 = total_publications * (decayfunction * (1 / (0.5**jc)))
                FTI06 = total_publications * (decayfunction * (1 / (0.6**jc)))
                FTI07 = total_publications * (decayfunction * (1 / (0.7**jc)))
                FTI08 = total_publications * (decayfunction * (1 / (0.8**jc)))
                FTI09 = total_publications * (decayfunction * (1 / (0.9**jc)))

                pdb.insert(
                    str(node) + ';' + str(other), node, other, FTI01, FTI02,
                    FTI03, FTI04, FTI05, FTI06, FTI07, FTI08, FTI09)

    pdb.commit()
    return pdb
Exemplo n.º 8
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2', 'CTS02', 'CTS05', 'CTS08')
    pdb.create_index('pair')

    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(
                    edge
                    for n1, n2, edge in graph.edges([node, other], data=True)
                    if ((n1 == node and n2 == other) or (
                        n1 == other and n2 == node)))
                timesofLinks = []
                for info in informations:
                    timesofLinks.append(int(info['time']))

                bagNode1 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([node], data=True)
                    if (n1 != other and n2 != other))
                bagNode2 = list(
                    eval(edge['keywords'])
                    for n1, n2, edge in graph.edges([other], data=True)
                    if (n1 != node and n2 != node))

                total_publications = len(informations)
                k = int(param.t0_) - max(timesofLinks)

                jc = get_jacard_domain(bagNode1, bagNode2)
                decayfunction = (0.6)**k
                decayfunction02 = (1 - 0.7)**jc
                decayfunction05 = (1 - 0.8)**jc
                decayfunction08 = (1 - 0.9)**jc

                CTS02 = total_publications * (decayfunction *
                                              (1 / decayfunction02))
                CTS05 = total_publications * (decayfunction *
                                              (1 / decayfunction05))
                CTS08 = total_publications * (decayfunction *
                                              (1 / decayfunction08))

                pdb.insert(
                    str(node) + ';' + str(other), node, other, CTS02, CTS05,
                    CTS08)

    pdb.commit()
    return pdb
def generate_finalResult(order,topRank, TestGraph, FileNameResult ):
    pdb = Base(FileNameResult)
    pdb.create('node1', 'node2', 'value', 'sucesso','topRank')
    pdb.create_index('node1', 'node2')
 
    indice = 0 
    for nodeToCheck in order:
        indice = indice+1
        isTopRank = (indice <= topRank)
        if (TestGraph.has_edge(nodeToCheck['node1'],nodeToCheck['node2'])):
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , True, isTopRank  ) 
        else:   
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , False, isTopRank  ) 
    
    pdb.commit()
    return pdb
Exemplo n.º 10
0
def generate_finalResult(order,topRank, TestGraph, FileNameResult ):
    pdb = Base(FileNameResult)
    pdb.create('node1', 'node2', 'value', 'sucesso','topRank')
    pdb.create_index('node1', 'node2')
 
    indice = 0 
    for nodeToCheck in order:
        indice = indice+1
        isTopRank = (indice <= topRank)
        if (TestGraph.has_edge(nodeToCheck['node1'],nodeToCheck['node2'])):
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , True, isTopRank  ) 
        else:   
            pdb.insert(str(nodeToCheck['node1']), nodeToCheck['node2'],nodeToCheck['value'] , False, isTopRank  ) 
    
    pdb.commit()
    return pdb
Exemplo n.º 11
0
def generateWeights(graph, weightFile, param):
    pdb = Base(weightFile)
    pdb.create('pair', 'node1', 'node2','FI')
    pdb.create_index('pair')
    
    sortedNodes = sorted(graph.nodes())
    for node in sortedNodes:
        others = sorted(set(n for n in sortedNodes if n > node))
        for other in others:
            if graph.has_edge(node, other):
                informations = list(edge for n1, n2, edge in graph.edges([node, other], data=True) if ((n1 == node and n2 == other) or (n1 == other and n2 == node)) )
                        
                total_publications = len(informations)   
                pdb.insert(str(node) + ';' + str(other),node,other, total_publications ) 
                 
    pdb.commit()            
    return pdb
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'cnWTS02', 'cnWTS05', 'cnWTS08', 'aaWTS02',
               'aaWTS05', 'aaWTS08')
    pdb.create_index('node1', 'node2')

    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element + 1
        FormatingDataSets.printProgressofEvents(
            element, qtyofNodesToProcess,
            "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        CNWts02Feature = 0
        CNWts05Feature = 0
        CNWts08Feature = 0
        AAWts02Feature = 0
        AAWts05Feature = 0
        AAWts08Feature = 0
        CNWJCFeature = 0
        AAWJCFeature = 0

        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            CNWts02Feature = CNWts02Feature + item['cnWts02']
            CNWts05Feature = CNWts05Feature + item['cnWts05']
            CNWts08Feature = CNWts08Feature + item['cnWts08']
            AAWts02Feature = AAWts02Feature + item['aaWts02']
            AAWts05Feature = AAWts05Feature + item['aaWts05']
            AAWts08Feature = AAWts08Feature + item['aaWts08']
            #CNWJCFeature = CNWJCFeature + item['cnWJC'];
            #AAWJCFeature = AAWJCFeature + item['aaWJC'];

        pdb.insert(str(pair[0]), str(pair[1]), CNWts02Feature, CNWts05Feature,
                   CNWts08Feature, AAWts02Feature, AAWts05Feature,
                   AAWts08Feature)
    pdb.commit()
    return pdb
Exemplo n.º 13
0
class Rates:
    """ Persistence layer for exchange rates. """
    def __init__(self, filename, erase_db):
        self.__db = Base(filename)
        self.__db.create('currency',
                         'rate',
                         mode="override" if erase_db else "open")
        self.__db.create_index('currency')

    def setRate(self, currency, rate):
        """ Persist a currency's exchange rate. """
        assert rate > 0.0

        records = self.__db._currency[currency]

        if len(records) > 0:
            assert len(
                records
            ) == 1  # We never expect several exchange rates for the same currency
            self.__db.update(records[0], rate=rate)
        else:
            self.__db.insert(currency=currency, rate=rate)

        self.__db.commit()

    def getRate(self, currency):
        """ Get the exchange rate with EUR for the provided currency or None if it is not found.
            An exchange rate for currency CUR is Value(EUR) / Value(CUR): 1 EUR = rate(CUR) CUR <=> 1/rate(CUR) EUR = 1 CUR.
        """
        records = self.__db._currency[currency]
        return records[0]['rate'] if len(records) > 0 else None

    def getAllRates(self):
        """ Get all known exchange rates as a dict. """
        return [(r['currency'], r['rate']) for r in self.__db]

    def getRatesCount(self):
        """ Get total number of exchange rates in db. """
        return len(self.__db)
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'cnWTS02','cnWTS05','cnWTS08', 'aaWTS02', 'aaWTS05', 'aaWTS08')
    pdb.create_index('node1', 'node2')
                
    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element+1
        FormatingDataSets.printProgressofEvents(element, qtyofNodesToProcess, "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        CNWts02Feature = 0;
        CNWts05Feature = 0;
        CNWts08Feature = 0;
        AAWts02Feature = 0;
        AAWts05Feature = 0;
        AAWts08Feature = 0;
        CNWJCFeature = 0;
        AAWJCFeature = 0;
        
        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            CNWts02Feature = CNWts02Feature + item['cnWts02'];
            CNWts05Feature = CNWts05Feature + item['cnWts05'];
            CNWts08Feature = CNWts08Feature + item['cnWts08'];
            AAWts02Feature = AAWts02Feature + item['aaWts02'];
            AAWts05Feature = AAWts05Feature + item['aaWts05'];
            AAWts08Feature = AAWts08Feature + item['aaWts08'];
            #CNWJCFeature = CNWJCFeature + item['cnWJC'];
            #AAWJCFeature = AAWJCFeature + item['aaWJC'];
        
            
        pdb.insert(str(pair[0]), str(pair[1]), CNWts02Feature, CNWts05Feature, CNWts08Feature, AAWts02Feature, AAWts05Feature, AAWts08Feature  )   
    pdb.commit()
    return pdb;
Exemplo n.º 15
0
class Rates:
    """ Persistence layer for exchange rates. """

    def __init__(self, filename, erase_db):
        self.__db = Base(filename)
        self.__db.create('currency', 'rate', mode="override" if erase_db else "open")
        self.__db.create_index('currency')

    def setRate(self, currency, rate):
        """ Persist a currency's exchange rate. """
        assert rate > 0.0

        records = self.__db._currency[currency]

        if len(records) > 0:
            assert len(records) == 1 # We never expect several exchange rates for the same currency
            self.__db.update(records[0], rate = rate)
        else:
            self.__db.insert(currency = currency, rate = rate)

        self.__db.commit()

    def getRate(self, currency):
        """ Get the exchange rate with EUR for the provided currency or None if it is not found.
            An exchange rate for currency CUR is Value(EUR) / Value(CUR): 1 EUR = rate(CUR) CUR <=> 1/rate(CUR) EUR = 1 CUR.
        """
        records = self.__db._currency[currency]
        return records[0]['rate'] if len(records) > 0 else None

    def getAllRates(self):
        """ Get all known exchange rates as a dict. """
        return [(r['currency'], r['rate']) for r in self.__db]

    def getRatesCount(self):
        """ Get total number of exchange rates in db. """
        return len(self.__db)
Exemplo n.º 16
0
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'WCNFTI01', 'WCNFTI02', 'WCNFTI03',
               'WCNFTI04', 'WCNFTI05', 'WCNFTI06', 'WCNFTI07', 'WCNFTI08',
               'WCNFTI09', 'WAAFTI01', 'WAAFTI02', 'WAAFTI03', 'WAAFTI04',
               'WAAFTI05', 'WAAFTI06', 'WAAFTI07', 'WAAFTI08', 'WAAFTI09')
    pdb.create_index('node1', 'node2')

    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element + 1
        FormatingDataSets.printProgressofEvents(
            element, qtyofNodesToProcess,
            "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        WCNFTI01 = 0
        WCNFTI02 = 0
        WCNFTI03 = 0
        WCNFTI04 = 0
        WCNFTI05 = 0
        WCNFTI06 = 0
        WCNFTI07 = 0
        WCNFTI08 = 0
        WCNFTI09 = 0

        WAAFTI01 = 0
        WAAFTI02 = 0
        WAAFTI03 = 0
        WAAFTI04 = 0
        WAAFTI05 = 0
        WAAFTI06 = 0
        WAAFTI07 = 0
        WAAFTI08 = 0
        WAAFTI09 = 0

        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            WCNFTI01 = WCNFTI01 + item['WCN'][0]
            WCNFTI02 = WCNFTI02 + item['WCN'][1]
            WCNFTI03 = WCNFTI03 + item['WCN'][2]
            WCNFTI04 = WCNFTI04 + item['WCN'][3]
            WCNFTI05 = WCNFTI05 + item['WCN'][4]
            WCNFTI06 = WCNFTI06 + item['WCN'][5]
            WCNFTI07 = WCNFTI07 + item['WCN'][6]
            WCNFTI08 = WCNFTI08 + item['WCN'][7]
            WCNFTI09 = WCNFTI09 + item['WCN'][8]

            WAAFTI01 = WAAFTI01 + item['WAA'][0]
            WAAFTI02 = WAAFTI02 + item['WAA'][1]
            WAAFTI03 = WAAFTI03 + item['WAA'][2]
            WAAFTI04 = WAAFTI04 + item['WAA'][3]
            WAAFTI05 = WAAFTI05 + item['WAA'][4]
            WAAFTI06 = WAAFTI06 + item['WAA'][5]
            WAAFTI07 = WAAFTI07 + item['WAA'][6]
            WAAFTI08 = WAAFTI08 + item['WAA'][7]
            WAAFTI09 = WAAFTI09 + item['WAA'][8]

        pdb.insert(
            str(pair[0]),
            str(pair[1]),
            WCNFTI01,
            WCNFTI02,
            WCNFTI02,
            WCNFTI03,
            WCNFTI04,
            WCNFTI05,
            WCNFTI06,
            WCNFTI07,
            WCNFTI08,
            WCNFTI09,
            WAAFTI01,
            WAAFTI02,
            WAAFTI02,
            WAAFTI03,
            WAAFTI04,
            WAAFTI05,
            WAAFTI06,
            WAAFTI07,
            WAAFTI08,
            WAAFTI09,
        )
    pdb.commit()
    return pdb
    #     palavrachaveArquivo = open(palavrachavePath, 'r')
    #
    #     publicacaoPath = '/Mestrado-2016/tabelas_dump/publicacao.csv'
    #     publicacaoArquivo = open(publicacaoPath, 'r')
    #
    #     autoresPath = '/Mestrado-2016/tabelas_dump/autorpublicacao.csv'
    #     autoresArquivo = open(autoresPath, 'r')
    #

    infoPath = '/Mestrado-2016/tabelas_dump/infoImportantes2000_2005.csv'
    infoArquivo = open(infoPath, 'w')

    infoArquivo.write('idpublication;year;keywords;authors\n')
    kdb = Base('/Mestrado-2016/tabelas_dump/palavra.pdl')
    kdb.open()
    kdb.create_index('idpublicacao')

    pdb = Base('/Mestrado-2016/tabelas_dump/publicacao.pdl')
    pdb.open()
    pdb.create_index('idpublicacao')

    adb = Base('/Mestrado-2016/tabelas_dump/autor.pdl')
    adb.open()
    adb.create_index('idpublicacao')

    pub = [r for r in pdb if r['ano'] >= 2000 and r['ano'] <= 2005]
    i = 0
    tamanho = len(pub)

    for row in pub:
        inicio = datetime.now()
Exemplo n.º 18
0
#!env/bin/python3

import json
import os

from pydblite import Base

db = Base('test.pdl')

db.create('owner', 'name', 'game', 'char', mode='override')
db.create_index('owner')
db.create_index('game')

c = {
    'name': 'Covfefe',
    'race': 'dwarf',
    'level': [1],
    'class': ['wizard#d20srd'],
    'class_features': [{'specialization': 'conjuration'}],
    'stats': { 
        'str': 10,
        'dex': 15,
        'con': 15,
        'int': 16,
        'wis': 11,
        'cha': 10  },
    'skills': {
        'spellcraft': {
            'class': True,
            'ranks': 1,
            'check': 'int'
Exemplo n.º 19
0

#iterate over all records
def fp():  #full print
    for r in db:
        print(r)
    print('#######')


fp()

#direct access to entry with id -> rec_id
record = db[0]  #check: record['__id__'] = rec_id

#index field
db.create_index('age')

#db search supports built-in comparison functions and return lists
for r in db('age') >= 20:
    print(r)
print('#######')
print('shortguy')
for r in db('name').ilike('jeremy'):
    print(r)
print('#######')
#db support list comprehension
really_really_really_short = next(r for r in db('size') < 1.0)

#update supports record(s) value(s) and updates the indicie
db.update(really_really_really_short, size=0.1)
fp()  #even shorter
def process(bot, chat_id, parsedCommand, messageText, currentMessage, update,
            instanceAge):
    def sendText(givenText,
                 replyingMessageID=0,
                 keyboardLayout=[]
                 ):  # A simple wrapper for botSendFunctions.sendText()
        botSendFunctions.sendText(bot, chat_id, givenText, replyingMessageID,
                                  keyboardLayout)

    gameRecords = Base(
        "chatStorage/records.pdl"
    )  # The following two lines create a DB to map gameID's to group chat ID's and player ID's
    gameRecords.create("gameID",
                       "groupChatID",
                       "memberUsernames",
                       "memberUserIDs",
                       "memberChatIDs",
                       "memberPoints",
                       "creator",
                       "creatorChatID",
                       "started",
                       mode="open")
    gameRecords.create_index(
        "gameID")  # Create a index to make selections by gameID
    gameRecords.create_index(
        "groupChatID")  # Create a index to make selections by groupChatID
    gameRecords.commit()  # Save changes to disk

    try:
        try:
            chatInstances[chat_id]['checking'] = True
        except Exception:
            chatInstances[chat_id] = {'checking': True}
        print("Processing command " + messageText)
        if parsedCommand == "/help" or parsedCommand == "/start":  # The default command Telegram sends to a bot is /start
            s = "This is the Cards Against Huge Manatees Bot\n"
            s += "To create a game type /newgame in a group chat\n"
            s += "To join a game type /join [gameID] in a private chat with the bot\n"
            s += "To start the game after everyone has joined type /startgame in the group chat.\n"
            s += "A new black card will appear in the group chat and your white cards will appear\n"
            s += "in your private chat. The judge will choose the winner of each round.\n"
            s += "To end the game type /quit in the group chat\n"
            s += "To add a custom black card type /addblackcard the card text here To insert a blank use 5 underscores\n"
            s += "To add a custom white card type /addwhitecard the card text here"

            sendText(s)
        elif parsedCommand == "/newgame":
            ident = game.generate()  # Generate a game ID
            gameRecords.insert(ident, str(chat_id), "", "", "", "",
                               currentMessage.from_user.id, "",
                               0)  # Make a new database record skeleton
            gameRecords.commit()
            sendText("The game ID is " + ident + " Please type /join " +
                     ident + " in a private chat with the bot."
                     )  # Send the ID to the group chat
        elif parsedCommand == "/join":
            rec = gameRecords._gameID[currentMessage.text.upper().split()
                                      [1]]  # Get the DB record by GameID
            if not rec:
                sendText("Game ID not found.")
                return
            rec = rec[-1]
            if rec['started']:  # If the game has already started they can't join.
                sendText("The game has already started. Sorry.")
                return
            if rec['groupChatID'] != str(chat_id):
                memberChats = rec['memberChatIDs']
                memberIDs = rec['memberUserIDs']
                memberNames = rec['memberUsernames']
                points = rec['memberPoints']
                if str(chat_id) not in memberChats:
                    if str(currentMessage.from_user.id) == str(rec['creator']):
                        gameRecords.update(rec, creatorChatID=chat_id)
                        gameRecords.commit()
                        sendText("You are the judge of game " +
                                 str(rec['gameID']))
                        return
                    memberChats += str(
                        chat_id
                    ) + " "  # String to list and back for the database.
                    memberIDs += str(currentMessage.from_user.id) + " "
                    memberNames += str(
                        currentMessage.from_user.first_name) + " "
                    points += "0 "
                    gameRecords.update(
                        rec,
                        memberUsernames=memberNames,
                        memberUserIDs=memberIDs,
                        memberChatIDs=memberChats,
                        memberPoints=points
                    )  # On every join update the database record
                    gameRecords.commit()
                    sendText("You have successfully joined the game " +
                             str(rec['gameID']))
                else:
                    sendText("You have already joined the game.")
            else:
                sendText(
                    "Please type this command in a private chat with the bot.")
        elif parsedCommand == "/startgame":
            try:
                rec = gameRecords._groupChatID[str(chat_id)]
                rec = rec[-1]
                if not rec['started']:
                    game.initGameEnv(rec['gameID'])
                    game.playGame(bot, rec['gameID'])
                    gameRecords.update(rec, started=1)
                    gameRecords.commit()
                else:
                    sendText("Game already started.")
            except Exception:
                traceback.format_exc()
                sendText("Error. No game record for this chat found.")
        elif parsedCommand == "/ans":
            game.getAnswers(bot, currentMessage, chat_id)
        elif parsedCommand == "/win":
            game.winner(bot, currentMessage, chat_id)
        elif parsedCommand == "/quit":
            game.endGame(bot, currentMessage, chat_id)
        elif parsedCommand == "/addblackcard":
            admin.customBlackCard(bot, currentMessage, chat_id)
        elif parsedCommand == "/addwhitecard":
            admin.customWhiteCard(bot, currentMessage, chat_id)
        elif parsedCommand[0] == "/":  # Error handling
            sendText("Not a valid command")

    except Exception:
        print(traceback.format_exc())
#     palavrachaveArquivo = open(palavrachavePath, 'r')
#     
#     publicacaoPath = '/Mestrado-2016/tabelas_dump/publicacao.csv'
#     publicacaoArquivo = open(publicacaoPath, 'r')
#     
#     autoresPath = '/Mestrado-2016/tabelas_dump/autorpublicacao.csv'
#     autoresArquivo = open(autoresPath, 'r')
#     
    
    infoPath = '/Mestrado-2016/tabelas_dump/infoImportantes2000_2005.csv'
    infoArquivo = open(infoPath, 'w')
    
    infoArquivo.write('idpublication;year;keywords;authors\n')
    kdb = Base('/Mestrado-2016/tabelas_dump/palavra.pdl')
    kdb.open()
    kdb.create_index('idpublicacao')
    
    pdb = Base('/Mestrado-2016/tabelas_dump/publicacao.pdl')
    pdb.open()
    pdb.create_index('idpublicacao')
    
    adb = Base('/Mestrado-2016/tabelas_dump/autor.pdl')
    adb.open()
    adb.create_index('idpublicacao')
    
    pub = [r for r in pdb if r['ano'] >= 2000 and r['ano'] <= 2005 ]
    i = 0
    tamanho = len(pub)
    
    for row in pub:
        inicio = datetime.now()
Exemplo n.º 22
0
def calculatingWeights(graph, nodesnotLinked, database, calculatingFile):
    pdb = Base(calculatingFile)
    pdb.create('node1', 'node2', 'WCNFTI01','WCNFTI02', 'WCNFTI03','WCNFTI04','WCNFTI05','WCNFTI06','WCNFTI07','WCNFTI08','WCNFTI09','WAAFTI01','WAAFTI02', 'WAAFTI03','WAAFTI04','WAAFTI05','WAAFTI06','WAAFTI07','WAAFTI08','WAAFTI09')
    pdb.create_index('node1', 'node2')
                
    element = 0
    qtyofNodesToProcess = len(nodesnotLinked)
    for pair in nodesnotLinked:
        element = element+1
        FormatingDataSets.printProgressofEvents(element, qtyofNodesToProcess, "Calculating features for nodes not liked: ")
        neighbors_node1 = all_neighbors(graph, pair[0])
        neighbors_node2 = all_neighbors(graph, pair[1])
        len_neihbors_node1 = len(neighbors_node1)
        len_neihbors_node2 = len(neighbors_node2)
        CommonNeigbors = neighbors_node1.intersection(neighbors_node2)
        WCNFTI01 = 0;
        WCNFTI02 = 0;
        WCNFTI03 = 0;
        WCNFTI04 = 0;
        WCNFTI05 = 0;
        WCNFTI06 = 0;
        WCNFTI07 = 0;
        WCNFTI08 = 0;
        WCNFTI09 = 0;
        
        WAAFTI01 = 0;
        WAAFTI02 = 0;
        WAAFTI03 = 0;
        WAAFTI04 = 0;
        WAAFTI05 = 0;
        WAAFTI06 = 0;
        WAAFTI07 = 0;
        WAAFTI08 = 0;
        WAAFTI09 = 0;
        
        
        for cn in CommonNeigbors:
            item = get_partOfWeightCalculating(graph, database, pair, cn)
            WCNFTI01 = WCNFTI01 + item['WCN'][0];
            WCNFTI02 = WCNFTI02 + item['WCN'][1];
            WCNFTI03 = WCNFTI03 + item['WCN'][2];
            WCNFTI04 = WCNFTI04 + item['WCN'][3];
            WCNFTI05 = WCNFTI05 + item['WCN'][4];
            WCNFTI06 = WCNFTI06 + item['WCN'][5];
            WCNFTI07 = WCNFTI07 + item['WCN'][6];
            WCNFTI08 = WCNFTI08 + item['WCN'][7];
            WCNFTI09 = WCNFTI09 + item['WCN'][8];
            
            WAAFTI01 = WAAFTI01 + item['WAA'][0];
            WAAFTI02 = WAAFTI02 + item['WAA'][1];
            WAAFTI03 = WAAFTI03 + item['WAA'][2];
            WAAFTI04 = WAAFTI04 + item['WAA'][3];
            WAAFTI05 = WAAFTI05 + item['WAA'][4];
            WAAFTI06 = WAAFTI06 + item['WAA'][5];
            WAAFTI07 = WAAFTI07 + item['WAA'][6];
            WAAFTI08 = WAAFTI08 + item['WAA'][7];
            WAAFTI09 = WAAFTI09 + item['WAA'][8];
            
        pdb.insert(str(pair[0]), str(pair[1]), WCNFTI01, WCNFTI02,  WCNFTI02,
                   WCNFTI03,WCNFTI04,WCNFTI05,WCNFTI06,WCNFTI07,WCNFTI08,WCNFTI09,
                   WAAFTI01, WAAFTI02,  WAAFTI02,
                   WAAFTI03,WAAFTI04,WAAFTI05,WAAFTI06,WAAFTI07,WAAFTI08,WAAFTI09,
                    
                    )   
    pdb.commit()
    return pdb;