mflbackend/src/backend.py
def compile():
    """
    Make a compilation, possible filtered. Contains more information than /list
    Possible values to compile: class, wf/wordform, paradigm
    """
    querystr = request.args.get('q', '')  # querystring
    search_f = request.args.get('s', '')  # search field
    compile_f = request.args.get('c', '')  # compilation field
    # if isfilter is true, the given query string will searched for as a
    # substring: ".*apa.*", instead of "apa"
    isfilter = request.args.get('filter', '')
    isfilter = isfilter in ['true', 'True', True]
    lexicon = request.args.get('lexicon', C.config['default'])
    lexconf = lexconfig.get_lexiconconf(lexicon)
    pos = helpers.read_pos(lexconf)
    size = request.args.get('size', '100')
    start = request.args.get('start', '0')
    query = []  # will contain all parts of the karp query
    if ',' in search_f:
        # Search many fields: 'q=katt,p100_rallarros..nn.1&s=wordform,paradigm'
        # Process and add to the karp query
        helpers.multi_query(lexicon, query, search_f.split(','),
                            querystr.split(','), isfilter)
        # Empty fields so that they are not added to the karp query again
        search_f = ''
        querystr = ''

    # Look up the correct search field name
    search_f = lexconf.get(search_f, search_f)
    if pos:
        query.append('and|%s|equals|%s' % (lexconf["pos"], '|'.join(pos)))

    if compile_f == 'class':
        # Compile on inflectional class: 'class=bklass&classname=2'
        classname = request.args.get('classname', '')
        if querystr:
            # if no search field is given, search the class to compile on
            s_field = search_f or classname
            operator = 'equals' if not isfilter else 'regexp'
            if isfilter:
                querystr = '.*'+querystr+'.*'
            query.append('and|%s|%s|%s' % (s_field, operator, querystr))
        if query:
            query = 'extended||' + '||'.join(query)
        else:
            # if no query is given, list all entries in the lexicon
            query = 'extended||and|lexiconName|equals|%s' %\
                    lexconf['lexiconName']

        buckets = [classname, lexconf['extractparadigm']]

        # ask karp how many classes that are matching the criteria
        count = helpers.karp_query('statistics',
                                   {'q': query,
                                    'buckets': '%s.bucket' % classname,
                                    'cardinality': True,
                                    'size': 1},
                                   resource=lexconf['lexiconName'],
                                   mode=lexconf['lexiconMode'])
        # ask karp for the combination of classes and paradigms (will result in
        # a nested structure from which the count cannot be easily read).
        res = helpers.karp_query('statistics',
                                 {'q': query,
                                  'size': start+size,
                                  'buckets': ','.join(['%s.bucket' % b for b in buckets])},
                                 resource=lexconf['lexiconName'],
                                 mode=lexconf['lexiconMode'])
        ans = []
        for pbucket in helpers.es_get_classbucket(classname, res, lexconf)[int(start):]:
            pcount = len(pbucket[lexconf['extractparadigmpath']]['buckets'])
            # [class name, number of paradigms, number of words]
            ans.append([pbucket["key"], pcount, pbucket["doc_count"]])

        return jsonify({"compiled_on": classname, "stats": ans,
                        "fields": [classname, "paradigm", "entries"],
                        "total": helpers.es_get_classcount(classname, count, lexconf)})

    elif compile_f in ["wf", "wordform"]:
        # Compile on words
        mode = lexconf['lexiconMode']
        # if no search field is given, search the baseform
        s_field = search_f or lexconf["baseform"]
        ans = helpers.compile_list(query, s_field, querystr, lexicon,
                                   lexconf["show"], size, start, mode,
                                   isfilter)

        # get lexicon specific function for how to present the entries
        def default(obj):
            return [obj['baseform']], ['baseform']
        func = helpers.extra_src(lexicon, 'make_overview', default)
        out, fields = [], []
        for obj in ans["ans"]:
            row, fields = func(obj)
            out.append(row)

        return jsonify({"compiled_on": "wordform", "stats": out,
                        "fields": fields, "total": ans["total"]})

    elif compile_f == "paradigm":
        # Compile on paradigms
        # if no search field is given, search the baseform
        s_field = search_f or pp.id_field
        show = ','.join([pp.id_field,
                         pp.transform_field,
                         pp.entries])
        lexicon = lexconf['paradigmlexiconName']
        mode = lexconf['paradigmMode']
        ans = helpers.compile_list(query, s_field, querystr, lexicon, show,
                                   size, start, mode, isfilter)
        res = []
        iclasses = []
        for hit in ans["ans"]:
            # the inflectional classes that are present.
            # only need one instance, so empty each iteration
            iclasses = []
            stats = [hit[pp.id_field]]
            for iclass in lexconf['inflectionalclass'].keys():
                stats.append(len(pp.get_transformcat(hit, iclass)))
                iclasses.append(iclass)
            stats.append(pp.get_entries(hit))
            res.append(stats)
        return jsonify({"compiled_on": "paradigm", "stats": res,
                        "fields": ['paradigm']+iclasses+['entries'],
                        "total": ans["total"]})

    else:
        raise e.MflException("Don't know what to do", code="unknown_action")

    Make a compilation, possible filtered. Contains more information than /list
    Possible values to compile: class, wf/wordform, paradigm
    
Make a compilation, possible filtered. Contains more information than /list
Possible values to compile: class, wf/wordform, paradigm
Make a compilation possible filtered . Contains more information than / list Possible values to compile : class wf / wordform paradigm
def compile():
    querystr = request.args.get('q', '')  
    search_f = request.args.get('s', '')  
    compile_f = request.args.get('c', '')  
    isfilter = request.args.get('filter', '')
    isfilter = isfilter in ['true', 'True', True]
    lexicon = request.args.get('lexicon', C.config['default'])
    lexconf = lexconfig.get_lexiconconf(lexicon)
    pos = helpers.read_pos(lexconf)
    size = request.args.get('size', '100')
    start = request.args.get('start', '0')
    query = []  
    if ',' in search_f:
        helpers.multi_query(lexicon, query, search_f.split(','),
                            querystr.split(','), isfilter)
        search_f = ''
        querystr = ''
    search_f = lexconf.get(search_f, search_f)
    if pos:
        query.append('and|%s|equals|%s' % (lexconf["pos"], '|'.join(pos)))
    if compile_f == 'class':
        classname = request.args.get('classname', '')
        if querystr:
            s_field = search_f or classname
            operator = 'equals' if not isfilter else 'regexp'
            if isfilter:
                querystr = '.*'+querystr+'.*'
            query.append('and|%s|%s|%s' % (s_field, operator, querystr))
        if query:
            query = 'extended||' + '||'.join(query)
        else:
            query = 'extended||and|lexiconName|equals|%s' %\
                    lexconf['lexiconName']
        buckets = [classname, lexconf['extractparadigm']]
        count = helpers.karp_query('statistics',
                                   {'q': query,
                                    'buckets': '%s.bucket' % classname,
                                    'cardinality': True,
                                    'size': 1},
                                   resource=lexconf['lexiconName'],
                                   mode=lexconf['lexiconMode'])
        res = helpers.karp_query('statistics',
                                 {'q': query,
                                  'size': start+size,
                                  'buckets': ','.join(['%s.bucket' % b for b in buckets])},
                                 resource=lexconf['lexiconName'],
                                 mode=lexconf['lexiconMode'])
        ans = []
        for pbucket in helpers.es_get_classbucket(classname, res, lexconf)[int(start):]:
            pcount = len(pbucket[lexconf['extractparadigmpath']]['buckets'])
            ans.append([pbucket["key"], pcount, pbucket["doc_count"]])
        return jsonify({"compiled_on": classname, "stats": ans,
                        "fields": [classname, "paradigm", "entries"],
                        "total": helpers.es_get_classcount(classname, count, lexconf)})
    elif compile_f in ["wf", "wordform"]:
        mode = lexconf['lexiconMode']
        s_field = search_f or lexconf["baseform"]
        ans = helpers.compile_list(query, s_field, querystr, lexicon,
                                   lexconf["show"], size, start, mode,
                                   isfilter)
        def default(obj):
            return [obj['baseform']], ['baseform']
        func = helpers.extra_src(lexicon, 'make_overview', default)
        out, fields = [], []
        for obj in ans["ans"]:
            row, fields = func(obj)
            out.append(row)
        return jsonify({"compiled_on": "wordform", "stats": out,
                        "fields": fields, "total": ans["total"]})
    elif compile_f == "paradigm":
        s_field = search_f or pp.id_field
        show = ','.join([pp.id_field,
                         pp.transform_field,
                         pp.entries])
        lexicon = lexconf['paradigmlexiconName']
        mode = lexconf['paradigmMode']
        ans = helpers.compile_list(query, s_field, querystr, lexicon, show,
                                   size, start, mode, isfilter)
        res = []
        iclasses = []
        for hit in ans["ans"]:
            iclasses = []
            stats = [hit[pp.id_field]]
            for iclass in lexconf['inflectionalclass'].keys():
                stats.append(len(pp.get_transformcat(hit, iclass)))
                iclasses.append(iclass)
            stats.append(pp.get_entries(hit))
            res.append(stats)
        return jsonify({"compiled_on": "paradigm", "stats": res,
                        "fields": ['paradigm']+iclasses+['entries'],
                        "total": ans["total"]})
    else:
        raise e.MflException("Don't know what to do", code="unknown_action")
def compile ( ) : querystr = request . args . get ( 'q' , '' ) search_f = request . args . get ( 's' , '' ) compile_f = request . args . get ( 'c' , '' ) isfilter = request . args . get ( 'filter' , '' ) isfilter = isfilter in [ 'true' , 'True' , True ] lexicon = request . args . get ( 'lexicon' , C . config [ 'default' ] ) lexconf = lexconfig . get_lexiconconf ( lexicon ) pos = helpers . read_pos ( lexconf ) size = request . args . get ( 'size' , '100' ) start = request . args . get ( 'start' , '0' ) query = [ ] if ',' in search_f : helpers . multi_query ( lexicon , query , search_f . split ( ',' ) , querystr . split ( ',' ) , isfilter ) search_f = '' querystr = '' search_f = lexconf . get ( search_f , search_f ) if pos : query . append ( 'and|%s|equals|%s' % ( lexconf [ "pos" ] , '|' . join ( pos ) ) ) if compile_f == 'class' : classname = request . args . get ( 'classname' , '' ) if querystr : s_field = search_f or classname operator = 'equals' if not isfilter else 'regexp' if isfilter : querystr = '.*' + querystr + '.*' query . append ( 'and|%s|%s|%s' % ( s_field , operator , querystr ) ) if query : query = 'extended||' + '||' . join ( query ) else : query = 'extended||and|lexiconName|equals|%s' % lexconf [ 'lexiconName' ] buckets = [ classname , lexconf [ 'extractparadigm' ] ] count = helpers . karp_query ( 'statistics' , { 'q' : query , 'buckets' : '%s.bucket' % classname , 'cardinality' : True , 'size' : 1 } , resource = lexconf [ 'lexiconName' ] , mode = lexconf [ 'lexiconMode' ] ) res = helpers . karp_query ( 'statistics' , { 'q' : query , 'size' : start + size , 'buckets' : ',' . join ( [ '%s.bucket' % b for b in buckets ] ) } , resource = lexconf [ 'lexiconName' ] , mode = lexconf [ 'lexiconMode' ] ) ans = [ ] for pbucket in helpers . es_get_classbucket ( classname , res , lexconf ) [ int ( start ) : ] : pcount = len ( pbucket [ lexconf [ 'extractparadigmpath' ] ] [ 'buckets' ] ) ans . append ( [ pbucket [ "key" ] , pcount , pbucket [ "doc_count" ] ] ) return jsonify ( { "compiled_on" : classname , "stats" : ans , "fields" : [ classname , "paradigm" , "entries" ] , "total" : helpers . es_get_classcount ( classname , count , lexconf ) } ) elif compile_f in [ "wf" , "wordform" ] : mode = lexconf [ 'lexiconMode' ] s_field = search_f or lexconf [ "baseform" ] ans = helpers . compile_list ( query , s_field , querystr , lexicon , lexconf [ "show" ] , size , start , mode , isfilter ) def default ( obj ) : return [ obj [ 'baseform' ] ] , [ 'baseform' ] func = helpers . extra_src ( lexicon , 'make_overview' , default ) out , fields = [ ] , [ ] for obj in ans [ "ans" ] : row , fields = func ( obj ) out . append ( row ) return jsonify ( { "compiled_on" : "wordform" , "stats" : out , "fields" : fields , "total" : ans [ "total" ] } ) elif compile_f == "paradigm" : s_field = search_f or pp . id_field show = ',' . join ( [ pp . id_field , pp . transform_field , pp . entries ] ) lexicon = lexconf [ 'paradigmlexiconName' ] mode = lexconf [ 'paradigmMode' ] ans = helpers . compile_list ( query , s_field , querystr , lexicon , show , size , start , mode , isfilter ) res = [ ] iclasses = [ ] for hit in ans [ "ans" ] : iclasses = [ ] stats = [ hit [ pp . id_field ] ] for iclass in lexconf [ 'inflectionalclass' ] . keys ( ) : stats . append ( len ( pp . get_transformcat ( hit , iclass ) ) ) iclasses . append ( iclass ) stats . append ( pp . get_entries ( hit ) ) res . append ( stats ) return jsonify ( { "compiled_on" : "paradigm" , "stats" : res , "fields" : [ 'paradigm' ] + iclasses + [ 'entries' ] , "total" : ans [ "total" ] } ) else : raise e . MflException ( "Don't know what to do" , code = "unknown_action" )
Make a compilation, possible filtered.
Make a compilation possible filtered .
"""
    Make a compilation, possible filtered. Contains more information than /list
    Possible values to compile: class, wf/wordform, paradigm
    """ # querystring # search field # compilation field # if isfilter is true, the given query string will searched for as a # substring: ".*apa.*", instead of "apa" # will contain all parts of the karp query # Search many fields: 'q=katt,p100_rallarros..nn.1&s=wordform,paradigm' # Process and add to the karp query # Empty fields so that they are not added to the karp query again # Look up the correct search field name # Compile on inflectional class: 'class=bklass&classname=2' # if no search field is given, search the class to compile on # if no query is given, list all entries in the lexicon # ask karp how many classes that are matching the criteria # ask karp for the combination of classes and paradigms (will result in # a nested structure from which the count cannot be easily read). # [class name, number of paradigms, number of words] # Compile on words # if no search field is given, search the baseform # get lexicon specific function for how to present the entries # Compile on paradigms # if no search field is given, search the baseform # the inflectional classes that are present. # only need one instance, so empty each iteration