File size: 20,605 Bytes
e4b9a7b | 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | # -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2016-2025 PyThaiNLP Project
# SPDX-FileCopyrightText: Copyright 2020 Nakhun Chumpolsathien
# SPDX-License-Identifier: Apache-2.0
"""
The implementation of sentence segmentator from Nakhun Chumpolsathien, 2020
original codes are from: https://github.com/nakhunchumpolsathien/ThaiSum
Cite:
@mastersthesis{chumpolsathien_2020,
title={Using Knowledge Distillation from Keyword Extraction to Improve the Informativeness of Neural Cross-lingual Summarization},
author={Chumpolsathien, Nakhun},
year={2020},
school={Beijing Institute of Technology}
"""
import math
import operator
import re
from typing import List
from pythainlp.tokenize import word_tokenize
def list_to_string(list: List[str]) -> str:
string = "".join(list)
string = " ".join(string.split())
return string
def middle_cut(sentences: List[str]) -> List[str]:
new_text = ""
for sentence in sentences:
sentence_size = len(word_tokenize(sentence, keep_whitespace=False))
for k in range(0, len(sentence)):
if k == 0 or k + 1 >= len(sentence):
continue
if sentence[k].isdigit() and sentence[k - 1] == " ":
sentence = sentence[: k - 1] + sentence[k:]
if k + 2 <= len(sentence):
if sentence[k].isdigit() and sentence[k + 1] == " ":
sentence = sentence[: k + 1] + sentence[k + 2 :]
fixed_text_lenth = 20
if sentence_size > fixed_text_lenth:
partition = math.floor(sentence_size / fixed_text_lenth)
tokens = word_tokenize(sentence, keep_whitespace=True)
for i in range(0, partition):
middle_space = sentence_size / (partition + 1) * (i + 1)
white_space_index = []
white_space_diff = {}
for j in range(len(tokens)):
if tokens[j] == " ":
white_space_index.append(j)
for white_space in white_space_index:
white_space_diff.update(
{white_space: abs(white_space - middle_space)}
)
if len(white_space_diff) > 0:
min_diff = min(
white_space_diff.items(), key=operator.itemgetter(1)
)
tokens.pop(min_diff[0])
tokens.insert(min_diff[0], "<stop>")
new_text = new_text + list_to_string(tokens) + "<stop>"
else:
new_text = new_text + sentence + "<stop>"
sentences = new_text.split("<stop>")
sentences = [s.strip() for s in sentences]
if "" in sentences:
sentences.remove("")
if "nan" in sentences:
sentences.remove("nan")
sentences = list(filter(None, sentences))
return sentences
class ThaiSentenceSegmentor:
def split_into_sentences(
self, text: str, isMiddleCut: bool = False
) -> List[str]:
# Declare Variables
th_alphabets = "([ก-๙])"
th_conjunction = "(ทำให้|โดย|เพราะ|นอกจากนี้|แต่|กรณีที่|หลังจากนี้|ต่อมา|ภายหลัง|นับตั้งแต่|หลังจาก|ซึ่งเหตุการณ์|ผู้สื่อข่าวรายงานอีก|ส่วนที่|ส่วนสาเหตุ|ฉะนั้น|เพราะฉะนั้น|เพื่อ|เนื่องจาก|จากการสอบสวนทราบว่า|จากกรณี|จากนี้|อย่างไรก็ดี)"
th_cite = "(กล่าวว่า|เปิดเผยว่า|รายงานว่า|ให้การว่า|เผยว่า|บนทวิตเตอร์ว่า|แจ้งว่า|พลเมืองดีว่า|อ้างว่า)"
th_ka_krub = "(ครับ|ค่ะ)"
th_stop_after = "(หรือไม่|โดยเร็ว|แล้ว|อีกด้วย)"
th_stop_before = "(ล่าสุด|เบื้องต้น|ซึ่ง|ทั้งนี้|แม้ว่า|เมื่อ|แถมยัง|ตอนนั้น|จนเป็นเหตุให้|จากนั้น|อย่างไรก็ตาม|และก็|อย่างใดก็ตาม|เวลานี้|เช่น|กระทั่ง)"
degit = "([0-9])"
th_title = "(นาย|นาง|นางสาว|เด็กชาย|เด็กหญิง|น.ส.|ด.ช.|ด.ญ.)"
text = f" {text} "
text = text.replace("\n", " ")
text = text.replace("", "")
text = text.replace("โดยเร็ว", "<rth_Doeirew>")
text = text.replace("เพื่อน", "<rth_friend>")
text = text.replace("แต่ง", "<rth_but>")
text = text.replace("โดยสาร", "<rth_passenger>")
text = text.replace("แล้วแต่", "<rth_leawtea>")
text = text.replace("หรือเปล่า", "<rth_repraw>")
text = text.replace("หรือไม่", "<rth_remai>")
text = text.replace("จึงรุ่งเรืองกิจ", "<rth_tanatorn_lastname>")
text = text.replace("ตั้งแต่", "<rth_tangtea>")
text = text.replace("แต่ละ", "<rth_teala>")
text = text.replace("วิตแล้ว", "<rth_chiwitleaw>")
text = text.replace("โดยประ", "<rth_doipra>")
text = text.replace("แต่หลังจากนั้น", "<rth_tealangjaknan>")
text = text.replace("พรรคเพื่อ", "<for_party>")
text = text.replace("แต่เนื่อง", "<rth_teaneung>")
text = text.replace("เพื่อทำให้", "เพื่อ<rth_tamhai>")
text = text.replace("ทำเพื่อ", "ทำ<rth_for>")
text = text.replace("จึงทำให้", "จึง<tamhai>")
text = text.replace("มาโดยตลอด", "<madoitalod>")
text = text.replace("แต่อย่างใด", "<teayangdaikptam>")
text = text.replace("แต่หลังจาก", "แต่<langjak>")
text = text.replace("คงทำให้", "<rth_kongtamhai>")
text = text.replace("แต่ทั้งนี้", "แต่<tangni>")
text = text.replace("มีแต่", "มี<tea>")
text = text.replace("เหตุที่ทำให้", "<hedteetamhai>")
text = text.replace("โดยหลังจาก", "โดย<langjak>")
text = text.replace("ซึ่งหลังจาก", "ซึ่ง<langjak>")
text = text.replace("ตั้งโดย", "<rth_tangdoi>")
text = text.replace("โดยตรง", "<rth_doitong>")
text = text.replace("นั้นหรือ", "<rth_nanhlor>")
text = text.replace("ซึ่งต้องทำให้", "ซึ่งต้อง<tamhai>")
text = text.replace("ชื่อต่อมา", "ชื่อ<tomar>")
text = text.replace("โดยเร่งด่วน", "<doi>เร่งด่วน")
text = text.replace("ไม่ได้ทำให้", "ไม่ได้<tamhai>")
text = text.replace("จะทำให้", "จะ<tamhai>")
text = text.replace("จนทำให้", "จน<tamhai>")
text = text.replace("เว้นแต่", "เว้น<rth_tea>")
text = text.replace("ก็ทำให้", "ก็<tamhai>")
text = text.replace(" ณ ตอนนั้น", " ณ <tonnan>")
text = text.replace("บางส่วน", "บาง<rth_suan>")
text = text.replace("หรือแม้แต่", "หรือ<rth_meatea>")
text = text.replace("โดยทำให้", "โดย<tamhai>")
text = text.replace("หรือเพราะ", "หรือ<rth_orbecause>")
text = text.replace("มาแต่", "มา<rth_tea>")
text = text.replace("แต่ไม่ทำให้", "แต่<maitamhai>")
text = text.replace("ฉะนั้นเมื่อ", "ฉะนั้น<rth_moe>")
text = text.replace("เพราะฉะนั้น", "เพราะ<rth_chanan>")
text = text.replace("เพราะหลังจาก", "เพราะ<rth_langjak>")
text = text.replace("สามารถทำให้", "สามารถ<rth_tamhai>")
text = text.replace("อาจทำ", "อาจ<rth_tam>")
text = text.replace("จะทำ", "จะ<rth_tam>")
text = text.replace("และนอกจากนี้", "นอกจากนี้")
text = text.replace("อีกทั้งเพื่อ", "อีกทั้ง<rth_for>")
text = text.replace("ทั้งนี้เพื่อ", "ทั้งนี้<rth_for>")
text = text.replace("เวลาต่อมา", "เวลา<rth_toma>")
text = text.replace("อย่างไรก็ตาม", "อย่างไรก็ตาม")
text = text.replace(
"อย่างไรก็ตามหลังจาก", "<stop>อย่างไรก็ตาม<rth_langjak>"
)
text = text.replace("ซึ่งทำให้", "ซึ่ง<rth_tamhai>")
text = text.replace("โดยประมาท", "<doi>ประมาท")
text = text.replace("โดยธรรม", "<doi>ธรรม")
text = text.replace("โดยสัจจริง", "<doi>สัจจริง")
if "และ" in text:
tokens = word_tokenize(text.strip(), keep_whitespace=True)
and_position = -1
nearest_space_position = -1
last_position = len(tokens)
pop_split_position = []
split_position = []
for i in range(len(tokens)):
if tokens[i] == "และ":
and_position = i
if (
and_position != -1
and i > and_position
and tokens[i] == " "
and nearest_space_position == -1
):
if i - and_position != 1:
nearest_space_position = i
if and_position != -1 and last_position - and_position == 3:
split_position.append(last_position)
and_position = -1
nearest_space_position = -1
if nearest_space_position != -1:
if nearest_space_position - and_position < 5:
pop_split_position.append(nearest_space_position)
else:
split_position.append(and_position)
and_position = -1
nearest_space_position = -1
for pop in pop_split_position:
tokens.pop(pop)
tokens.insert(pop, "<stop>")
for split in split_position:
tokens.insert(split, "<stop>")
text = list_to_string(tokens)
if "หรือ" in text:
tokens = word_tokenize(text.strip(), keep_whitespace=True)
or_position = -1
nearest_space_position = -1
last_position = len(tokens)
pop_split_position = []
split_position = []
for i in range(len(tokens)):
if tokens[i] == "หรือ":
or_position = i
if (
or_position != -1
and i > or_position
and tokens[i] == " "
and nearest_space_position == -1
):
if i - or_position != 1:
nearest_space_position = i
if or_position != -1 and last_position - or_position == 3:
split_position.append(last_position)
or_position = -1
nearest_space_position = -1
if nearest_space_position != -1:
if nearest_space_position - or_position < 4:
pop_split_position.append(nearest_space_position)
else:
split_position.append(or_position)
or_position = -1
nearest_space_position = -1
for pop in pop_split_position:
tokens.pop(pop)
tokens.insert(pop, "<stop>")
for split in split_position:
tokens.insert(split, "<stop>")
text = list_to_string(tokens)
if "จึง" in text:
tokens = word_tokenize(text.strip(), keep_whitespace=True)
cung_position = -1
nearest_space_position = -1
pop_split_position = []
last_position = len(tokens)
split_position = []
for i in range(len(tokens)):
if tokens[i] == "จึง":
cung_position = i
if (
cung_position != -1
and tokens[i] == " "
and i > cung_position
and nearest_space_position == -1
):
if i - cung_position != 1:
nearest_space_position = i
if cung_position != -1 and last_position - cung_position == 2:
split_position.append(last_position)
cung_position = -1
nearest_space_position = -1
if nearest_space_position != -1:
if nearest_space_position - cung_position < 3:
pop_split_position.append(nearest_space_position)
else:
split_position.append(cung_position)
cung_position = -1
nearest_space_position = -1
for pop in pop_split_position:
tokens.pop(pop)
tokens.insert(pop, "<stop>")
for split in split_position:
tokens.insert(split, "<stop>")
text = list_to_string(tokens)
text = re.sub(" " + th_stop_before, "<stop>\\1", text)
text = re.sub(th_ka_krub, "\\1<stop>", text)
text = re.sub(th_conjunction, "<stop>\\1", text)
text = re.sub(th_cite, "\\1<stop>", text)
text = re.sub(" " + degit + "[.]" + th_title, "<stop>\\1.\\2", text)
text = re.sub(
" " + degit + degit + "[.]" + th_title, "<stop>\\1\\2.\\3", text
)
text = re.sub(th_alphabets + th_stop_after + " ", "\\1\\2<stop>", text)
if "”" in text:
text = text.replace(".”", "”.")
if '"' in text:
text = text.replace('."', '".')
if "!" in text:
text = text.replace('!"', '"!')
if "?" in text:
text = text.replace('?"', '"?')
text = text.replace("<rth_Doeirew>", "โดยเร็ว")
text = text.replace("<rth_friend>", "เพื่อน")
text = text.replace("<rth_but>", "แต่ง")
text = text.replace("<rth_passenger>", "โดยสาร")
text = text.replace("<rth_leawtea>", "แล้วแต่")
text = text.replace("<rth_repraw>", "หรือเปล่า")
text = text.replace("<rth_remai>", "หรือไม่")
text = text.replace("<rth_tanatorn_lastname>", "จึงรุ่งเรืองกิจ")
text = text.replace("<rth_tangtea>", "ตั้งแต่")
text = text.replace("<rth_teala>", "แต่ละ")
text = text.replace("<rth_chiwitleaw>", "วิตแล้ว")
text = text.replace("<rth_doipra>", "โดยประ")
text = text.replace("<rth_tealangjaknan>", "แต่หลังจากนั้น")
text = text.replace("<for_party>", "พรรคเพื่อ")
text = text.replace("<rth_teaneung>", "แต่เนื่อง")
text = text.replace("เพื่อ<rth_tamhai>", "เพื่อทำให้")
text = text.replace("ทำ<rth_for>", "ทำเพื่อ")
text = text.replace("จึง<tamhai>", "จึงทำให้")
text = text.replace("<madoitalod>", "มาโดยตลอด")
text = text.replace("แต่<langjak>", "แต่หลังจาก")
text = text.replace("แต่<tangni>", "แต่ทั้งนี้")
text = text.replace("มี<tea>", "มีแต่")
text = text.replace("<teayangdaikptam>", "แต่อย่างใด")
text = text.replace("<rth_kongtamhai>", "คงทำให้")
text = text.replace("<hedteetamhai>", "เหตุที่ทำให้")
text = text.replace("โดย<langjak>", "โดยหลังจาก")
text = text.replace("ซึ่ง<langjak>", "ซึ่งหลังจาก")
text = text.replace("<rth_tangdoi>", "ตั้งโดย")
text = text.replace("<rth_doitong>", "โดยตรง")
text = text.replace("<rth_nanhlor>", "นั้นหรือ")
text = text.replace("ซึ่งต้อง<tamhai>", "ซึ่งต้องทำให้")
text = text.replace("ชื่อ<tomar>", "ชื่อต่อมา")
text = text.replace("<doi>เร่งด่วน", "โดยเร่งด่วน")
text = text.replace("ไม่ได้<tamhai>", "ไม่ได้ทำให้")
text = text.replace("จะ<tamhai>", "จะทำให้")
text = text.replace("จน<tamhai>", "จนทำให้")
text = text.replace("เว้น<rth_tea>", "เว้นแต่")
text = text.replace("ก็<tamhai>", "ก็ทำให้")
text = text.replace(" ณ <tonnan>", " ณ ตอนนั้น")
text = text.replace("บาง<rth_suan>", "บางส่วน")
text = text.replace("หรือ<rth_meatea>", "หรือแม้แต่")
text = text.replace("โดย<tamhai>", "โดยทำให้")
text = text.replace("หรือ<rth_orbecause>", "หรือเพราะ")
text = text.replace("มา<rth_tea>", "มาแต่")
text = text.replace("แต่<maitamhai>", "แต่ไม่ทำให้")
text = text.replace("ฉะนั้น<rth_moe>", "ฉะนั้นเมื่อ")
text = text.replace("เพราะ<rth_chanan>", "เพราะฉะนั้น")
text = text.replace("เพราะ<rth_langjak>", "เพราะหลังจาก")
text = text.replace("สามารถ<rth_tamhai>", "สามารถทำให้")
text = text.replace("อาจ<rth_tam>", "อาจทำ")
text = text.replace("จะ<rth_tam>", "จะทำ")
text = text.replace("อีกทั้ง<rth_for>", "อีกทั้งเพื่อ")
text = text.replace("ทั้งนี้<rth_for>", "ทั้งนี้เพื่อ")
text = text.replace("เวลา<rth_toma>", "เวลาต่อมา")
text = text.replace(
"อย่างไรก็ตาม<rth_langjak>",
"อย่างไรก็ตามหลังจาก",
)
text = text.replace("ซึ่ง<rth_tamhai>", "ซึ่งทำให้")
text = text.replace("<doi>ประมาท", "โดยประมาท")
text = text.replace("<doi>ธรรม", "โดยธรรม")
text = text.replace("<doi>สัจจริง", "โดยสัจจริง")
text = text.replace("?", "?<stop>")
text = text.replace("!", "!<stop>")
text = text.replace("<prd>", ".")
sentences = text.split("<stop>")
sentences = [s.strip() for s in sentences]
if "" in sentences:
sentences.remove("")
if "nan" in sentences:
sentences.remove("nan")
sentences = list(filter(None, sentences))
if isMiddleCut:
return middle_cut(sentences)
else:
return sentences
|