-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextractdefinitions.py
executable file
·86 lines (68 loc) · 2.63 KB
/
extractdefinitions.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
#!/usr/bin/env python3
import os
import re
import json
import sys
def extract_defns(directory, chapter):
pattern = re.compile("DEFIN")
defns=[]
chdefns=[]
fileroot=""
flashcards=directory+'/flashcards/'
print(flashcards)
print("scanning directory: ", directory)
for filename in os.listdir(directory):
if filename.endswith(".ipynb"):
with open(directory + "/" + filename,'r') as file:
if len(defns)>0:
print(fileroot, len(defns))
print(defns)
print("\n\n")
#return defns
if not os.path.isdir(flashcards):
os.makedirs(flashcards)
outfile=flashcards+fileroot+'.json'
print(outfile)
with open(outfile,'w') as out:
json.dump(defns,out,indent=4)
fileroot=filename.split('.')[0]
#print(fileroot)
defns=[]
defn_start=0
for line in file:
for match in re.finditer(pattern, line):
defn_start=1
if defn_start>0:
if defn_start==4:
term=line.replace('",','')
term=term.replace('"','')
term=term.strip()
term=term.replace("\\n","")
#print(term)
if defn_start==5:
defn=line.replace('",','')
defn=defn.replace('"','')
defn=defn.replace(':','')
defn=defn.strip()
defn=defn.replace("\\n","")
defns+=[{"front":term, "back":defn}]
chdefns+=[{"front":term, "back":defn}]
#print(defn_start, line)
defn_start+=1
if defn_start==6:
#print(term,defn)
defn_start=0
else:
continue
outfile=flashcards+'ch'+str(chapter)+'.json'
print(outfile)
with open(outfile,'w') as out:
json.dump(chdefns,out,indent=4)
def main(args):
print(args)
if len(args)==2:
extract_defns( args[0], int(args[1]) )
else:
print("FAILED: expect exactly 2 arguments (directory and chapter number)")
if __name__ == "__main__":
main(sys.argv[1:])