Skip to content

medspacy.section_detection.util

This module will contain helper functions and classes for common clinical processing tasks which will be used in medspaCy's sectionizer.

is_end_line(idx, doc, pattern)

Check whether the token at idx occurs at the end of the line.

Parameters:

Name Type Description Default
idx int

The token index to check.

required
doc Doc

The doc to check in.

required
pattern Pattern

The newline pattern to check with.

required

Returns:

Type Description
bool

Whether the token occurs at the end of a line.

Source code in medspacy/section_detection/util.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def is_end_line(idx: int, doc: Doc, pattern: re.Pattern) -> bool:
    """
    Check whether the token at idx occurs at the end of the line.

    Args:
        idx: The token index to check.
        doc: The doc to check in.
        pattern: The newline pattern to check with.

    Returns:
        Whether the token occurs at the end of a line.
    """
    # If it's the end of the doc, return True
    if idx == len(doc) - 1:
        return True

    # Check if either the token has trailing newlines,
    # or if the next token is a newline
    text = doc[idx].text_with_ws
    if pattern.search(text) is not None:
        return True
    following_text = doc[idx + 1].text_with_ws
    return pattern.search(following_text) is not None

is_start_line(idx, doc, pattern)

Check whether the token at idx occurs at the start of the line.

Parameters:

Name Type Description Default
idx int

The token index to check.

required
doc Doc

The doc to check in.

required
pattern Pattern

The newline pattern to check with.

required

Returns:

Type Description
bool

Whether the token occurs at the start of a line.

Source code in medspacy/section_detection/util.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def is_start_line(idx: int, doc: Doc, pattern: re.Pattern) -> bool:
    """
    Check whether the token at idx occurs at the start of the line.

    Args:
        idx: The token index to check.
        doc: The doc to check in.
        pattern: The newline pattern to check with.

    Returns:
        Whether the token occurs at the start of a line.
    """
    # If it's the start of the doc, return True
    if idx == 0:
        return True
    # Otherwise, check if the preceding token ends with newlines
    preceding_text = doc[idx - 1].text_with_ws
    return pattern.search(preceding_text) is not None