-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.py
More file actions
90 lines (67 loc) · 1.73 KB
/
p1.py
File metadata and controls
90 lines (67 loc) · 1.73 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import json
import nltk.data
import os
def Capitalise_Name(string):
capitalized_str = " ".join([word.capitalize() for word in string.split(" ")])
return capitalized_str
def Capitalise_Paragraph(string):
sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
sentences = sent_tokenizer.tokenize(string)
sentences = [sent.capitalize() for sent in sentences]
out = ""
for sent in sentences:
out = out + " " + sent
return out
def makeJsonFile(keys, values, file):
if len(keys) == len(values):
size =len(keys)
json_dict = {}
for i in range(size):
json_dict[keys[i]] = values[i]
else:
return None
filename = file.split(".")
filename[0] = filename[0] + "_o"
file = filename[0] + "." + filename[1]
with open(file, 'w') as json_file:
json.dump(json_dict, json_file)
return 1
def main():
path2dir = str(input("Enter path to directory containing files : "))
print(path2dir)
filenames = []
data = []
for file in os.listdir(path2dir):
if os.path.isfile(os.path.join(path2dir, file)):
#print(file)
if file != ".DS_Store":
filenames.append(file)
f = open(file)
d = json.load(f)
data.append(d)
f.close()
# print(data)
# print(len(data))
i = 0
for d in data:
keys = []
values = []
for (k,v) in d.items():
keys.append(k)
values.append(v)
index = 0
for k in keys:
if "name" in k:
#Capitalise the name
values[index] = Capitalise_Name(values[index])
elif "comment" in k or "note" in k:
#Capitalise the paragraph
values[index] = Capitalise_Paragraph(values[index])
index += 1
if makeJsonFile(keys,values,filenames[i]) != None:
print("File succesfully updated.")
else:
print("Error updating file.")
i += 1
if __name__ == '__main__':
main()