import re


def update_idq(rts):
    """
    Function to update the identification qualifier field when appropriate.
    This function searches a taxonomic string for Open Nomenclature abbreviations (e.g. cf. aff. sp. indet.)
    If found the string is excised from the taxon field and the verbatim taxon field is copied to
    the identification qualifier field.
    Darwin Core stipulates that all notations about taxonomic uncertainty are limited to the
    identificationQualifier field.
    Example: if verbatim_genus == cf. Australopithecus,
    tgenus = Australopithecus, identification_qualifier = cf. Australopithecus
    :param rts: The raw taxanomic string, e.g. 'cf. major'
    :return: returns the cleaned taxon string and the identification qualifier string
    """
    idq = None
    ts = rts.strip()
    # regex matches all of the following:
    # test_list = ['? major', '?major', 'aff. major', 'Nov. sp.', 'indet', 'Indet.', 'cf.', 'Cf', 'cf. major', 'sp.',
    #             'Sp.', 'sp', 'Sp', ' sp.', 'major sp. nov.', 'sp. A', 'sp. A, sp. B', 'major']
    idqls = re.compile(r'^large sp.$|^small sp.$|[Ii]ndeterminate')
    ndetre = re.compile(r'[Nn]o [Dd]et[.]*[ her]*')
    noidre = re.compile(r'[nN][oO] [iI][dD]')
    idqre = re.compile(r'[sS]p[.]?.*|[cC]f[.]?|[iI]ndet[.]?|Nov[.]?.*|[Aa]ff[.]? |[?]|no det.')
    if ts:
        if idqls.search(ts):
            idq = ts
            ts = idqls.sub('', ts)
        elif noidre.search(ts):
            idq = ts
            ts = noidre.sub('', ts)
        elif ndetre.search(ts):
            idq = ts
            ts = ndetre.sub('', ts)
        elif idqre.search(ts):
            idq = ts
            ts = idqre.sub('', ts)
    return ts, idq