lexibank/scripts/initializedb.py
def prime_cache(args):
    """If data needs to be denormalized for lookup, do that here.
    This procedure should be separate from the db initialization, because
    it will have to be run periodically whenever data has been updated.
    """
    concept_labels = {r[0]: r[1] for r in
                      DBSession.query(common.Parameter.pk, common.Parameter.name)}
    for cogset in DBSession.query(Cognateset) \
            .options(joinedload(
                Cognateset.counterparts).joinedload(
                CognatesetCounterpart.counterpart).joinedload(
                common.Value.valueset)):
        concepts = set()
        for cp in cogset.counterparts:
            concepts.add(cp.counterpart.valueset.parameter_pk)
        cogset.name = '-'.join(sorted([concept_labels[pk] for pk in concepts]))
        cogset.representation = len(cogset.counterparts)

    for concept in DBSession.query(Concept):
        concept.representation = DBSession.query(common.Language)\
            .join(common.ValueSet)\
            .filter(common.ValueSet.parameter_pk == concept.pk)\
            .distinct()\
            .count()

    for prov in DBSession.query(Provider):
        q = DBSession.query(common.ValueSet.language_pk)\
            .filter(common.ValueSet.contribution_pk == prov.pk)\
            .distinct()
        prov.language_count = q.count()
        prov.update_jsondata(language_pks=[r[0] for r in q])

        prov.parameter_count = DBSession.query(common.ValueSet.parameter_pk) \
            .filter(common.ValueSet.contribution_pk == prov.pk) \
            .distinct() \
            .count()
        prov.lexeme_count = DBSession.query(common.Value.pk)\
            .join(common.ValueSet)\
            .filter(common.ValueSet.contribution_pk == prov.pk)\
            .count()

        syns = defaultdict(dict)
        vs = common.ValueSet.__table__
        cp = Counterpart.__table__
        v = common.Value.__table__
        for vn, lpk, ppk, count in DBSession.query(
            cp.c.variety_name,
            vs.c.language_pk,
            vs.c.parameter_pk,
            func.count(v.c.pk)) \
            .filter(cp.c.pk == v.c.pk) \
            .filter(v.c.valueset_pk == vs.c.pk) \
            .filter(vs.c.contribution_pk == prov.pk) \
            .group_by(
            cp.c.variety_name,
            vs.c.language_pk,
            vs.c.parameter_pk
        ):
            syns[(vn, lpk)][ppk] = count

        if syns:
            prov.synonym_index = sum(
                [sum(list(counts.values())) / len(counts)
                 for counts in syns.values()]) / len(set(syns.keys()))
If data needs to be denormalized for lookup, do that here.
    This procedure should be separate from the db initialization, because
    it will have to be run periodically whenever data has been updated.
    
If data needs to be denormalized for lookup, do that here.
This procedure should be separate from the db initialization, because
it will have to be run periodically whenever data has been updated.
If data needs to be denormalized for lookup do that here . This procedure should be separate from the db initialization because it will have to be run periodically whenever data has been updated .
def prime_cache(args):
    concept_labels = {r[0]: r[1] for r in
                      DBSession.query(common.Parameter.pk, common.Parameter.name)}
    for cogset in DBSession.query(Cognateset) \
            .options(joinedload(
                Cognateset.counterparts).joinedload(
                CognatesetCounterpart.counterpart).joinedload(
                common.Value.valueset)):
        concepts = set()
        for cp in cogset.counterparts:
            concepts.add(cp.counterpart.valueset.parameter_pk)
        cogset.name = '-'.join(sorted([concept_labels[pk] for pk in concepts]))
        cogset.representation = len(cogset.counterparts)
    for concept in DBSession.query(Concept):
        concept.representation = DBSession.query(common.Language)\
            .join(common.ValueSet)\
            .filter(common.ValueSet.parameter_pk == concept.pk)\
            .distinct()\
            .count()
    for prov in DBSession.query(Provider):
        q = DBSession.query(common.ValueSet.language_pk)\
            .filter(common.ValueSet.contribution_pk == prov.pk)\
            .distinct()
        prov.language_count = q.count()
        prov.update_jsondata(language_pks=[r[0] for r in q])
        prov.parameter_count = DBSession.query(common.ValueSet.parameter_pk) \
            .filter(common.ValueSet.contribution_pk == prov.pk) \
            .distinct() \
            .count()
        prov.lexeme_count = DBSession.query(common.Value.pk)\
            .join(common.ValueSet)\
            .filter(common.ValueSet.contribution_pk == prov.pk)\
            .count()
        syns = defaultdict(dict)
        vs = common.ValueSet.__table__
        cp = Counterpart.__table__
        v = common.Value.__table__
        for vn, lpk, ppk, count in DBSession.query(
            cp.c.variety_name,
            vs.c.language_pk,
            vs.c.parameter_pk,
            func.count(v.c.pk)) \
            .filter(cp.c.pk == v.c.pk) \
            .filter(v.c.valueset_pk == vs.c.pk) \
            .filter(vs.c.contribution_pk == prov.pk) \
            .group_by(
            cp.c.variety_name,
            vs.c.language_pk,
            vs.c.parameter_pk
        ):
            syns[(vn, lpk)][ppk] = count
        if syns:
            prov.synonym_index = sum(
                [sum(list(counts.values())) / len(counts)
                 for counts in syns.values()]) / len(set(syns.keys()))
def prime_cache ( args ) : concept_labels = { r [ 0 ] : r [ 1 ] for r in DBSession . query ( common . Parameter . pk , common . Parameter . name ) } for cogset in DBSession . query ( Cognateset ) . options ( joinedload ( Cognateset . counterparts ) . joinedload ( CognatesetCounterpart . counterpart ) . joinedload ( common . Value . valueset ) ) : concepts = set ( ) for cp in cogset . counterparts : concepts . add ( cp . counterpart . valueset . parameter_pk ) cogset . name = '-' . join ( sorted ( [ concept_labels [ pk ] for pk in concepts ] ) ) cogset . representation = len ( cogset . counterparts ) for concept in DBSession . query ( Concept ) : concept . representation = DBSession . query ( common . Language ) . join ( common . ValueSet ) . filter ( common . ValueSet . parameter_pk == concept . pk ) . distinct ( ) . count ( ) for prov in DBSession . query ( Provider ) : q = DBSession . query ( common . ValueSet . language_pk ) . filter ( common . ValueSet . contribution_pk == prov . pk ) . distinct ( ) prov . language_count = q . count ( ) prov . update_jsondata ( language_pks = [ r [ 0 ] for r in q ] ) prov . parameter_count = DBSession . query ( common . ValueSet . parameter_pk ) . filter ( common . ValueSet . contribution_pk == prov . pk ) . distinct ( ) . count ( ) prov . lexeme_count = DBSession . query ( common . Value . pk ) . join ( common . ValueSet ) . filter ( common . ValueSet . contribution_pk == prov . pk ) . count ( ) syns = defaultdict ( dict ) vs = common . ValueSet . __table__ cp = Counterpart . __table__ v = common . Value . __table__ for vn , lpk , ppk , count in DBSession . query ( cp . c . variety_name , vs . c . language_pk , vs . c . parameter_pk , func . count ( v . c . pk ) ) . filter ( cp . c . pk == v . c . pk ) . filter ( v . c . valueset_pk == vs . c . pk ) . filter ( vs . c . contribution_pk == prov . pk ) . group_by ( cp . c . variety_name , vs . c . language_pk , vs . c . parameter_pk ) : syns [ ( vn , lpk ) ] [ ppk ] = count if syns : prov . synonym_index = sum ( [ sum ( list ( counts . values ( ) ) ) / len ( counts ) for counts in syns . values ( ) ] ) / len ( set ( syns . keys ( ) ) )
If data needs to be denormalized for lookup, do that here.
If data needs to be denormalized for lookup do that here .
"""If data needs to be denormalized for lookup, do that here.
    This procedure should be separate from the db initialization, because
    it will have to be run periodically whenever data has been updated.
    """
args