-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_books.py
executable file
·179 lines (138 loc) · 5.05 KB
/
clean_books.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
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
#! /usr/bin/python
import os
import sys
import nltk
import glob
import argparse
import numpy as np
stupidNames = ['in', 'an', 'many', 'love', 'precious', 'king', 'long', \
'forest', 'sage', 'chance', 'ai', 'sun', 'rose', 'golden', \
'so', 'miles', 'my', 'son', 'see', 'may', 'else', 'spring', \
'soon', 'young', 'miss', 'man', 'moon', 'van', 'marry', \
'autumn', 'summer', 'song', 'season', 'sang']
rawBookDir = 'test/'
cleanBookDir = 'clean_books_test/'
gatherBookDir = 'clean_books_div/'
namesDBFile = 'names_db.txt'
""" Function to load in names database """
def getNamesDB():
namesList = []
fidNames = open(namesDBFile, 'r')
for nameLine in fidNames:
name = nameLine.split()[0]
name = name.lower()
if name not in stupidNames:
namesList.append(name)
fidNames.close()
return set(namesList)
""" Function to obtain counts of tokens for determining <unk> threshold """
def countAndCalculate(args):
keepThresh = args.th
wordCounterDict = {}
for currBook in glob.glob(rawBookDir + '*.txt'):
print 'Getting counts for:', currBook
fid = open(currBook, 'r')
rawBook = fid.read().decode('utf-8')
wordTokens = nltk.word_tokenize(rawBook)
lowerWordTokens = [wl.lower() for wl in wordTokens]
lowerWordTokens = \
[t.replace("``", '"').replace("''", '"') for t in lowerWordTokens]
for wl in range(len(lowerWordTokens)):
lowerWordTokens[wl] = lowerWordTokens[wl].\
rstrip('-').strip('-').rstrip('_').strip('_')
# Keep track of counts
for word in lowerWordTokens:
if word in wordCounterDict:
wordCounterDict[word] += 1
else:
wordCounterDict[word] = 1
fid.close()
totalTokens = len(wordCounterDict)
# Compute "keep threshold"
cutoff = np.ceil(totalTokens * keepThresh)
unkWords = []
for key, value in wordCounterDict.iteritems():
if value < cutoff:
unkWords.append(key)
# Print some statistics
print 'Total number of unique tokens:', totalTokens
print 'Cutoff count:', cutoff
print 'Number of <unk> words:', len(unkWords)
print
return set(unkWords)
""" Function to tokenize and clean tokens. Should be run after
countAndCalculate """
def tokenizeAndClean(unkWordSet):
# Get all names
namesDB = getNamesDB()
# Check if directory exists. If not, make it.
if not os.path.exists(cleanBookDir):
os.makedirs(cleanBookDir)
for currBook in glob.glob(rawBookDir + '*.txt'):
bookName = currBook.split('/')[-1]
print 'Now cleaning:', bookName
fidClean = open(cleanBookDir + bookName, 'w')
fidRaw = open(currBook, 'r')
rawBook = fidRaw.read().decode('utf-8')
sentTokens = nltk.sent_tokenize(rawBook)
# For each sentence, tokenize and make all words lowercase
for sent in sentTokens:
wordTokens = nltk.word_tokenize(sent)
lowerWordTokens = [wl.lower() for wl in wordTokens]
lowerWordTokens = \
[t.replace("``", '"').replace("''", '"') for t in lowerWordTokens]
for wl in range(len(lowerWordTokens)):
lowerWordTokens[wl] = lowerWordTokens[wl].\
rstrip('-').strip('-').rstrip('_').strip('_')
for w in range(len(lowerWordTokens)):
if lowerWordTokens[w] in namesDB:
lowerWordTokens[w] = '<name>'
elif lowerWordTokens[w] in unkWordSet:
lowerWordTokens[w] = '<unk>'
# Join words
cleanSent = ' '.join(lowerWordTokens)
fidClean.write(cleanSent.encode('utf-8') + '\n')
fidClean.close()
fidRaw.close()
print 'All books have been cleaned! Cleaned books available in', cleanBookDir
def gatherBooks(args):
# Check if directory exists. If not, make it.
if not os.path.exists(gatherBookDir):
os.makedirs(gatherBookDir)
lineCap = args.linesCap
uniqueAuthorSet = set()
for currBook in glob.glob(cleanBookDir + '*.txt'):
currbookSplit = currBook.split('___')[0].split('/')
uniqueAuthorSet.add(currbookSplit[1])
for author in uniqueAuthorSet:
fidAll = open(gatherBookDir + author + '_all.txt', 'w')
currLines = 0
for currBook in glob.glob(cleanBookDir + author + '*.txt'):
fidRead = open(currBook, 'r')
for line in fidRead:
if currLines < lineCap:
fidAll.write(line)
currLines += 1
else:
break
if currLines >= lineCap:
break
fidRead.close()
fidAll.close()
print 'Concatenation complete, now getting line counts'
for currBook in glob.glob(gatherBookDir + '*_all.txt'):
fid = open(currBook, 'r')
allLines = fid.readlines()
bookTitle = currBook.split('/')[1]
print bookTitle, 'lines:', len(allLines)
print 'Line counting complete!'
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('--th', type=float, default=0.0001,
help='Perc of total tokens to keep (default: 0.0001)')
ap.add_argument('--linesCap', type=int, default=12000,
help='Line limit per author (default: 12000)')
args = ap.parse_args()
unkWords = countAndCalculate(args)
tokenizeAndClean(unkWords)
gatherBooks(args)