forked from kuanghuei/Question-Answering-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.py
More file actions
69 lines (54 loc) · 1.39 KB
/
merge.py
File metadata and controls
69 lines (54 loc) · 1.39 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
import sys
import glob
import errno
import codecs
def sortFile(fileList):
quickSortHelper(fileList,0,len(fileList)-1)
def quickSortHelper(fileList,start,end):
if start < end:
pivot = partition(fileList,start,end)
quickSortHelper(fileList,start,pivot-1)
quickSortHelper(fileList,pivot+1,end)
def partition(fileList,start,end):
pivot = int((fileList[start].split('/')[-1]).split('.')[0])
left = start + 1
right = end
done = False
while not done:
while left <= right and \
int((fileList[left].split('/')[-1]).split('.')[0]) <= pivot:
left += 1
while int((fileList[right].split('/')[-1]).split('.')[0]) >= pivot \
and right >= left:
right -= 1
if right < left:
done = True
else:
temp = fileList[left]
fileList[left] = fileList[right]
fileList[right] = temp
temp = fileList[start]
fileList[start] = fileList[right]
fileList[right] = temp
return right
def main():
filepath = sys.argv[1] # THIS MAY CHANGE
filepath += "/*.txt"
files = glob.glob(filepath)
sortFile(files)
one = "processed/" + (filepath.split('/')[1]).split('_')[1] + ".txt"
print "merge " + one
fmerge = open(one,'a') # THIS MAY CHANGE
for name in files:
#print name
try:
with open(name) as f:
body = f.read()
body = body + '\n\n'
fmerge.write(body)
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
fmerge.close()
if __name__ == '__main__':
main()