Upload 2 files
Browse files- .gitattributes +1 -0
- separate-stories.py +68 -0
- stories.json +3 -0
.gitattributes
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
cleaned_merged_fairy_tales_without_eos.txt filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 1 |
cleaned_merged_fairy_tales_without_eos.txt filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
stories.json filter=lfs diff=lfs merge=lfs -text
|
separate-stories.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def main():
|
| 5 |
+
|
| 6 |
+
json_array = []
|
| 7 |
+
|
| 8 |
+
with open('all_titles.txt', encoding="utf-8") as title_file:
|
| 9 |
+
all_titles = title_file.read().splitlines()
|
| 10 |
+
|
| 11 |
+
with open('cleaned_merged_fairy_tales_without_eos.txt', encoding='UTF-8') as stories_file:
|
| 12 |
+
content = stories_file.read().splitlines()
|
| 13 |
+
|
| 14 |
+
with open('lines.txt', encoding="utf-8") as lines_file:
|
| 15 |
+
all_lines = lines_file.read().splitlines()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
print("Now converting the .txt file to a .json ...")
|
| 19 |
+
print("Please wait ...")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
for idx in range(len(all_titles)):
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
|
| 26 |
+
json_item = {}
|
| 27 |
+
|
| 28 |
+
title1 = all_titles[idx]
|
| 29 |
+
json_item["Title"] = title1
|
| 30 |
+
|
| 31 |
+
start_idx = int(all_lines[idx])
|
| 32 |
+
end_idx = int(all_lines[idx+1])
|
| 33 |
+
|
| 34 |
+
content_list = content[start_idx : end_idx-1]
|
| 35 |
+
json_item["Content"] = " ".join([str(item) for item in content_list])
|
| 36 |
+
json_array.append(json_item)
|
| 37 |
+
|
| 38 |
+
except: # except IndexError: list index out of range
|
| 39 |
+
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
result = remove_duplicate_items(json_array)
|
| 43 |
+
|
| 44 |
+
with open('stories.json', "w", encoding="utf-8") as myfile2:
|
| 45 |
+
myfile2.write(json.dumps(result, ensure_ascii=False, indent=4))
|
| 46 |
+
|
| 47 |
+
print("Completed. Please find the stories in stories.json.")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def remove_duplicate_items(json_list):
|
| 51 |
+
|
| 52 |
+
# keep a dictionary of key = title, value = count to see how many times the same story appears,
|
| 53 |
+
# then remove duplicates
|
| 54 |
+
|
| 55 |
+
result_json = []
|
| 56 |
+
keys = []
|
| 57 |
+
|
| 58 |
+
for idx in range(len(json_list)):
|
| 59 |
+
if json_list[idx]['Title'] in keys:
|
| 60 |
+
pass
|
| 61 |
+
else:
|
| 62 |
+
result_json.append(json_list[idx])
|
| 63 |
+
keys.append(json_list[idx]['Title'])
|
| 64 |
+
|
| 65 |
+
return result_json
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
main()
|
stories.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c081e282dd2d58a439c12546db0b35ca8246c94bf10de5d709c7b10d1f3d952b
|
| 3 |
+
size 20503451
|