Machine-Learning-for-Medical-Language / ctakes-client-py

Apache License 2.0
4 stars 1 forks source link

Feature request: helper methods for testing Span overlaps #55

Open comorbidity opened 1 year ago

comorbidity commented 1 year ago

def overlaps(span1: Span, span2: Span) -> set: """ :param span1: 1st text Span :param span2: 2nd text Span :return: set of CHAR positions (convertible to range or Span) """ range1 = range(span1.begin, span1.end) range2 = range(span2.begin, span2.end) return set(range1).intersection(set(range2))

def overlaps(span1: Span, span2: Span, min_length=2, max_length=20) -> bool: """ True/False text overlap exists between two spans of 'highlighted' text.

:param span1: 1st text Span
:param span2: 2nd text Span
:param min_length: MIN length of comparison, default 2 chars
:param max_length: MAX length of comparison, default 20 chars (or equals)
:return: true/false the two spans overlap
"""
shared = intersect(span1, span2)
if len(shared) == len(range(span1.begin, span1.end)):
    return True
elif (len(shared) >= min_length) and (len(shared) <= max_length):
    return True
else:
    return False