-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtility.py
More file actions
105 lines (76 loc) · 2.85 KB
/
Utility.py
File metadata and controls
105 lines (76 loc) · 2.85 KB
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
import re
import json
def deleteEmojify(text):
regrex_pattern = re.compile(pattern="["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U0001F1E0-\U0001F1FF"
"]+", flags=re.UNICODE)
return regrex_pattern.sub(r'', text)
def formatString(text):
text = deleteEmojify(text)
text = formatSentence(text)
return text
def removeNBSP(text):
return text.replace(" ", '')
def splipWithEndLine(text):
list_ = list()
leng = len(text) - 1
next_ = 0
start = 0
crop_at = 0
while start < leng:
if(next_ >= leng):
break
if(text[start] != '\n'):
start = start + 1
next_ = start
continue
if(text[next_] !='\n'):
line = text[crop_at: start + 1]
line = formatSentence(line)
#pdb.set_trace()
list_.append(line)
crop_at = next_
start = next_
continue
next_ = next_ + 1
if(crop_at != next_):
list_.append(formatSentence(text[crop_at: start + 1]))
newString = ""
for line in list_:
if line == "" or line == "":
continue
newString += "{}".format(line)
return newString
def formatSentence(text):
text = bytes(text, "utf-8").decode('utf-8', 'ignore')
text = text.replace('\a', " ")
text = text.replace('\r', " ")
text = re.sub('\xa0', " ", text)
text = re.sub(" ", " ", text)
text = re.sub("\u200b", " ", text)
text = re.sub("./", " ", text)
text = re.sub("•", "", text)
text = re.sub("\”", "", text)
text = re.sub("\t+", " ", text)
text = re.sub(' +', " ", text)
text = re.sub("[\n]+?[ ]+?[\n]+", "\n", text)
# trường hợp trên đáng ra phải bao quá hết cả ở dưới nhưng vì 1 lý do nào đó mà không có tác dụng
text = re.sub("[\n]+?[ ]+", "\n", text)
text = re.sub("[\n]+", "\n", text)
return text
def formatDocumentWithComma(text, comma):
text = re.sub("[\n]+?[ ]+?[\n]+", "\n", text)
# trường hợp trên đáng ra phải bao quá hết cả ở dưới nhưng vì 1 lý do nào đó mà không có tác dụng
text = re.sub("[\n]+?[ ]+", "\n", text)
text = splipWithEndLine(text)
text = text.replace("…", "{}{}{}".format(comma, comma, comma))
return text
def stringJsonToOject(str):
return json.loads(str)
def objectToJson(object):
jsonSTring = json.dumps(object, ensure_ascii = False).encode('utf8')
return jsonSTring.decode()