Detect acronyms by checking against a list of acronyms.

    Check a run of words represented by the range [s, i].
    Return last index of new word groups.

def _advanced_acronym_detection(s, i, words, acronyms):
    """
    Detect acronyms by checking against a list of acronyms.

    Check a run of words represented by the range [s, i].
    Return last index of new word groups.
    """
    # Combine each letter into single string.
    acstr = ''.join(words[s:i])

    # List of ranges representing found acronyms.
    range_list = []
    # Set of remaining letters.
    not_range = set(range(len(acstr)))

    # Search for each acronym in acstr.
    for acronym in acronyms:
        # TODO: Sanitize acronyms to include only letters.
        rac = regex.compile(unicode(acronym))

        # Loop until all instances of the acronym are found,
        # instead of just the first.
        n = 0
        while True:
            m = rac.search(acstr, n)
            if not m:
                break

            a, b = m.start(), m.end()
            n = b

            # Make sure found acronym doesn't overlap with others.
            ok = True
            for r in range_list:
                if a < r[1] and b > r[0]:
                    ok = False
                    break

            if ok:
                range_list.append((a, b))
                for j in xrange(a, b):
                    not_range.remove(j)

    # Add remaining letters as ranges.
    for nr in not_range:
        range_list.append((nr, nr + 1))

    # No ranges will overlap, so it's safe to sort by lower bound,
    # which sort() will do by default.
    range_list.sort()

    # Remove original letters in word list.
    for _ in xrange(s, i):
        del words[s]

    # Replace them with new word grouping.
    for j in xrange(len(range_list)):
        r = range_list[j]
        words.insert(s + j, acstr[r[0]:r[1]])

    return s + len(range_list) - 1