def equals_cross_reference(sense):
    xr = sense.equals_crossreference()
    target_senses, sense_count = tdb.cross_reference_target(lemma=xr.lemma,
        refentry=xr.refentry, refid=xr.refid, wordclass=sense.wordclass)

    if target_senses:
        hiscores = [t for t in target_senses
            if t.rating() == target_senses[0].rating()]
        hiscores.sort(key=lambda i: i.node_size(), reverse=True)
        if hiscores[0].thesclass is not None:
            match = hiscores[0].thesclass
            match.reason_text = 'Equivalent to %s' % hiscores[0].lemma
            match.reason_code = 'eqxr'
            return match
    return None
def cf_cross_reference(sense):
    """
    If the current sense has 'cf'-type cross-reference, the classification
    of the target sense is taken as a good guide to the classification
    of the current sense.

    If the target sense and the current sense are in the same wordclass and
    have the same end word-ending, we assume that the current sense should
    go in the target sense's class (similar to how 'equals'-type xrefs are
    treated.

    Otherwise, we put the current sense at the top of the equivalent
    wordclass-level branch.
    """
    xr = sense.cf_crossreference()
    # Nb don't specify a wordclass here, since the target wordclass may well
    #  be different from the current sense's wordclass
    target_senses, sense_count = tdb.cross_reference_target(lemma=xr.lemma,
        refentry=xr.refentry, refid=xr.refid)

    # Don't attempt if the target is too ambiguous (too many possible senses)
    if target_senses and sense_count <= 3:
        high_scores = [t for t in target_senses
            if t.rating() == target_senses[0].rating()]
        high_scores.sort(key=lambda i: i.node_size(), reverse=True)
        if high_scores[0].thesclass is not None:
            target = high_scores[0]
            if (target.wordclass == sense.wordclass and
                target.lemma[-2:] == sense.lemma[-2:]):
                match = target.thesclass
            elif target.wordclass == sense.wordclass:
                match = target.wordclass_parent()
            else:
                match = tdb.equivalent_class(target.thesclass, sense.wordclass)
            if match is not None:
                match.reason_text = 'Analogy with target of cf-type xref ("%s")' % target.lemma
                match.reason_code = 'cfxr'
            return match

    return None