-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsefiles.py
More file actions
executable file
·56 lines (50 loc) · 1.7 KB
/
parsefiles.py
File metadata and controls
executable file
·56 lines (50 loc) · 1.7 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
# Tokenize and Parse a bunch of newsela articles and write to stdout.
import sys
import newselautil as nsla
import classpaths as path
import StanfordParse
def all():
"""
tokenize all articles
:return: None
"""
articles = nsla.loadMetafile()
i = 0
# process articles by slug
while i < len(articles):
slug = articles[i]['slug']
NOfLevels = 1
while (i < len(articles))and(articles[i+1]['slug'] == slug):
NOfLevels += 1
i += 1
processFile(slug, NOfLevels)
print ('Parsing:' + slug+' '+str(round(i/float(len(articles)), 5))+' of the task completed')
i += 1
def particular(needed):
"""
tokenized files with specified slugs
:param needed: the list of slugs to tokenize
:return: None
"""
for slug in needed:
processFile(slug)
def processFile(slug,numberOfLevels = 6):
"""
tokenize the files with a given slug
:param slug: the slug to tokenize
:param numberOfLevels: specified number of Levels to process
:return: None
"""
for i in range(numberOfLevels):
try:
StanfordParse.tokenize(path.BASEDIR + '/articles/' + slug + ".en." + str(i) + ".txt")
with open(path.BASEDIR + '/articles/' + slug + ".en." + str(i) + ".txt.tok") as file:
lines = file.readlines()
for j in range(len(lines)):
lines[j] = lines[j].replace("'", "`")
with open(path.BASEDIR + '/articles/' + slug + ".en." + str(i) + ".txt.tok", 'w') as file:
file.writelines(lines)
except:
print('ERROR while parsing %s' % (slug))
if __name__ == "__main__":
processFile("Hurricane-drones")