Datasets:
File size: 7,427 Bytes
06124c0 |
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 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from pathlib import Path
import re
import shutil
import string
import pandas as pd
from project_settings import project_path
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--data_dir",
default=(project_path / "data2").as_posix(),
type=str
)
parser.add_argument(
"--output_file",
default="metadata.csv",
type=str
)
args = parser.parse_args()
return args
class SentenceType(object):
def __init__(self):
self.advertise_keywords = [
"请访问",
]
self.url_keywords = [
"www.",
]
self.pattern_predictor_map = {
"www.(?:.*?).(net|com|cn)": "url"
}
def contains_advertise(self, text: str):
for k in self.advertise_keywords:
if text.__contains__(k):
return "advertise"
return None
def contains_url(self, text: str):
for k in self.url_keywords:
if text.__contains__(k):
return "url"
return None
def pattern_predict(self, text: str):
for k, v in self.pattern_predictor_map.items():
if re.search(k, text, flags=re.IGNORECASE) is not None:
return v
return None
def predict(self, text: str):
result = self.pattern_predict(text)
if result is not None:
return result
result = self.contains_advertise(text)
if result is not None:
return result
return None
class ContentPreprocess(object):
def __init__(self):
self.s_type = SentenceType()
self.punctuation_map = {
",": ",",
"。": ".",
"、": ",",
"?": "?",
":": ":",
"{": "{",
"}": "}",
"(": "(",
")": ")",
"【": "(",
"】": ")",
"「": "\"",
"」": "\"",
"『": "\"",
"』": "\"",
"《": "(",
"》": ")",
"”": "\"",
"“": "\"",
"‘": "\'",
"’": "\'",
"=": "",
"": "",
" ": "",
" ": "",
"\t": "",
"\n": "",
"\r": "",
"\v": "",
"\f": "",
}
self.string_map = {
"^_^": "",
"◆": "",
"☆": "",
"...": "…",
"()": "",
"{}": "",
"『』": "",
"《》": "",
"<": "<",
">": ">",
"&": " ",
" ": " ",
"quot;": "\"",
"nbsp;": " ",
"amp;": "",
"✘": "✘",
"龙腾小说网ltxs520.com": "",
"龙腾小说吧www.ltxsba.net": "",
"龙腾小说网 ltxs520.com": "",
"龙腾小说 ltxs520.com": "",
"龙腾小说ltxs520.com": "",
"(..)免费": "",
"\"\"": "\"\n\"",
}
self.pattern_map = {
r"</(?:.*?)>": "",
r"⌒{4,}": "###",
r"#{4,}": "###"
}
self.queue = list()
self.queue_length = 0
self.max_seq_length = 128
self.output_list = list()
def char_b2q(self, uchar):
"""单个字符 半角转全角"""
inside_code = ord(uchar)
if inside_code < 0x0020 or inside_code > 0x7e:
return uchar
if inside_code == 0x0020:
inside_code = 0x3000
else:
inside_code += 0xfee0
return chr(inside_code)
def char_q2b(self, uchar):
"""单个字符 全角转半角"""
inside_code = ord(uchar)
if inside_code == 0x3000:
inside_code = 0x0020
else:
inside_code -= 0xfee0
if inside_code < 0x0020 or inside_code > 0x7e:
return uchar
return chr(inside_code)
def q2b(self, text: str):
"""全角转半角"""
result = ""
for c in text:
c = self.char_q2b(c)
result += c
return result
def remove_space(self, text: str):
text = text.replace(" ", "")
return text
def replace_punctuation(self, text: str):
text_ = text
for k, v in self.punctuation_map.items():
text_ = text_.replace(k, v)
if text_ != text:
text_ = self.replace_punctuation(text_)
return text_
def replace_string(self, text: str):
for k, v in self.string_map.items():
text = text.replace(k, v)
return text
def replace_by_pattern(self, text: str):
for k, v in self.pattern_map.items():
text = re.sub(k, v, text)
return text
def process(self, text: str):
text = self.q2b(text)
text = self.replace_punctuation(text)
text = self.replace_by_pattern(text)
text = self.replace_string(text)
self.in_queue(text)
return text
def in_queue(self, text: str):
if self.s_type.predict(text) is not None:
return
if len(text) == 0:
return
text_splits = text.split(".")
num_splits = len(text_splits)
for idx, text_split in enumerate(text_splits):
char_num = len(text_split)
if self.queue_length + char_num > self.max_seq_length:
text_ = "".join(self.queue)
self.output_list.append(text_)
self.queue = list()
self.queue_length = 0
if idx == 0 and len(self.queue) != 0:
self.queue.append("\n")
if num_splits == 1:
self.queue.append(text_split)
self.queue_length += char_num
else:
if idx + 1 != num_splits:
self.queue.append("{}.".format(text_split))
self.queue_length += char_num
else:
self.queue.append(text_split)
self.queue_length += char_num
return
def main():
args = get_args()
data_dir = Path(args.data_dir)
filename_list = data_dir.glob("*/*.txt")
s_type = SentenceType()
c_preprocess = ContentPreprocess()
result = list()
for filename in filename_list:
name = filename.stem
splits = name.split("_")
idx = splits[-1]
novel_name = "_".join(splits[:-1])
to_filename = Path(filename.as_posix().replace("/data2/", "/data3/"))
to_filename.parent.mkdir(parents=True, exist_ok=True)
with open(filename.as_posix(), "r", encoding="utf-8") as fin, \
open(to_filename.as_posix(), "w", encoding="utf-8") as fout:
for row in fin:
row = str(row).strip()
c_preprocess.process(row)
for row_ in c_preprocess.output_list:
# print(row_)
# print(len(row_))
# print("-" * 150)
# result.append(row_)
fout.write("{}\n".format(row_))
c_preprocess.output_list = list()
return
if __name__ == '__main__':
main()
|