mflbackend/src/handleparadigms.py
def make_new_table(lexicon, table, paradigm, paradigms, identifier, baseform,
                   pos, classes, ppriorv=None, newword=False, newpara=False):
    """
    Check that the given table and identifiers are ok, that the table matches
    given paradigm and then add the table to the paradigm,
    in karp and internally.
    Args:
        lexicon (str): the lexicon name
        table (str): the comma separated word forms, possibly with msds.
            "katt,katter|pl indef nom,katts"
        paradigm (str): a paradigm name
        paradigms (dict): a dictionary with all paradigms
            '{"lexname": {"nn": [], "vb": []}'
        identifier (str): the word's identifier
        baseform (str): the word's baseform
        pos (str): the tables word class
        classes (str): input classes 'bklass:2,cklass:3'
        ppriorv (int, optional): setting for pextract
        newword (bool, optional): is the word new? Defaults to false.
        newpara (bool, optional): is the paradigm new? Defaults to false.

    Returns:
       a tuple (identifier, wf_table, para, v, classes)
         where
         identifier (str): identifier of  the table
         wf_table (obj): the formatted inflection table
         para (obj): the paradigm object
         v (list): the variable instances
         classes (dict): the formatted classes of the word
    """
    # check that the table's identier is unique
    # if it is new: make sure that it doesn't already exists.
    #    otherwise: construct a new one
    # if it is not new: make sure that it does exists.
    #    otherwise: fail
    lresource = lexconfig.get_lexiconname(lexicon)
    ok = helpers.check_identifier(identifier,
                                  lexconfig.get_identifierfield(lexicon),
                                  lresource,
                                  lexconfig.get_lexiconmode(lexicon),
                                  unique=newword, fail=not newword)
    if not ok:
        logging.debug('identifier %s not ok', identifier)
        lexconf = lexconfig.get_lexiconconf(lexicon)  # TODO remove after refactoring helpers
        word = baseform or helpers.get_baseform(lexconf, identifier)
        identifier = helpers.make_identifier(lexicon, word, pos)
        logging.debug('\t...use %s', identifier)

    # make sure that all required fields are present
    required = [('identifier', identifier), ('paradigm', paradigm),
                ('partOfSpeech', pos)]
    for name, field in required:
        if not field:
            raise errs.MflException("Both identifier, partOfSpeech and paradigm must be given!",
                                    code="unknown_%s" % name)
    # parse input classes
    try:
        logging.debug('make classes: %s', classes)
        if classes:
            classes = dict([c.split(':') for c in classes.split(',')])
        else:
            classes = {}
    except Exception as e1:
        logging.warning('Could not parse classes')
        logging.error(e1)
        raise errs.MflException("Could not parse classes. Format should be 'classname:apa,classname2:bepa'",
                                classes,
                                code="unparsable_class")

    paras, numex, lms = helpers.relevant_paradigms(paradigms, lexicon, pos)
    if newpara and paradigm in [p.name for p in paras]:
        raise errs.MflException("Paradigm name %s is already used" % (paradigm),
                                code="unique_paradigm")

    # parse the input table into pextract and karp format
    pex_table = helpers.tableize(table, add_tags=False)
    wf_table = helpers.karp_wftableize(lexicon, paradigm, table, classes,
                                       baseform=baseform, identifier=identifier,
                                       pos=pos,
                                       resource=lresource)
    if newpara:
        # prepare to try all paradigm
        fittingparadigms = paras
        # check that this is a new name
        helpers.check_identifier(paradigm, pp.id_field,
                                 lexconfig.get_paradigmlexicon(lexicon),
                                 lexconfig.get_paradigmmode(lexicon))

    else:
        logging.debug('not new, look for %s', paradigm)
        # prepare to try only the given paradigm
        fittingparadigms = [p for p in paras if p.p_id == paradigm]
        if not fittingparadigms:
            raise errs.MflException("Could not find paradigm %s" % paradigm,
                                    code="unknown_paradigm")

    logging.debug('fitting %s', fittingparadigms)
    logging.debug('pex_table %s %s', pex_table[0], pex_table[1])
    if ppriorv is None:
        ppriorv = lexconfig.get_pprior(lexicon)

    ans = mp.test_paradigms(pex_table, fittingparadigms, numex, lms,
                            C.config["print_tables"], C.config["debug"],
                            ppriorv, returnempty=False,
                            match_all=True)

    if newpara and ans:
        logging.warning("Could inflect %s as %s", table, ans[0][1].p_id)
        raise errs.MflException("Table should belong to paradigm %s" % (ans[0][1].p_id),
                                code="inflect_problem")

    if not newpara and not ans:
        logging.warning("Could not inflect %s as %s", table, paradigm)
        raise errs.MflException("Table can not belong to paradigm %s" % (paradigm),
                                code="inflect_problem")

    if ans:
        # found a match, add the word to this paradig
        score, para, v = ans[0]
        add_word_to_paradigm(lexicon, para, paradigms, identifier, pos,
                             classes, v)

    else:
        # if no results are found (no matching paradigms), make a new paradigm
        # giving exactly the input forms
        pex_table = helpers.tableize(table, add_tags=False,
                                     identifier=identifier)
        para = pex.learnparadigms([pex_table])[0]
        v = [var for ix, var in para.var_insts[0][1:]]
        add_paradigm(lexicon, paradigm, para, paradigms, identifier, pos,
                     classes)

    return identifier, wf_table, para, v, classes

    Check that the given table and identifiers are ok, that the table matches
    given paradigm and then add the table to the paradigm,
    in karp and internally.
    Args:
        lexicon (str): the lexicon name
        table (str): the comma separated word forms, possibly with msds.
            "katt,katter|pl indef nom,katts"
        paradigm (str): a paradigm name
        paradigms (dict): a dictionary with all paradigms
            '{"lexname": {"nn": [], "vb": []}'
        identifier (str): the word's identifier
        baseform (str): the word's baseform
        pos (str): the tables word class
        classes (str): input classes 'bklass:2,cklass:3'
        ppriorv (int, optional): setting for pextract
        newword (bool, optional): is the word new? Defaults to false.
        newpara (bool, optional): is the paradigm new? Defaults to false.

    Returns:
       a tuple (identifier, wf_table, para, v, classes)
         where
         identifier (str): identifier of  the table
         wf_table (obj): the formatted inflection table
         para (obj): the paradigm object
         v (list): the variable instances
         classes (dict): the formatted classes of the word
    
Check that the given table and identifiers are ok, that the table matches
given paradigm and then add the table to the paradigm,
in karp and internally.
Check that the given table and identifiers are ok that the table matches given paradigm and then add the table to the paradigm in karp and internally .
def make_new_table(lexicon, table, paradigm, paradigms, identifier, baseform,
                   pos, classes, ppriorv=None, newword=False, newpara=False):
    lresource = lexconfig.get_lexiconname(lexicon)
    ok = helpers.check_identifier(identifier,
                                  lexconfig.get_identifierfield(lexicon),
                                  lresource,
                                  lexconfig.get_lexiconmode(lexicon),
                                  unique=newword, fail=not newword)
    if not ok:
        logging.debug('identifier %s not ok', identifier)
        lexconf = lexconfig.get_lexiconconf(lexicon)  
        word = baseform or helpers.get_baseform(lexconf, identifier)
        identifier = helpers.make_identifier(lexicon, word, pos)
        logging.debug('\t...use %s', identifier)
    required = [('identifier', identifier), ('paradigm', paradigm),
                ('partOfSpeech', pos)]
    for name, field in required:
        if not field:
            raise errs.MflException("Both identifier, partOfSpeech and paradigm must be given!",
                                    code="unknown_%s" % name)
    try:
        logging.debug('make classes: %s', classes)
        if classes:
            classes = dict([c.split(':') for c in classes.split(',')])
        else:
            classes = {}
    except Exception as e1:
        logging.warning('Could not parse classes')
        logging.error(e1)
        raise errs.MflException("Could not parse classes. Format should be 'classname:apa,classname2:bepa'",
                                classes,
                                code="unparsable_class")
    paras, numex, lms = helpers.relevant_paradigms(paradigms, lexicon, pos)
    if newpara and paradigm in [p.name for p in paras]:
        raise errs.MflException("Paradigm name %s is already used" % (paradigm),
                                code="unique_paradigm")
    pex_table = helpers.tableize(table, add_tags=False)
    wf_table = helpers.karp_wftableize(lexicon, paradigm, table, classes,
                                       baseform=baseform, identifier=identifier,
                                       pos=pos,
                                       resource=lresource)
    if newpara:
        fittingparadigms = paras
        helpers.check_identifier(paradigm, pp.id_field,
                                 lexconfig.get_paradigmlexicon(lexicon),
                                 lexconfig.get_paradigmmode(lexicon))
    else:
        logging.debug('not new, look for %s', paradigm)
        fittingparadigms = [p for p in paras if p.p_id == paradigm]
        if not fittingparadigms:
            raise errs.MflException("Could not find paradigm %s" % paradigm,
                                    code="unknown_paradigm")
    logging.debug('fitting %s', fittingparadigms)
    logging.debug('pex_table %s %s', pex_table[0], pex_table[1])
    if ppriorv is None:
        ppriorv = lexconfig.get_pprior(lexicon)
    ans = mp.test_paradigms(pex_table, fittingparadigms, numex, lms,
                            C.config["print_tables"], C.config["debug"],
                            ppriorv, returnempty=False,
                            match_all=True)
    if newpara and ans:
        logging.warning("Could inflect %s as %s", table, ans[0][1].p_id)
        raise errs.MflException("Table should belong to paradigm %s" % (ans[0][1].p_id),
                                code="inflect_problem")
    if not newpara and not ans:
        logging.warning("Could not inflect %s as %s", table, paradigm)
        raise errs.MflException("Table can not belong to paradigm %s" % (paradigm),
                                code="inflect_problem")
    if ans:
        score, para, v = ans[0]
        add_word_to_paradigm(lexicon, para, paradigms, identifier, pos,
                             classes, v)
    else:
        pex_table = helpers.tableize(table, add_tags=False,
                                     identifier=identifier)
        para = pex.learnparadigms([pex_table])[0]
        v = [var for ix, var in para.var_insts[0][1:]]
        add_paradigm(lexicon, paradigm, para, paradigms, identifier, pos,
                     classes)
    return identifier, wf_table, para, v, classes
def make_new_table ( lexicon , table , paradigm , paradigms , identifier , baseform , pos , classes , ppriorv = None , newword = False , newpara = False ) : lresource = lexconfig . get_lexiconname ( lexicon ) ok = helpers . check_identifier ( identifier , lexconfig . get_identifierfield ( lexicon ) , lresource , lexconfig . get_lexiconmode ( lexicon ) , unique = newword , fail = not newword ) if not ok : logging . debug ( 'identifier %s not ok' , identifier ) lexconf = lexconfig . get_lexiconconf ( lexicon ) word = baseform or helpers . get_baseform ( lexconf , identifier ) identifier = helpers . make_identifier ( lexicon , word , pos ) logging . debug ( '\t...use %s' , identifier ) required = [ ( 'identifier' , identifier ) , ( 'paradigm' , paradigm ) , ( 'partOfSpeech' , pos ) ] for name , field in required : if not field : raise errs . MflException ( "Both identifier, partOfSpeech and paradigm must be given!" , code = "unknown_%s" % name ) try : logging . debug ( 'make classes: %s' , classes ) if classes : classes = dict ( [ c . split ( ':' ) for c in classes . split ( ',' ) ] ) else : classes = { } except Exception as e1 : logging . warning ( 'Could not parse classes' ) logging . error ( e1 ) raise errs . MflException ( "Could not parse classes. Format should be 'classname:apa,classname2:bepa'" , classes , code = "unparsable_class" ) paras , numex , lms = helpers . relevant_paradigms ( paradigms , lexicon , pos ) if newpara and paradigm in [ p . name for p in paras ] : raise errs . MflException ( "Paradigm name %s is already used" % ( paradigm ) , code = "unique_paradigm" ) pex_table = helpers . tableize ( table , add_tags = False ) wf_table = helpers . karp_wftableize ( lexicon , paradigm , table , classes , baseform = baseform , identifier = identifier , pos = pos , resource = lresource ) if newpara : fittingparadigms = paras helpers . check_identifier ( paradigm , pp . id_field , lexconfig . get_paradigmlexicon ( lexicon ) , lexconfig . get_paradigmmode ( lexicon ) ) else : logging . debug ( 'not new, look for %s' , paradigm ) fittingparadigms = [ p for p in paras if p . p_id == paradigm ] if not fittingparadigms : raise errs . MflException ( "Could not find paradigm %s" % paradigm , code = "unknown_paradigm" ) logging . debug ( 'fitting %s' , fittingparadigms ) logging . debug ( 'pex_table %s %s' , pex_table [ 0 ] , pex_table [ 1 ] ) if ppriorv is None : ppriorv = lexconfig . get_pprior ( lexicon ) ans = mp . test_paradigms ( pex_table , fittingparadigms , numex , lms , C . config [ "print_tables" ] , C . config [ "debug" ] , ppriorv , returnempty = False , match_all = True ) if newpara and ans : logging . warning ( "Could inflect %s as %s" , table , ans [ 0 ] [ 1 ] . p_id ) raise errs . MflException ( "Table should belong to paradigm %s" % ( ans [ 0 ] [ 1 ] . p_id ) , code = "inflect_problem" ) if not newpara and not ans : logging . warning ( "Could not inflect %s as %s" , table , paradigm ) raise errs . MflException ( "Table can not belong to paradigm %s" % ( paradigm ) , code = "inflect_problem" ) if ans : score , para , v = ans [ 0 ] add_word_to_paradigm ( lexicon , para , paradigms , identifier , pos , classes , v ) else : pex_table = helpers . tableize ( table , add_tags = False , identifier = identifier ) para = pex . learnparadigms ( [ pex_table ] ) [ 0 ] v = [ var for ix , var in para . var_insts [ 0 ] [ 1 : ] ] add_paradigm ( lexicon , paradigm , para , paradigms , identifier , pos , classes ) return identifier , wf_table , para , v , classes
Check that the given table and identifiers are ok, that the table matches
given paradigm and then add the table to the paradigm,
in karp and internally.
Check that the given table and identifiers are ok that the table matches given paradigm and then add the table to the paradigm in karp and internally .
"""
    Check that the given table and identifiers are ok, that the table matches
    given paradigm and then add the table to the paradigm,
    in karp and internally.
    Args:
        lexicon (str): the lexicon name
        table (str): the comma separated word forms, possibly with msds.
            "katt,katter|pl indef nom,katts"
        paradigm (str): a paradigm name
        paradigms (dict): a dictionary with all paradigms
            '{"lexname": {"nn": [], "vb": []}'
        identifier (str): the word's identifier
        baseform (str): the word's baseform
        pos (str): the tables word class
        classes (str): input classes 'bklass:2,cklass:3'
        ppriorv (int, optional): setting for pextract
        newword (bool, optional): is the word new? Defaults to false.
        newpara (bool, optional): is the paradigm new? Defaults to false.

    Returns:
       a tuple (identifier, wf_table, para, v, classes)
         where
         identifier (str): identifier of  the table
         wf_table (obj): the formatted inflection table
         para (obj): the paradigm object
         v (list): the variable instances
         classes (dict): the formatted classes of the word
    """ # check that the table's identier is unique # if it is new: make sure that it doesn't already exists. #    otherwise: construct a new one # if it is not new: make sure that it does exists. #    otherwise: fail # TODO remove after refactoring helpers # make sure that all required fields are present # parse input classes # parse the input table into pextract and karp format # prepare to try all paradigm # check that this is a new name # prepare to try only the given paradigm # found a match, add the word to this paradig # if no results are found (no matching paradigms), make a new paradigm # giving exactly the input forms
a tuple (identifier, wf_table, para, v, classes)
where
identifier (str): identifier of  the table
wf_table (obj): the formatted inflection table
para (obj): the paradigm object
v (list): the variable instances
classes (dict): the formatted classes of the word
a tuple ( identifier wf_table para v classes ) where identifier ( str ) : identifier of the table wf_table ( obj ) : the formatted inflection table para ( obj ) : the paradigm object v ( list ) : the variable instances classes ( dict ) : the formatted classes of the word
lexicon
the lexicon name
the lexicon name
table
the comma separated word forms, possibly with msds.
"katt,katter|pl indef nom,katts"
the comma separated word forms possibly with msds . " katt katter|pl indef nom katts "
paradigm
a paradigm name
a paradigm name
paradigms
a dictionary with all paradigms
'{"lexname": {"nn": [], "vb": []}'
a dictionary with all paradigms ' { " lexname " : { " nn " : [] " vb " : [] } '
identifier
the word's identifier
the word ' s identifier
baseform
the word's baseform
the word ' s baseform
pos
the tables word class
the tables word class
classes
input classes 'bklass:2,cklass:3'
input classes ' bklass : 2 cklass : 3 '
ppriorv
setting for pextract
setting for pextract
newword
None
newpara
None