-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_doc.py
97 lines (73 loc) · 3.44 KB
/
get_doc.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
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
import os
from time import sleep,time
import deepcut
import pickle
import numpy as np
import datetime
owd = os.getcwd()
print(owd)
def extract_word():
os.chdir('data_dek-d_I')
list_dir1 = os.listdir()
# writer
if list_dir1 is not None:
for ii in range(2475, len(list_dir1)):
the_dir1 = list_dir1[ii]
time_now = datetime.datetime.now().strftime('%Y%m%d-%H:%M')
print(time_now)
print(ii, the_dir1)
os.chdir(the_dir1)
list_dir2 = os.listdir()
# novel
if list_dir2 is not None and list_dir2 != []:
for the_dir2 in list_dir2:
os.chdir(the_dir2)
list_dir3 = os.listdir()
# chapter
if list_dir3 is not None and list_dir3 != []:
doc = ''
token_all = []
count_chap = 1
for the_dir3 in list_dir3:
file = the_dir3
token = ''
novel = ''
f = open(file, "r", encoding="utf-8")
novel = f.read()
token = deepcut.tokenize(novel)
doc += novel
token_all += token
if len(token_all) >= 2000:
file_name = (the_dir1 +'_'+ the_dir2 + '_%1d%1d%1d.txt' % (np.floor(count_chap / 100), np.floor(count_chap/10) % 10, count_chap % 10))
F = open('C:\\Users\First ThinkPad\Desktop\Thai Stylometry\data_raheem\document\\'+
str(file_name), 'wt', encoding='utf-8')
F.writelines(doc)
F.close()
# save token file
with open('C:\\Users\First ThinkPad\Desktop\Thai Stylometry\data_raheem\\token\\' +
str(file_name), "wb") as fp: # Pickling
pickle.dump(token_all, fp, protocol=pickle.HIGHEST_PROTOCOL)
count_chap += 1
token_all = []
doc = ''
if count_chap < 5 and count_chap > 1:
for each_chap in range(1, count_chap):
file_name = (the_dir1 + '_' + the_dir2 + '_%1d%1d%1d.txt' % (
np.floor(each_chap / 100), np.floor(each_chap / 10) % 10, each_chap % 10))
# delete one by one
os.remove('C:\\Users\First ThinkPad\Desktop\Thai Stylometry\data_raheem\document\\'+ str(file_name))
os.remove(
'C:\\Users\First ThinkPad\Desktop\Thai Stylometry\data_raheem\\token\\' + str(
file_name))
os.chdir('..')
os.chdir('..')
if __name__ == '__main__':
extract_word()
# def into_dir(list_dir):
# if list_dir is not None:
# for the_dir in list_dir:
# os.chdir(the_dir)
# list_dir = os.listdir()
#
# return list_dir
#