|
| 1 | +# This file contains code adapted from the WordTokenizers.jl |
| 2 | +# https://github.com/JuliaText/WordTokenizers.jl project. |
| 3 | +# It is subject to the license terms in the Apache License file |
| 4 | +# found in the top-level directory of this distribution. |
| 5 | +# This file has been modified by Guardrails AI on September 27 2024. |
| 6 | + |
| 7 | +import re |
| 8 | + |
| 9 | + |
| 10 | +def replace_til_no_change(input_text, pattern, replacement): |
| 11 | + while True: |
| 12 | + new_text = re.sub(pattern, replacement, input_text) |
| 13 | + if new_text == input_text: |
| 14 | + break |
| 15 | + input_text = new_text |
| 16 | + return input_text |
| 17 | + |
| 18 | + |
| 19 | +def postproc_splits(sentences, separator): |
| 20 | + """ |
| 21 | + Applies heuristic rules to repair sentence splitting errors. |
| 22 | + Developed for use as postprocessing for the GENIA sentence |
| 23 | + splitter on PubMed abstracts, with minor tweaks for |
| 24 | + full-text documents. |
| 25 | +
|
| 26 | + `sentences` should be a string, with line breaks on sentence boundaries. |
| 27 | + Returns a similar string, but more correct. |
| 28 | +
|
| 29 | + Based on |
| 30 | + https://github.com/ninjin/geniass/blob/master/geniass-postproc.pl |
| 31 | + Which is |
| 32 | + (c) 2010 Sampo Pyysalo. No rights reserved, i.e. do whatever you like with this. |
| 33 | + Which draws in part on heuristics included in Yoshimasa Tsuruoka's |
| 34 | + medss.pl script. |
| 35 | + """ |
| 36 | + |
| 37 | + # Remove Windows line endings |
| 38 | + sentences = sentences.replace("\r", "") |
| 39 | + |
| 40 | + # Breaks sometimes missing after "?", "safe" cases |
| 41 | + sentences = re.sub( |
| 42 | + r"\b([a-z]+\?)\s+([A-Z][a-z]+)\b", rf"\1{separator}\2", sentences |
| 43 | + ) |
| 44 | + # Breaks sometimes missing after ".", "safe" cases |
| 45 | + sentences = re.sub( |
| 46 | + r"\b([a-z]+ \.)\s+([A-Z][a-z]+)\b", rf"\1{separator}\2", sentences |
| 47 | + ) |
| 48 | + |
| 49 | + # No breaks producing lines only containing sentence-ending punctuation |
| 50 | + sentences = re.sub(rf"{separator}([.!?]+){separator}", r"\1" + separator, sentences) |
| 51 | + |
| 52 | + # No breaks inside parentheses/brackets |
| 53 | + sentences = replace_til_no_change( |
| 54 | + sentences, |
| 55 | + r"\[([^\[\]\(\)]*)" + re.escape(separator) + r"([^\[\]\(\)]*)\]", |
| 56 | + r"[\1 \2]", |
| 57 | + ) |
| 58 | + sentences = replace_til_no_change( |
| 59 | + sentences, |
| 60 | + r"\(([^\[\]\(\)]*)" + re.escape(separator) + r"([^\[\]\(\)]*)\)", |
| 61 | + r"(\1 \2)", |
| 62 | + ) |
| 63 | + # Standard mismatched with possible intervening |
| 64 | + sentences = replace_til_no_change( |
| 65 | + sentences, |
| 66 | + r"\[([^\[\]]{0,250})" + re.escape(separator) + r"([^\[\]]{0,250})\]", |
| 67 | + r"[\1 \2]", |
| 68 | + ) |
| 69 | + sentences = replace_til_no_change( |
| 70 | + sentences, |
| 71 | + r"\(([^\(\)]{0,250})" + re.escape(separator) + r"([^\(\)]{0,250})\)", |
| 72 | + r"(\1 \2)", |
| 73 | + ) |
| 74 | + |
| 75 | + # Line breaks within quotes |
| 76 | + sentences = replace_til_no_change( |
| 77 | + sentences, |
| 78 | + r'"([^"\n]{0,250})' + re.escape(separator) + r'([^"\n]{0,250})"', |
| 79 | + r'"\1 \2"', |
| 80 | + ) |
| 81 | + sentences = replace_til_no_change( |
| 82 | + sentences, |
| 83 | + r"'([^'\n]{0,250})" + re.escape(separator) + r"([^'\n]{0,250})'", |
| 84 | + r"'\1 \2'", |
| 85 | + ) |
| 86 | + |
| 87 | + # Nesting to depth one |
| 88 | + sentences = replace_til_no_change( |
| 89 | + sentences, |
| 90 | + r"\[((?:[^\[\]]|\[[^\[\]]*\]){0,250})" |
| 91 | + + re.escape(separator) |
| 92 | + + r"((?:[^\[\]]|\[[^\[\]]*\]){0,250})\]", |
| 93 | + r"[\1 \2]", |
| 94 | + ) |
| 95 | + sentences = replace_til_no_change( |
| 96 | + sentences, |
| 97 | + r"\(((?:[^\(\)]|\([^\(\)]*\)){0,250})" |
| 98 | + + re.escape(separator) |
| 99 | + + r"((?:[^\(\)]|\([^\(\)]*\)){0,250})\)", |
| 100 | + r"(\1 \2)", |
| 101 | + ) |
| 102 | + |
| 103 | + # No break after periods followed by a non-uppercase "normal word" |
| 104 | + sentences = re.sub(rf"\.{separator}([a-z]{{3,}}[a-z-]*[ .:,])", r". \1", sentences) |
| 105 | + |
| 106 | + # No break after a single letter other than I |
| 107 | + sentences = re.sub(rf"(\b[A-HJ-Z]\.){separator}", r"\1 ", sentences) |
| 108 | + |
| 109 | + # No break before coordinating conjunctions (CC) |
| 110 | + coordinating_conjunctions = ["and", "or", "but", "nor", "yet"] |
| 111 | + for cc in coordinating_conjunctions: |
| 112 | + sentences = re.sub(rf"{separator}({cc}\s)", r" \1", sentences) |
| 113 | + |
| 114 | + # No break before prepositions (IN) |
| 115 | + prepositions = [ |
| 116 | + "of", |
| 117 | + "in", |
| 118 | + "by", |
| 119 | + "as", |
| 120 | + "on", |
| 121 | + "at", |
| 122 | + "to", |
| 123 | + "via", |
| 124 | + "for", |
| 125 | + "with", |
| 126 | + "that", |
| 127 | + "than", |
| 128 | + "from", |
| 129 | + "into", |
| 130 | + "upon", |
| 131 | + "after", |
| 132 | + "while", |
| 133 | + "during", |
| 134 | + "within", |
| 135 | + "through", |
| 136 | + "between", |
| 137 | + "whereas", |
| 138 | + "whether", |
| 139 | + ] |
| 140 | + for prep in prepositions: |
| 141 | + sentences = re.sub(rf"{separator}({prep}\s)", r" \1", sentences) |
| 142 | + |
| 143 | + # No sentence breaks in the middle of specific abbreviations |
| 144 | + sentences = re.sub(rf"(\be\.){separator}(g\.)", r"\1 \2", sentences) |
| 145 | + sentences = re.sub(rf"(\bi\.){separator}(e\.)", r"\1 \2", sentences) |
| 146 | + sentences = re.sub(rf"(\bi\.){separator}(v\.)", r"\1 \2", sentences) |
| 147 | + |
| 148 | + # No sentence break after specific abbreviations |
| 149 | + abbreviations = [ |
| 150 | + r"e\.?g\.", |
| 151 | + r"i\.?e\.", |
| 152 | + r"i\.?v\.", |
| 153 | + r"vs\.", |
| 154 | + r"cf\.", |
| 155 | + r"Dr\.", |
| 156 | + r"Mr\.", |
| 157 | + r"Ms\.", |
| 158 | + r"Mrs\.", |
| 159 | + r"Prof\.", |
| 160 | + r"Ph\.?D\.", |
| 161 | + r"Jr\.", |
| 162 | + r"St\.", |
| 163 | + r"Mt\.", |
| 164 | + r"etc\.", |
| 165 | + r"Fig\.", |
| 166 | + r"vol\.", |
| 167 | + r"Vols\.", |
| 168 | + r"No\.", |
| 169 | + r"Nos\.", |
| 170 | + r"et\.", |
| 171 | + r"al\.", |
| 172 | + r"Inc\.", |
| 173 | + r"Ltd\.", |
| 174 | + r"Co\.", |
| 175 | + r"Corp\.", |
| 176 | + r"Dept\.", |
| 177 | + r"est\.", |
| 178 | + r"Asst\.", |
| 179 | + r"approx\.", |
| 180 | + r"dr\.", |
| 181 | + r"fig\.", |
| 182 | + r"mr\.", |
| 183 | + r"mrs\.", |
| 184 | + r"ms\.", |
| 185 | + r"prof\.", |
| 186 | + r"rep\.", |
| 187 | + r"jr\.", |
| 188 | + r"sen\.", |
| 189 | + r"st\.", |
| 190 | + r"vs\.", |
| 191 | + r"i\.?e\.", |
| 192 | + ] |
| 193 | + for abbr in abbreviations: |
| 194 | + sentences = re.sub( |
| 195 | + rf"(\b{abbr}){separator}", r"\1", sentences, flags=re.IGNORECASE |
| 196 | + ) |
| 197 | + |
| 198 | + return sentences |
| 199 | + |
| 200 | + |
| 201 | +def split_sentences(text, separator="abcdsentenceseperatordcba"): |
| 202 | + # Use the separator in the regex |
| 203 | + text = re.sub(r"([?!.])(?=\s|$)", rf"\1{separator}", text) |
| 204 | + text = postproc_splits(text, separator) |
| 205 | + return re.split(rf"\n?{separator} ?\n?", text) |
0 commit comments