Skip to content

Commit

Permalink
Add scriptified extract_emotion_labels.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Demfier committed Jan 18, 2020
1 parent 39f198f commit 46363c6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 1_extract_emotion_labels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
76 changes: 76 additions & 0 deletions src/extract_emotion_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
This script parses the dataset, extracts label and stores it at one place
Run this script from root as python src/extract_emotion_labels.py
"""

import re
import os
import pandas as pd


def extract_info():
"""
returns info_dict containing important info from the IEMOCAP dataset
such as start time, end time, emotion labels etc.
extract_info: None -> Dict
"""
info_dict = {'start_times': [], 'end_times': [], 'wav_file_names': [],
'emotions': [], 'vals': [], 'acts': [], 'doms': []}

# regex used to identify useful info in the dataset files
info_line = re.compile(r'\[.+\]\n', re.IGNORECASE)
for sess in range(1, 6):
emo_evaluation_dir = 'data/IEMOCAP_full_release/Session{}/dialog/EmoEvaluation/'.format(sess)
# Only include the session files
evaluation_files = [l for l in os.listdir(emo_evaluation_dir)
if 'Ses' in l]
for file in evaluation_files:
with open(emo_evaluation_dir + file) as f:
content = f.read()
# grab the important stuff
info_lines = re.findall(info_lines, content)
for line in info_line[1:]: # skipping the first header line
# Refer to the dataset to see how `line` looks like
start_end_time, wav_file_name, emotion, val_act_dom = \
line.strip().split('\t')
start_time, end_time = start_end_time[1:-1].split('-')
val, act, dom = val_act_dom[1:-1].split(',')
val, act, dom = float(val), float(act), float(dom)
start_time, end_time = float(start_time), float(end_time)
info_dict['start_times'].append(start_time)
info_dict['end_times'].append(end_time)
info_dict['wav_file_names'].append(wav_file_name)
info_dict['emotions'].append(emotion)
info_dict['vals'].append(val)
info_dict['acts'].append(act)
info_dict['doms'].append(dom)
return info_dict


def compile_dataset(info_dict):
"""
creates a csv file from info_dict which will serve as the dataset
compile_dataset: Dict -> None
"""
df_iemocap = pd.DataFrame(columns=['start_time', 'end_time', 'wav_file', 'emotion', 'val', 'act', 'dom'])

df_iemocap['start_time'] = info_dict['start_times']
df_iemocap['end_time'] = info_dict['end_times']
df_iemocap['wav_file'] = info_dict['wav_file_names']
df_iemocap['emotion'] = info_dict['emotions']
df_iemocap['val'] = info_dict['vals']
df_iemocap['act'] = info_dict['acts']
df_iemocap['dom'] = info_dict['doms']
# Finally, save to a file
df_iemocap.to_csv('data/pre-processed/df_iemocap.csv', index=False)


def main():
compile_dataset(extract_info())


if __name__ == '__main__':
main()

0 comments on commit 46363c6

Please sign in to comment.