-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess.py
More file actions
204 lines (160 loc) · 6.27 KB
/
preprocess.py
File metadata and controls
204 lines (160 loc) · 6.27 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import csv
import time
import sys
import subprocess
import codecs
from utils import *
import unicodedata
import argparse
import cPickle as pickle
reload(sys)
sys.setdefaultencoding('utf8')
# pattern for removing redundunt spaces
space_pat = re.compile(u'\s\s+', re.U)
# # pattern for removing English - not used now
# eng_words_pat = re.compile(u'[A-Za-z]*',re.U)
# aux 1.1+
def translate_non_alphanumerics(to_translate, translate_to=None):
"""
Deleting not needed symbols
"""
not_letters_or_digits = u'[!&#%\"\'()_`{※+,』\|}~?...…「〜>r()<`!」?_%・@@”’":;+ー!。。。、_・_ _『 □***-\.\/:;<=>△?@\[\]\^'
translate_table = dict((ord(char), translate_to) for char in not_letters_or_digits)
return to_translate.translate(translate_table)
# aux 1+
def clean_lyrics(lyrics_file):
"""
Take crawled data and do some simple cleaning on it
:param lyrics_file: crawled data
:return: cleaned data, which will be fed to Kytea
"""
data_corpus = []
with open(lyrics_file) as csvfile:
reader = csv.reader(csvfile, delimiter="\t")
for row in reader:
sentences = row[2].strip().split(u"<BR>")
for sentence in sentences:
sentence = unicode(sentence)
sentence = translate_non_alphanumerics(sentence)
sentence = space_pat.sub(u' ', sentence)
if len(sentence) > 1:
data_corpus.append(sentence)
data_corpus.append(u"\n")
print(" Done cleaning crawled data! ")
# saving the corpus
with codecs.open("data/cleaned_lyrics.txt", "w", 'UTF-8') as f:
f.write("\n".join(data_corpus))
# aux 2
def create_corpus(crawled_lyrics_file, save=False):
"""
Load cleaned crawled corpus from local folder, feed to Kytea, get the output.
Then laod the output and do post-processing.
:param crawled_lyrics_file:
:param save:
:return: clean_corpus file
"""
# generating cleaned lyrics corpus from crawled data
clean_lyrics(crawled_lyrics_file) # the corpus is one sequence of characters per line
subprocess.call('kytea < ./data/cleaned_lyrics.txt > ./data/kytea_out.txt', shell=True) # processing with kytea
print(" Done kytea processing! ")
pron = []
unk_pat = re.compile(u"/補助記号/UNK")
slash_pat = re.compile(ur"\\")
with codecs.open("data/kytea_out.txt", 'UTF-8') as f:
for line in f:
line = line.decode(encoding="utf-8")
if line[0] == "\n":
pron.append(u"\n")
line = line.strip()
line = unk_pat.sub(u"", line)
line = slash_pat.sub(u"", line)
triplets = line.split(u" ") # take a look at Kytea output: https://github.com/chezou/Mykytea-python
seq = []
for item in triplets:
try:
hir = item.split(u"/")[0]
if hir != "\\":
seq.append(hir)
except IndexError:
continue
candidate_line = unicodedata.normalize("NFKC", u" ".join(seq))
candidate_line = re.sub(u"[A-Za-z]", u"", candidate_line)
candidate_line = re.sub(u"\s+", u"", candidate_line)
candidate_line = re.sub(u"\d+", u"5", candidate_line)
if len(candidate_line) > 2:
pron.append(candidate_line)
juman_input = u"\n".join(pron)
juman_input = re.sub(u"\n{4}",u"\n\n",juman_input)
return juman_input
# main function - creates input for the NN
def clean_corpus(crawled_lyrics_file, savepath=None):
print("Preparing data ...")
text = create_corpus(crawled_lyrics_file, save=False).lower()
if savepath != None:
with open(savepath, "w") as f:
f.write(text)
print(" Clean data saved into ----->%s " % (savepath))
def process_juman_output(juman_outfile):
print(" Processing juman output ...")
corpus = []
hiragana_corpus = []
daihyou_vocab = {}
with open(juman_outfile) as csvfile:
reader = csv.reader(csvfile, delimiter=str(u" "))
sent = []
hirag_sent = []
for line in reader:
if line[0] == u"@":
continue
if line[0] == u"EOS":
corpus.append(u" ".join(sent))
hiragana_corpus.append(u" ".join(hirag_sent))
hirag_sent = []
sent=[]
continue
if line[11] != "NIL":
value = line[11]
value = re.sub("代表表記:", u"", value,re.U)
value = value.split(u"/")[0]
else:
value = line[0]
hiragana = line[1]
hirag_sent.append(hiragana)
key = line[0]
daihyou_vocab[key] = value
sent.append(key)
corpus = u"\n".join(corpus)
hiragana_corpus = u"\n".join(hiragana_corpus)
corpus = re.sub(u"\n\n",u"\n",corpus)
hiragana_corpus = re.sub(u"\n\n",u"\n", hiragana_corpus)
print " All in all unique lemmas: %d" %(len(daihyou_vocab.values()))
# save a txt corpus file
with open("data/string_corpus.txt","w") as f:
for line in corpus.split(u"\n"):
print >> f, line
# save hiragana corpus
with open("data/hiragana_corpus.txt","w") as fo:
for line in hiragana_corpus.split(u"\n"):
print >> fo, line
# save a vocabulary
with open("data/daihyou_vocab.p", "w") as vocabfile:
pickle.dump(daihyou_vocab, vocabfile)
print "cleaning datadir ..."
subprocess.call('rm -f ./data/juman_input.txt ./data/kytea_out.txt ./data/cleaned_lyrics.txt',
shell=True)
def main():
parser = argparse.ArgumentParser(description="An LSTM language model")
parser.add_argument('-juman', help='Preprocess juman file', nargs=1)
parser.add_argument('-crawl', help='Preprocess crawled data', nargs=1)
opts = parser.parse_args()
if opts.crawl:
clean_corpus(opts.crawl[0], savepath="data/juman_input.txt")
print " Done cleaning crawled data"
if opts.juman:
process_juman_output(opts.juman[0])
print "Done preparing the corpus"
if __name__ == '__main__':
main()