-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclean.py
executable file
·57 lines (48 loc) · 1.71 KB
/
clean.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Basic text cleaning for https://github.com/wannaphongcom/thaigov-corpus
- Remove leading and trailing spaces (str.strip())
- Remove blank lines
== Output Format ==
Title
(one blank line)
Article body, running with no blank line
(one blank line)
Source : url
(one blank line)
"""
from __future__ import unicode_literals
import codecs
import argparse
import re
def main():
parser = argparse.ArgumentParser(
description='Basic text cleaning. Remove leading and trailing spaces. Remove blank lines.')
parser.add_argument('filename', type=str, nargs='+',
help='name of a file to clean')
args = vars(parser.parse_args())
filenames = args['filename']
for filename in filenames:
text = ''
page_view_line = 0
with codecs.open(filename, 'r', encoding='utf-8') as f:
for n, line in enumerate(f):
line = line.strip()
if n == 0: # title line
text = line + '\n'
else:
if line:
if re.match(r'^[\d,]+$', line): # skip page view count
page_view_line = n
continue
if line == 'พิมพ์' and page_view_line and page_view_line < n: # skip 'print'
continue
if re.match(r'^ที่มา : http', line):
text = text + '\n\n' + line + '\n'
else:
text = text + '\n' + line
with codecs.open(filename, 'w', encoding='utf-8') as f:
f.write(text)
if __name__ == "__main__":
main()