def disambiguate_language(text, assumed_languages, db):
    """
    Some Wiktionary links simply point to a term without specifying what
    language it's in. In that case, we have to guess.

    The possible languages are:

        - The language of the Wiktionary it's in
        - The language of the other term in the assertion

    We accept one of the possible languages if we have seen the term defined
    in that language in Wiktionary. Ideally, this leaves us with one
    possibility. But if we're left with 2 or 0, we default to the language
    of the other term.
    """
    if len(assumed_languages) == 1:
        return assumed_languages[0]
    ok_languages = []
    for language in assumed_languages:
        c = db.cursor()
        c.execute('SELECT * from titles where language=? and title=? limit 1',
            (language, text))
        if c.fetchone():
            ok_languages.append(language)
    if len(ok_languages) == 0:
        return None
    else:
        return ok_languages[0]
