Spaces:
Sleeping
Sleeping
File size: 9,491 Bytes
ac901c7 |
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
#
# Copyright (c) 2013-present, Anoop Kunchukuttan
# All rights reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
# Program for sentence splitting of Indian language input
#
# @author Anoop Kunchukuttan
#
"""
Sentence splitter for Indian languages. Contains a rule-based
sentence splitter that can understand common non-breaking phrases
in many Indian languages.
"""
import regex as re
from indicnlp.transliterate import unicode_transliterate
from indicnlp import langinfo
## for language which have danda as delimiter
## period is not part of the sentence delimiters
DELIM_PAT_DANDA = re.compile(r"[\?!\u0964\u0965]")
## for languages which don't have danda as delimiter
DELIM_PAT_NO_DANDA = re.compile(
r"[\.\?!\u0964\u0965\uAAF1\uAAF0\uABEB\uABEC\uABED\uABEE\uABEF\u1C7E\u1C7F]"
)
## pattern to check for presence of danda in text
CONTAINS_DANDA = re.compile(r"[\u0964\u0965]")
## pattern to check for presence of valid domain characters in text
CONTAINS_VALID_DOMAIN_CHAR = re.compile(r"^[a-zA-Z0-9_-]$")
## pattern to check for presence of multiple consecutive spaces in text
CONTAINS_MULTIPLE_SPACES = re.compile(" +")
def is_latin_or_numeric(character):
"""
Check if a character is a Latin character (uppercase or lowercase) or a number.
Parameters:
character (str): The character to be checked.
Returns:
bool: True if the character is a Latin character or a number, False otherwise.
"""
return re.match(CONTAINS_VALID_DOMAIN_CHAR, character) is not None
def is_acronym_abbvr(text, lang):
"""Is the text a non-breaking phrase
Args:
text (str): text to check for non-breaking phrase
lang (str): ISO 639-2 language code
Returns:
boolean: true if `text` is a non-breaking phrase
"""
ack_chars = {
## acronym for latin characters
"ए",
"ऎ",
"बी",
"बि",
"सी",
"सि",
"डी",
"डि",
"ई",
"इ",
"एफ",
"ऎफ",
"जी",
"जि",
"एच",
"ऎच",
"आई",
"आइ",
"ऐ",
"जे",
"जॆ",
"के",
"कॆ",
"एल",
"ऎल",
"एम",
"ऎम",
"एन",
"ऎन",
"ओ",
"ऒ",
"पी",
"पि",
"क्यू",
"क्यु",
"आर",
"एस",
"ऎस",
"टी",
"टि",
"यू",
"यु",
"वी",
"वि",
"व्ही",
"व्हि",
"डब्ल्यू",
"डब्ल्यु",
"एक्स",
"ऎक्स",
"वाय",
"जेड",
"ज़ेड",
## add halant to the previous English character mappings.
"एफ्",
"ऎफ्",
"एच्",
"ऎच्",
"एल्",
"ऎल्",
"एम्",
"ऎम्",
"एन्",
"ऎन्",
"आर्",
"एस्",
"ऎस्",
"एक्स्",
"ऎक्स्",
"वाय्",
"जेड्",
"ज़ेड्",
# Indic vowels
"ऄ",
"अ",
"आ",
"इ",
"ई",
"उ",
"ऊ",
"ऋ",
"ऌ",
"ऍ",
"ऎ",
"ए",
"ऐ",
"ऑ",
"ऒ",
"ओ",
"औ",
"ॠ",
"ॡ",
# Indic consonants
"क",
"ख",
"ग",
"घ",
"ङ",
"च",
"छ",
"ज",
"झ",
"ञ",
"ट",
"ठ",
"ड",
"ढ",
"ण",
"त",
"थ",
"द",
"ध",
"न",
"ऩ",
"प",
"फ",
"ब",
"भ",
"म",
"य",
"र",
"ऱ",
"ल",
"ळ",
"ऴ",
"व",
"श",
"ष",
"स",
"ह",
## abbreviation
"श्री",
"डॉ",
"कु",
"चि",
"सौ",
}
return (
unicode_transliterate.UnicodeIndicTransliterator.transliterate(text, lang, "hi")
in ack_chars
)
def sentence_split(text, lang, delim_pat="auto"): ## New signature
"""split the text into sentences
A rule-based sentence splitter for Indian languages written in
Brahmi-derived scripts. The text is split at sentence delimiter
boundaries. The delimiters can be configured by passing appropriate
parameters.
The sentence splitter can identify non-breaking phrases like
single letter, common abbreviations/honorofics for some Indian
languages.
Args:
text (str): text to split into sentence
lang (str): ISO 639-2 language code
delim_pat (str): regular expression to identify sentence delimiter characters. If set to 'auto', the delimiter pattern is chosen automatically based on the language and text.
Returns:
list: list of sentences identified from the input text
"""
if lang == "ur":
from indicnlp.urduhack.tokenization import sentence_tokenizer
if len(text.split()) < 2:
sentences = text.split()
else:
sentences = sentence_tokenizer(text)
return sentences
# print('Input: {}'.format(delim_pat))
if delim_pat == "auto":
if langinfo.is_danda_delim(lang):
# in modern texts it is possible that period is used as delimeter
# instead of DANDA. Hence, a check. Use danda delimiter pattern
# only if text contains at least one danda
if CONTAINS_DANDA.search(text) is None:
delim_pat = DELIM_PAT_NO_DANDA
# print('LANG has danda delim. TEXT_CONTAINS_DANDA: FALSE --> DELIM_PAT_NO_DANDA')
else:
delim_pat = DELIM_PAT_DANDA
# print('LANG has danda delim. TEXT_CONTAINS_DANDA: TRUE --> DELIM_PAT_DANDA')
else:
delim_pat = DELIM_PAT_NO_DANDA
# print('LANG has no danda delim --> DELIM_PAT_NO_DANDA')
## otherwise, assume the caller set the delimiter pattern
### Phase 1: break on sentence delimiters.
cand_sentences = []
begin = 0
text = text.strip()
for mo in delim_pat.finditer(text):
p1 = mo.start()
p2 = mo.end()
## NEW
if p1 > 0 and text[p1 - 1].isnumeric():
continue
## Prevents splitting on "." in URLs/emails in indic texts.
if lang != "en":
if is_latin_or_numeric(text[p1 - 1]):
if p1 + 1 < len(text) and is_latin_or_numeric(text[p1 + 1]):
continue
end = p1 + 1
s = text[begin:end].strip()
if len(s) > 0:
cand_sentences.append(s)
begin = p1 + 1
s = text[begin:].strip()
if len(s) > 0:
cand_sentences.append(s)
if not delim_pat.search("."):
## run phase 2 only if delimiter pattern contains period
# print('No need to run phase2')
return cand_sentences
# print(cand_sentences)
# print('====')
# return cand_sentences
### Phase 2: Address the fact that '.' may not always be a sentence delimiter
### Method: If there is a run of lines containing only a word (optionally) and '.',
### merge these lines as well one sentence preceding and succeeding this run of lines.
final_sentences = []
sen_buffer = ""
bad_state = False
for i, sentence in enumerate(cand_sentences):
words = sentence.split(" ")
# if len(words)<=2 and words[-1]=='.':
if len(words) == 1 and sentence[-1] == ".":
bad_state = True
sen_buffer = sen_buffer + " " + sentence
## NEW condition
elif sentence[-1] == "." and is_acronym_abbvr(words[-1][:-1], lang):
if len(sen_buffer) > 0 and not bad_state:
final_sentences.append(sen_buffer)
sen_buffer = sentence
else:
sen_buffer = sen_buffer + " " + sentence
bad_state = True
elif bad_state:
sen_buffer = sen_buffer + " " + sentence
if len(sen_buffer) > 0:
final_sentences.append(sen_buffer)
sen_buffer = ""
bad_state = False
else: ## good state
if len(sen_buffer) > 0:
final_sentences.append(sen_buffer)
sen_buffer = sentence
bad_state = False
if len(sen_buffer) > 0:
final_sentences.append(sen_buffer)
for i in range(0, len(final_sentences)):
final_sentences[i] = CONTAINS_MULTIPLE_SPACES.sub(
" ", final_sentences[i].strip()
)
return final_sentences
|