-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
build_corpus.py
executable file
·67 lines (54 loc) · 2.21 KB
/
build_corpus.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
58
59
60
61
62
63
64
65
66
67
"""
Before running this code, make sure that you've downloaded Leipzig Chinese Corpus
(http://corpora2.informatik.uni-leipzig.de/downloads/zho_news_2007-2009_1M-text.tar.gz)
Extract and copy the `zho_news_2007-2009_1M-sentences.txt` to `data/` folder.
This code should generate a file which looks like this:
2[Tab]zhegeyemianxianzaiyijingzuofei...。[Tab]这__个_页_面___现___在__已_经___作__废__...。
In each line, the id, pinyin, and a chinese sentence are separated by a tab.
Note that _ [email protected]
"""
from __future__ import print_function
import codecs
import os
import regex # pip install regex
from xpinyin import Pinyin # pip install xpinyin
def align(sent):
'''
Args:
sent: A string. A sentence.
Returns:
A tuple of pinyin and chinese sentence.
'''
pinyin = Pinyin()
pnyns = pinyin.get_pinyin(sent, " ").split()
hanzis = []
for char, p in zip(sent.replace(" ", ""), pnyns):
hanzis.extend([char] + ["_"] * (len(p) - 1))
pnyns = "".join(pnyns)
hanzis = "".join(hanzis)
assert len(pnyns) == len(hanzis), "The hanzis and the pinyins must be the same in length."
return pnyns, hanzis
def clean(text):
if regex.search("[A-Za-z0-9]", text) is not None: # For simplicity, roman alphanumeric characters are removed.
return ""
text = regex.sub(u"[^ \p{Han}。,!?]", "", text)
return text
def build_corpus():
with codecs.open("data/zh.tsv", 'w', 'utf-8') as fout:
with codecs.open("data/zho_news_2007-2009_1M-sentences.txt", 'r', 'utf-8') as fin:
i = 1
while 1:
line = fin.readline()
if not line: break
try:
idx, sent = line.strip().split("\t")
sent = clean(sent)
if len(sent) > 0:
pnyns, hanzis = align(sent)
fout.write(u"{}\t{}\t{}\n".format(idx, pnyns, hanzis))
except:
continue # it's okay as we have a pretty big corpus!
if i % 10000 == 0: print(i, )
i += 1
if __name__ == "__main__":
build_corpus(); print("Done")