Upload text_split.py
Browse files- text_split.py +134 -0
text_split.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import logging
|
3 |
+
from typing import Set, Tuple, List
|
4 |
+
|
5 |
+
SENTENCE_SPLITTER = re.compile(r'[^\.?!…]+[\.?!…]*["»“]*')
|
6 |
+
|
7 |
+
LAST_WORD_PATTERN = re.compile(r'(?:\b|\d)([a-zа-я]+)\.$', re.IGNORECASE)
|
8 |
+
FIRST_WORD_PATTERN = re.compile(r'^\W*(\w+)')
|
9 |
+
ENDS_WITH_ONE_LETTER_LAT_AND_DOT_PATTERN = re.compile(r'(\d|\W|\b)([a-zA-Z])\.$')
|
10 |
+
HAS_DOT_INSIDE_PATTERN = re.compile(r'[\w]+\.[\w]+\.$', re.IGNORECASE)
|
11 |
+
INITIALS_PATTERN = re.compile(r'(\W|\b)([A-ZА-Я]{1})\.$')
|
12 |
+
ONLY_RUS_CONSONANTS_PATTERN = re.compile(r'^[бвгджзйклмнпрстфхцчшщ]{1,4}$', re.IGNORECASE)
|
13 |
+
STARTS_WITH_EMPTYNESS_PATTERN = re.compile(r'^\s+')
|
14 |
+
ENDS_WITH_EMOTION_PATTERN = re.compile(r'[!?…]|\.{2,}\s?[)"«»,“]?$')
|
15 |
+
STARTS_WITH_LOWER_PATTERN = re.compile(r'^\s*[–-—-("«]?\s*[a-zа-я]')
|
16 |
+
STARTS_WITH_DIGIT_PATTERN = re.compile(r'^\s*\d')
|
17 |
+
NUMERATION_PATTERN = re.compile(r'^\W*[IVXMCL\d]+\.$')
|
18 |
+
PAIRED_SHORTENING_IN_THE_END_PATTERN = re.compile(r'\b(\w+)\. (\w+)\.\W*$')
|
19 |
+
|
20 |
+
JOIN = 0
|
21 |
+
MAYBE = 1
|
22 |
+
SPLIT = 2
|
23 |
+
|
24 |
+
JOINING_SHORTENINGS = {
|
25 |
+
'mr', 'mrs', 'ms', 'dr', 'vs', 'англ', 'итал', 'греч', 'евр', 'араб', 'яп', 'слав', 'кит',
|
26 |
+
'тел', 'св', 'ул', 'устар', 'им', 'г', 'см', 'д', 'стр', 'корп', 'пл', 'пер', 'сокр', 'рис'
|
27 |
+
}
|
28 |
+
|
29 |
+
SHORTENINGS = {
|
30 |
+
'co', 'corp', 'inc', 'авт', 'адм', 'барр', 'внутр', 'га', 'дифф', 'дол', 'долл', 'зав', 'зам', 'искл',
|
31 |
+
'коп', 'корп', 'куб', 'лат', 'мин', 'о', 'обл', 'обр', 'прим', 'проц', 'р', 'ред', 'руб', 'рус', 'русск',
|
32 |
+
'сан', 'сек', 'тыс', 'эт', 'яз', 'гос', 'мн', 'жен', 'муж', 'накл', 'повел', 'букв', 'шутл', 'ед'
|
33 |
+
}
|
34 |
+
|
35 |
+
PAIRED_SHORTENINGS = {('и', 'о'), ('т', 'е'), ('т', 'п'), ('у', 'е'), ('н', 'э')}
|
36 |
+
|
37 |
+
|
38 |
+
def split_sentences(text: str) -> List[str]:
|
39 |
+
return [x.strip() for x in SENTENCE_SPLITTER.findall(text)]
|
40 |
+
|
41 |
+
|
42 |
+
def is_sentence_end(left: str, right: str,
|
43 |
+
shortenings: Set[str],
|
44 |
+
joining_shortenings: Set[str],
|
45 |
+
paired_shortenings: Set[Tuple[str, str]]) -> int:
|
46 |
+
if not STARTS_WITH_EMPTYNESS_PATTERN.match(right):
|
47 |
+
return JOIN
|
48 |
+
|
49 |
+
if HAS_DOT_INSIDE_PATTERN.search(left):
|
50 |
+
return JOIN
|
51 |
+
|
52 |
+
left_last_word = LAST_WORD_PATTERN.search(left)
|
53 |
+
lw = ' '
|
54 |
+
if left_last_word:
|
55 |
+
lw = left_last_word.group(1)
|
56 |
+
|
57 |
+
if lw.lower() in joining_shortenings:
|
58 |
+
return JOIN
|
59 |
+
|
60 |
+
if ONLY_RUS_CONSONANTS_PATTERN.search(lw) and lw[-1].islower():
|
61 |
+
return MAYBE
|
62 |
+
|
63 |
+
pse = PAIRED_SHORTENING_IN_THE_END_PATTERN.search(left)
|
64 |
+
if pse:
|
65 |
+
s1, s2 = pse.groups()
|
66 |
+
if (s1, s2) in paired_shortenings:
|
67 |
+
return MAYBE
|
68 |
+
|
69 |
+
right_first_word = FIRST_WORD_PATTERN.match(right)
|
70 |
+
if right_first_word:
|
71 |
+
rw = right_first_word.group(1)
|
72 |
+
if (lw, rw) in paired_shortenings:
|
73 |
+
return MAYBE
|
74 |
+
|
75 |
+
if ENDS_WITH_EMOTION_PATTERN.search(left) and STARTS_WITH_LOWER_PATTERN.match(right):
|
76 |
+
return JOIN
|
77 |
+
|
78 |
+
initials = INITIALS_PATTERN.search(left)
|
79 |
+
if initials:
|
80 |
+
border, _ = initials.groups()
|
81 |
+
if (border or ' ') not in "°'":
|
82 |
+
return JOIN
|
83 |
+
|
84 |
+
if lw.lower() in shortenings:
|
85 |
+
return MAYBE
|
86 |
+
|
87 |
+
last_letter = ENDS_WITH_ONE_LETTER_LAT_AND_DOT_PATTERN.search(left)
|
88 |
+
if last_letter:
|
89 |
+
border, _ = last_letter.groups()
|
90 |
+
if (border or ' ') not in "°'":
|
91 |
+
return MAYBE
|
92 |
+
if NUMERATION_PATTERN.match(left):
|
93 |
+
return JOIN
|
94 |
+
return SPLIT
|
95 |
+
|
96 |
+
|
97 |
+
def split_by_sentences(text: str,
|
98 |
+
shortenings: Set[str] = SHORTENINGS,
|
99 |
+
joining_shortenings: Set[str] = JOINING_SHORTENINGS,
|
100 |
+
paired_shortenings: Set[Tuple[str, str]] = PAIRED_SHORTENINGS) -> List[str]:
|
101 |
+
sentences = []
|
102 |
+
sents = split_sentences(text)
|
103 |
+
si = 0
|
104 |
+
processed_index = 0
|
105 |
+
sent_start = 0
|
106 |
+
while si < len(sents):
|
107 |
+
s = sents[si]
|
108 |
+
span_start = text[processed_index:].index(s) + processed_index
|
109 |
+
span_end = span_start + len(s)
|
110 |
+
processed_index += len(s)
|
111 |
+
|
112 |
+
si += 1
|
113 |
+
|
114 |
+
send = is_sentence_end(text[sent_start: span_end], text[span_end:],
|
115 |
+
shortenings, joining_shortenings, paired_shortenings)
|
116 |
+
if send == JOIN:
|
117 |
+
continue
|
118 |
+
|
119 |
+
if send == MAYBE:
|
120 |
+
if STARTS_WITH_LOWER_PATTERN.match(text[span_end:]):
|
121 |
+
continue
|
122 |
+
if STARTS_WITH_DIGIT_PATTERN.match(text[span_end:]):
|
123 |
+
continue
|
124 |
+
|
125 |
+
if not text[sent_start: span_end].strip():
|
126 |
+
print(text)
|
127 |
+
sentences.append(text[sent_start: span_end].strip())
|
128 |
+
sent_start = span_end
|
129 |
+
processed_index = span_end
|
130 |
+
|
131 |
+
if sent_start != len(text):
|
132 |
+
if text[sent_start:].strip():
|
133 |
+
sentences.append(text[sent_start:].strip())
|
134 |
+
return sentences
|