Upload regex_tokenlizer.py
Browse files- regex_tokenlizer.py +159 -0
regex_tokenlizer.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/python
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
|
4 |
+
import re
|
5 |
+
import sys
|
6 |
+
import unicodedata
|
7 |
+
|
8 |
+
|
9 |
+
def Text(text):
|
10 |
+
"""provide a wrapper for python string
|
11 |
+
map byte to str (python 3)
|
12 |
+
all string in utf-8 encoding
|
13 |
+
normalize string to NFC
|
14 |
+
"""
|
15 |
+
if not is_unicode(text):
|
16 |
+
text = text.decode("utf-8")
|
17 |
+
text = unicodedata.normalize("NFC", text)
|
18 |
+
return text
|
19 |
+
|
20 |
+
|
21 |
+
def is_unicode(text):
|
22 |
+
return type(text) == str
|
23 |
+
|
24 |
+
|
25 |
+
UPCASE_CHARACTERS = "QWERTYUIOPASDFGHJKLZXCVBNMÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸĐ"
|
26 |
+
LOWCASE_CHARACTERS = UPCASE_CHARACTERS.lower()
|
27 |
+
|
28 |
+
specials = [r"==>", r"->", r"\.\.\.", r">>", r"=\)\)"]
|
29 |
+
digit = r"\d+([\.,_]\d+)+"
|
30 |
+
email = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
|
31 |
+
|
32 |
+
|
33 |
+
# urls pattern from nltk
|
34 |
+
# https://www.nltk.org/_modules/nltk/tokenize/casual.html
|
35 |
+
# with Vu Anh's modified to match fpt protocol
|
36 |
+
urls = [
|
37 |
+
r"(ftp|http|https)?://(?:www\.)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!$&\'()*+,;=]+",
|
38 |
+
r"""(?:(https|http|ftp):(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:[a-z]{2,13}))
|
39 |
+
(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)(...))+
|
40 |
+
(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’])
|
41 |
+
|(?:(?<!@)[a-z0-9]+(?:[.\-][a-z0-9]+)*[.](?:[a-z]{2,13})\b/?(?!@))""",
|
42 |
+
r"(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}",
|
43 |
+
r"www\.[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+\.[a-zA-Z]{2,}",
|
44 |
+
]
|
45 |
+
|
46 |
+
datetime = [
|
47 |
+
# Thông tư 58/2020
|
48 |
+
r"\d{1,4}\/\d{4}",
|
49 |
+
# 01/1/2014 , 1/1
|
50 |
+
r"\d{1,2}\/\d{1,2}(\/\d+)?",
|
51 |
+
r"\d{1,2}-\d{1,2}(-\d+)?",
|
52 |
+
]
|
53 |
+
|
54 |
+
word = r"\w+"
|
55 |
+
non_word = r"[^\w\s]"
|
56 |
+
abbreviations = [
|
57 |
+
r"D.C",
|
58 |
+
r"Tp\.",
|
59 |
+
r"BS\.",
|
60 |
+
r"BS\.",
|
61 |
+
r"U\.S",
|
62 |
+
r"Mr\.",
|
63 |
+
r"Mrs\.",
|
64 |
+
r"Ms\.",
|
65 |
+
r"Dr\.",
|
66 |
+
r"ThS\.",
|
67 |
+
r"TP\.",
|
68 |
+
r"St\.",
|
69 |
+
]
|
70 |
+
|
71 |
+
special_names = [
|
72 |
+
# r"[A-Za-z]+s\'",
|
73 |
+
r"[A-Z]{1}\w+-[A-Z]{1}\w+", # al-Hajar, bin-Laden
|
74 |
+
r"[a-z]+-[A-Z]{1}\w+", # al-Hajar, bin-Laden
|
75 |
+
r"[A-Z]+[-]{1}[A-Za-z0-9]+", # M&A , T&H
|
76 |
+
r"[A-Z]+[&]{1}[A-Z]+", # M&A , T&H
|
77 |
+
r"[A-Z]{1,2}[-+]{1}", # K+ , nhóm máu A+ , A-, RH+
|
78 |
+
r"[A-Z]+\d+\.\d+", # A2.0 , SA2.0,
|
79 |
+
r"[A-Z]{1,2}[0-9]{1,4}[-+]{1}", # S21+
|
80 |
+
r"[A-Z0-9]{1,3}[.]{1}[A-Z0-9]{1,3}[.]{1}[A-Z0-9]{1,3}", # I.O.I, V.1.1
|
81 |
+
r"[A-Z]{1}[A-Za-z0-9]*[-]{1}\w+", # hyphen, F-16, Su-8, Apolo-2 ,H-Capital, A-Apolo Nano-Bio ...
|
82 |
+
r"\w+[\'’]{1}\w+", # H'Mông, Đắc'Rong, Ê'Đê ...
|
83 |
+
r"[fF]/\d+.\d+",
|
84 |
+
]
|
85 |
+
|
86 |
+
cus_hyphen = [
|
87 |
+
# biển số xe: 29H-116.87, 30E-180.54
|
88 |
+
r"[0-9A-Z]{3,5}[\-]{1}[.0-9]{3,6}",
|
89 |
+
r"d\'\w+",
|
90 |
+
]
|
91 |
+
|
92 |
+
units = [
|
93 |
+
r"(người)[/\\]{1}(km²)",
|
94 |
+
r"(lít)[/\\]{1}(m²|m2|ha|m³)",
|
95 |
+
r"(m|cm|km)[/\\]{1}(giờ|phút|giây|h)",
|
96 |
+
r"^\d+(.\d+)?(%|km|kg)",
|
97 |
+
r"[0-9]{1,3}[%]{1}", # 97%, 100%, 20%, 700%'
|
98 |
+
]
|
99 |
+
|
100 |
+
patterns = []
|
101 |
+
patterns.extend(special_names)
|
102 |
+
patterns.extend(units)
|
103 |
+
patterns.extend(cus_hyphen)
|
104 |
+
patterns.extend(datetime)
|
105 |
+
patterns.extend(abbreviations)
|
106 |
+
patterns.extend(specials)
|
107 |
+
patterns.extend(urls)
|
108 |
+
patterns.extend([email])
|
109 |
+
patterns.extend([digit])
|
110 |
+
patterns.extend([non_word])
|
111 |
+
patterns.extend([word])
|
112 |
+
|
113 |
+
|
114 |
+
patterns = "(" + "|".join(patterns) + ")"
|
115 |
+
patterns = re.compile(patterns, re.UNICODE)
|
116 |
+
|
117 |
+
|
118 |
+
def tokenize(text, format=None):
|
119 |
+
"""
|
120 |
+
tokenize text for word segmentation
|
121 |
+
|
122 |
+
:param text: raw text input
|
123 |
+
:return: tokenize text
|
124 |
+
"""
|
125 |
+
text = Text(text)
|
126 |
+
text = text.replace("\t", " ")
|
127 |
+
tokens = re.findall(patterns, text)
|
128 |
+
tokens = [token[0] for token in tokens]
|
129 |
+
if format == "text":
|
130 |
+
return " ".join(tokens)
|
131 |
+
else:
|
132 |
+
return tokens
|
133 |
+
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
DIGIT = re.compile(digit)
|
137 |
+
print(DIGIT.search(','))
|
138 |
+
exit()
|
139 |
+
|
140 |
+
s = ". ".join(
|
141 |
+
[
|
142 |
+
"Lần trước chúng tôi chứng kiến cảnh St. Petersburg bị tàn phá hoàn toàn ở khắp mọi nơi và giờ điều đó lại xảy ra một lần nữa",
|
143 |
+
"lãnh đạo Triều Tiên Kim Jong-un.",
|
144 |
+
"Vợ chồng 'sao đẹp nhất Philippines' bên con mùa Noel",
|
145 |
+
'"Về mặt y tế công cộng, chúng ta không còn e ngại gì đối với các ca tái dương tính", ông nói',
|
146 |
+
"Cựu thành viên I.O.I được khen, V.1.1, Ê'Đê, Women’s US Open, iPhone Xs 64GB 97% , Phiên bản S21+ 5G, Midi-Pyrénées, người/km²,km²,U.S, bin-Ladem ,Trung Lương-Mỹ Thuận, Dar al-Hajar vốn là nơi ở mùa hè của , ngày 30/6/2018, Zozibini, BS.Hê Thanh Nhã Yến, sân St James' Park, Thông tư 58/2020, 45.5%, 30km,50.2kg,Dar al-Hajar, VN-Index Honda WR-V 2020 giá từ 11.300 USD.vô địch La Liga. C.T Group lọt 3 mong HCM. M&A, Lux A2, Fadil, Lux A2.0 và Lux SA2.0, Brooklyn Beckham, K+, nhóm máu RH+, RH- ĐH KHXH&NV 120 km/h ti số 2-0 'la sao'. biển kiểm soát 30E-180.54 đi với tốc độ nhanh, lạng lách, làm xe tải biển 29H-116.87 lật ngang 2 phương thức tuyển sinh của Đại học Hùng Vương TP HCM. Trường 20/09/2019 http://google.com/?index=1",
|
147 |
+
"độc giả liên hệ qua email [email protected] hoặc số 024 7300 8899 (máy lẻ 4529) để được hỗ trợ.",
|
148 |
+
"sau khi một trực thăng chiến đấu Israel hạ sát thủ lĩnh lúc bấy giờ Abbas al-Musawi.",
|
149 |
+
"Sea-Group nè",
|
150 |
+
"Lần trước chúng tôi chứng kiến cảnh St. Petersburg bị tàn phá hoàn toàn ở khắp mọi nơi và giờ điều đó lại xảy ra một lần nữa",
|
151 |
+
"Ba nhà khoa học đoạt giải Nobel Hóa học nhờ nghiên cứu về protein. Ảnh: Phys.org",
|
152 |
+
"Ngân hạng Nhà nước công bố sáng nay ở mức 24.036 đồng , tăng 36 đồng so với hôm qua",
|
153 |
+
"ống f/1.8, ống f/1.9,ống f/2.0",
|
154 |
+
"Tại BatDongSan.com.vn",
|
155 |
+
"Ảnh minh họa: iStock.com",
|
156 |
+
]
|
157 |
+
)
|
158 |
+
# match = patterns.findall(s)
|
159 |
+
print(tokenize(s))
|