File size: 3,770 Bytes
aa8760e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d707b13
 
 
 
 
 
 
aa8760e
d707b13
 
 
aa8760e
 
d707b13
aa8760e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from __future__ import annotations

import re
from typing import List


class ReferenceExtractor:

    """
    BNS Reference Extractor

    Detects:

        section 103

        Section 103

        sections 103 and 104

        sections 103, 104, 105

        section 103 of this Sanhita
    """

    # =====================================================
    # SECTION REFERENCES
    # =====================================================

    SECTION_RE = re.compile(
        r"\bsection[s]?\s+([0-9,\sand]+)",
        re.IGNORECASE
    )

    NUMBER_RE = re.compile(
        r"\d+"
    )

    # =====================================================
    # EXTRACT REFERENCES
    # =====================================================

    def extract_references(
        self,
        text: str,
        source_section: str
    ) -> List[dict]:

        references = []

        matches = (
            self.SECTION_RE.finditer(
                text
            )
        )

        for match in matches:

            raw = match.group(1)

            targets = (
                self.NUMBER_RE.findall(
                    raw
                )
            )

            for target in targets:

                references.append(
                    {
                        "source_section":
                            source_section,

                        "target_section":
                            target,

                        "reference_type":
                            "section"
                    }
                )

        return references

    # =====================================================
    # UNIQUE REFERENCES
    # =====================================================

    def unique_references(
        self,
        refs: List[dict]
    ) -> List[dict]:

        seen = set()

        unique = []

        for ref in refs:

            key = (
                ref["source_section"],
                ref["target_section"]
            )

            if key in seen:
                continue

            seen.add(key)

            unique.append(ref)

        return unique

    # =====================================================
    # VALIDATION
    # =====================================================

    def validate_references(
        self,
        refs: List[dict]
    ) -> List[str]:

        errors = []

        for ref in refs:

            # Skip malformed references
            if not isinstance(ref, dict):
                errors.append(
                    "Invalid reference object"
                )
                continue

            if (
                "source_section" not in ref
                or
                "target_section" not in ref
            ):
                errors.append(
                    "Reference missing source/target section"
                )

        return errors


# =========================================================
# TEST
# =========================================================

if __name__ == "__main__":

    sample = """
Whoever commits murder under section 103
shall be punished.

The provisions of sections 63 and 64
shall also apply.

Nothing in section 111 shall affect
the operation of this section.
"""

    extractor = (
        ReferenceExtractor()
    )

    refs = (
        extractor.extract_references(
            text=sample,
            source_section="104"
        )
    )

    refs = (
        extractor.unique_references(
            refs
        )
    )

    print(
        "References:",
        len(refs)
    )

    print()

    for r in refs:

        print(r)

    print()

    errors = (
        extractor.validate_references(
            refs
        )
    )

    print(
        "Errors:",
        len(errors)
    )