Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script for extracting word occurrences in the movie scripts #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ A new metadata file is created with the following format:
}
}
}

### Get Word Occurance in Dialogue

Run
```
python get_occurances.py
```


```

## Directory structure
Expand Down
12 changes: 12 additions & 0 deletions get_all_titles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import json
from tqdm import tqdm

with open(os.path.join(os.getcwd(), 'scripts/metadata/clean_parsed_meta.json'), 'r', encoding='utf-8') as f:
data = json.load(f)
titles = dict()
for movie in data.items():
name = movie[1]['file']['name']
titles[name] = name.replace(' ', '-')+'.json'
with open('all_titles.json', 'w') as json_file:
json.dump(titles, json_file, indent=4, separators=(',',':'))
29 changes: 29 additions & 0 deletions get_occurances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import json
import glob
from pathlib import Path
from tqdm import tqdm

WRITE_DIR = 'scripts/occurances/'
if not os.path.exists(WRITE_DIR):
os.makedirs(WRITE_DIR)

for filename in tqdm(glob.glob('scripts/parsed/dialogue/*.txt')):
with open(os.path.join(os.getcwd(), filename), 'r', encoding='utf-8') as f:
words = dict()
for line in f:
dialogue = line.split(">",1)[1].lower()
for word in dialogue.split():
if (word):
list_of_chars = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for char in list_of_chars:
word = word.replace(char,'')
if (word in words):
words[word] += 1
else:
words[word] = 1
sorted_words = dict(sorted(words.items(), key=lambda item: item[1]))
clean_filename = Path(filename).stem
clean_filename = clean_filename.replace('_dialogue', '')
with open(WRITE_DIR+clean_filename+'.json', 'w') as json_file:
json.dump(sorted_words, json_file, indent=4, separators=(',',':'))