def what_was_duration(dataset, narrative, rel_diff=0.1): questions = [ 'What was the <AD> sound?', 'What was the <AD> sound you [heard,listened to]?', 'What was the <AD> sound that you [heard,listened to]?', 'What was the <AD> sound that was heard?', ] question = str(np.random.choice(questions)) # sample question duration = sample_absolute_duration() question = question.replace('<AD>', duration) # insert duration question = sanitize_question(question) # correct grammar lst_events = get_lst_events(narrative) lst_durations = get_lst_durations(narrative) if 'long' in question: est = np.argmax(lst_durations) elif 'short' in question: est = np.argmin(lst_durations) else: assert False, \ 'Duration illdefined in Question (what_was_duration).' # Assert a good margin in relative duration evt_duration = lst_durations[est] x_durations = [j for i, j in enumerate(lst_durations) if i != est] rel_duration_diff = compute_rel_diff(np.array(x_durations), np.array(evt_duration)) assert np.sum(rel_duration_diff < rel_diff) <= 0, \ 'Question (what_was_duration) illposed.' e = lst_events[est] answer = (str(np.random.choice(dataset['sources'][e])) + ' ' + str(np.random.choice(dataset['actions'][e]))) return question, answer
def what_was_duration_relative(dataset, narrative, rel_diff=0.1): questions = [ 'What was the <AD> sound <RO> the <S> <A>?', 'What was the <AD> sound <RO> [hearing,listening to] the <S> <A>?', 'What was the <AD> sound <RO> the <S> <A> was heard?', ] question = str(np.random.choice(questions)) # sample question duration = sample_absolute_duration() preposition = sample_preposition() lst_events = get_lst_events(narrative) unique_lst_events = [e for e in lst_events if lst_events.count(e) == 1] assert len(unique_lst_events) > 0, \ 'Question (what_was_duration_relative) illposed.' event = str(np.random.choice(unique_lst_events)) source = str(np.random.choice(dataset['sources'][event])) # sample source action = str(np.random.choice(dataset['actions'][event])) # sample action question = question.replace('<AD>', duration) # insert duration question = question.replace('<RO>', preposition) # insert preposition question = question.replace('<S>', source) # insert source question = question.replace('<A>', action) # insert action question = sanitize_question(question) # correct grammar assert lst_events.count(event) == 1, \ 'Question (what_was_duration_relative) illposed.' lst_durations = get_lst_durations(narrative) event_idx = lst_events.index(event) if 'before' in question: lst_events_e = lst_events[:event_idx] lst_events_d = lst_durations[:event_idx] elif 'after' in question: lst_events_e = lst_events[(event_idx + 1):] lst_events_d = lst_durations[(event_idx + 1):] else: assert False, \ 'Preposition illdefined in Question (what_was_duration_relative).' assert len(lst_events_e) > 0, \ 'Question (what_was_duration_relative) illposed.' if 'long' in question: est = np.argmax(lst_events_d) elif 'short' in question: est = np.argmin(lst_events_d) else: assert False, \ 'Duration illdefined in Question (what_was_duration_relative).' # Assert a good margin in relative duration evt_duration = lst_events_d[est] x_durations = [j for i, j in enumerate(lst_events_d) if i != est] rel_duration_diff = compute_rel_diff(np.array(x_durations), np.array(evt_duration)) assert np.sum(rel_duration_diff < rel_diff) <= 0, \ 'Question (what_was_duration_relative) illposed.' e = lst_events_e[est] answer = (str(np.random.choice(dataset['sources'][e])) + ' ' + str(np.random.choice(dataset['actions'][e]))) return question, answer
def what_was_duration_relative_ordinal(dataset, narrative, rel_diff=0.1): questions = [ 'What was the <AD> sound <RO> the <O> sound?', 'What was the <AD> sound <RO> [hearing,listening to] the <O> sound?', 'What was the <AD> sound <RO> the <O> sound was heard?', ] question = str(np.random.choice(questions)) # sample question duration = sample_absolute_duration() preposition = sample_preposition() lst_events = get_lst_events(narrative) number, ordinal = sample_number(len(lst_events)) question = question.replace('<AD>', duration) # insert duration question = question.replace('<RO>', preposition) # insert preposition question = question.replace('<O>', ordinal) # insert ordinal question = sanitize_question(question) # correct grammar lst_durations = get_lst_durations(narrative) event_idx = (number - 1) answer = None if 'before' in question: if (event_idx - 1) < 0: answer = 'nothing' else: lst_events_e = lst_events[:event_idx] lst_events_d = lst_durations[:event_idx] elif 'after' in question: if (event_idx + 1) >= len(lst_events): answer = 'nothing' else: lst_events_e = lst_events[(event_idx + 1):] lst_events_d = lst_durations[(event_idx + 1):] else: assert False, \ 'Preposition illdefined in Question (what_was_duration_relative_ordinal).' if answer is None: assert len(lst_events_e) > 0, \ 'Question (what_was_duration_relative_ordinal) illposed.' if 'long' in question: est = np.argmax(lst_events_d) elif 'short' in question: est = np.argmin(lst_events_d) else: assert False, \ 'Duration illdefined in Question (what_was_duration_relative_ordinal).' # Assert a good margin in relative duration evt_duration = lst_events_d[est] x_durations = [j for i, j in enumerate(lst_events_d) if i != est] rel_duration_diff = compute_rel_diff(np.array(x_durations), np.array(evt_duration)) assert np.sum(rel_duration_diff < rel_diff) <= 0, \ 'Question (what_was_duration_relative_ordinal) illposed.' e = lst_events_e[est] answer = (str(np.random.choice(dataset['sources'][e])) + ' ' + str(np.random.choice(dataset['actions'][e]))) return question, answer
def compare_duration(dataset, narrative, rel_diff=0.1): questions = [ 'Was the <S1> <A1> <RD> than the <S2> <A2>?', 'Was the sound of the <S1> <A1> <RD> than the sound of the <S2> <A2>?', # noqa: E501 '[Comparing,Listening to,Hearing] the sound of the <S1> <A1> and the sound of the <S2> <A2>, was the former <RD>?', # noqa: E501 '[Comparing,Listening to,Hearing] the sounds of the <S1> <A1> and the <S2> <A2>, was the former <RD>?', # noqa: E501 '[Comparing,Listening to,Hearing] the sound of the <S2> <A2> and the sound of the <S1> <A1>, was the latter <RD>?', # noqa: E501 '[Comparing,Listening to,Hearing] the sounds of the <S2> <A2> and the <S1> <A1>, was the latter <RD>?', # noqa: E501 ] question = str(np.random.choice(questions)) # sample question lst_events = get_lst_events(narrative) unique_lst_events = [e for e in lst_events if lst_events.count(e) == 1] assert len(unique_lst_events) > 0, \ 'Question (compare_duration) illposed.' event_1 = str(np.random.choice(unique_lst_events)) # sample event source_1 = str(np.random.choice(dataset['sources'][event_1])) action_1 = str(np.random.choice(dataset['actions'][event_1])) rel_duration = sample_rel_duration() x_unique_lst_events = [e for e in unique_lst_events if e != event_1] assert len(x_unique_lst_events) > 0, \ 'Question (compare_duration) illposed.' event_2 = str(np.random.choice(x_unique_lst_events)) # sample event source_2 = str(np.random.choice(dataset['sources'][event_2])) action_2 = str(np.random.choice(dataset['actions'][event_2])) assert lst_events.count(event_1) == 1, \ 'Question (compare_duration) illposed.' assert lst_events.count(event_2) == 1, \ 'Question (compare_duration) illposed.' assert event_1 != event_2, 'Question (compare_duration) illposed.' question = question.replace('<S1>', source_1) # insert source question = question.replace('<A1>', action_1) # insert action question = question.replace('<RD>', rel_duration) # insert duration question = question.replace('<S2>', source_2) # insert source question = question.replace('<A2>', action_2) # insert action question = sanitize_question(question) lst_duration = get_lst_durations(narrative) e_1_duration = lst_duration[lst_events.index(event_1)] e_2_duration = lst_duration[lst_events.index(event_2)] # Assert a good margin in relative duration rel_duration_diff = compute_rel_diff(np.array(e_1_duration), np.array(e_2_duration)) assert np.sum(rel_duration_diff < rel_diff) <= 0, \ 'Question (compare_duration) illposed.' if 'short' in question: answer = 'yes' if e_1_duration < e_2_duration else 'no' elif 'long' in question: answer = 'yes' if e_1_duration > e_2_duration else 'no' else: assert False, 'Duration illdefined in Question (compare_duration).' return question, answer
def how_many_sounds_duration_event(dataset, narrative, rel_diff=0.1): questions = ['How many [sound events,sounds] [roughly,approximately] as <D> as the <S> <A>?', # noqa: E501 'How many [sound events,sounds] that are [roughly,approximately] as <D> as the <S> <A> [did,could] you [hear,listen to]?', # noqa: E501 'How many [sound events,sounds] that are [roughly,approximately] as <D> as the <S> <A> have you heard?', # noqa: E501 'How many [sound events,sounds] that have [roughly,approximately] the same duration as the <S> <A>?', # noqa: E501 'How many [sound events,sounds] that have [roughly,approximately] the same duration as the <S> <A> [did,could] you [hear,listen to]?', # noqa: E501 'How many [sound events,sounds] that have [roughly,approximately] the same duration as the <S> <A> have you heard?', # noqa: E501 'What is the number of [sound events,sounds] [roughly,approximately] as <D> as the <S> <A>?', # noqa: E501 'What is the number of [sound events,sounds] that are [roughly,approximately] as <D> as the <S> <A> [did,could] you [hear,listen to]?', # noqa: E501 'What is the number of [sound events,sounds] that are [roughly,approximately] as <D> as the <S> <A> have you heard?', # noqa: E501 'What is the number of [sound events,sounds] that have [roughly,approximately] the same duration as the <S> <A>?', # noqa: E501 'What is the number of [sound events,sounds] that have [roughly,approximately] the same duration as the <S> <A> [did,could] you [hear,listen to]?', # noqa: E501 'What is the number of [sound events,sounds] that have [roughly,approximately] the same duration as the <S> <A> have you heard?', # noqa: E501 'There is [a,an] <S> <A>; how many [sound events,sounds] that are [roughly,approximately] as <D>?', # noqa: E501 'There is [a,an] <S> <A>; what is the number of [sound events,sounds] that are [roughly,approximately] as <D>?', # noqa: E501 ] question = str(np.random.choice(questions)) # sample question duration = sample_duration() # sample duration lst_events = get_lst_events(narrative) unique_lst_events = [e for e in lst_events if lst_events.count(e) == 1] assert len(unique_lst_events) > 0, \ 'Question (how_many_sounds_duration_event) illposed.' event = str(np.random.choice(unique_lst_events)) source = str(np.random.choice(dataset['sources'][event])) # sample source action = str(np.random.choice(dataset['actions'][event])) # sample action question = question.replace('<D>', duration) # insert duration question = question.replace('<S>', source) # insert source question = question.replace('<A>', action) # insert action question = sanitize_question(question) # correct grammar assert lst_events.count(event) == 1, \ 'Question (how_many_sounds_duration_event) illposed.' lst_durations = get_lst_durations(narrative) event_idx = lst_events.index(event) evt_duration = lst_durations[event_idx] x_durations = [j for i, j in enumerate(lst_durations) if i != event_idx] rel_durations_diff = compute_rel_diff(np.array(x_durations), np.array(evt_duration)) # Assert a good margin in relative loudness assert np.sum(np.logical_and(rel_durations_diff > rel_diff, rel_durations_diff < (2 * rel_diff))) <= 0, \ 'Question (how_many_sounds_duration_event) illposed.' answer = numbers_to_words(np.sum(rel_durations_diff <= rel_diff)) return question, answer
def compare_duration_ordinal(dataset, narrative, rel_diff=0.1): questions = [ 'Was the <O1> [sound event,sound] <RD> than the <O2> [sound event,sound]?', # noqa: E501 '[Comparing,Listening to,Hearing] the <O1> [sound event,sound] and the <O2> [sound event,sound], was the former <RD>?', # noqa: E501 '[Comparing,Listening to,Hearing] the <O1> and <O2> [sound events,sounds], was the former <RD>?', # noqa: E501 '[Comparing,Listening to,Hearing] the <O2> [sound event,sound] and the <O1> [sound event,sound], was the latter <RD>?', # noqa: E501 '[Comparing,Listening to,Hearing] the <O2> and <O1> [sound events,sounds], was the latter <RD>?', # noqa: E501 ] question = str(np.random.choice(questions)) # sample question lst_events = get_lst_events(narrative) number_1, ordinal_1 = sample_number(len(lst_events)) rel_duration = sample_rel_duration() number_2, ordinal_2 = sample_second_number(len(lst_events), number_1) assert number_1 != number_2, 'Question (compare_duration_ordinal) illposed.' question = question.replace('<O1>', ordinal_1) # insert ordinal question = question.replace('<RD>', rel_duration) # insert duration question = question.replace('<O2>', ordinal_2) # insert ordinal question = sanitize_question(question) # correct grammar lst_duration = get_lst_durations(narrative) e_1_duration = lst_duration[number_1 - 1] e_2_duration = lst_duration[number_2 - 1] # Assert a good margin in relative duration rel_duration_diff = compute_rel_diff(np.array(e_1_duration), np.array(e_2_duration)) assert np.sum(rel_duration_diff < rel_diff) <= 0, \ 'Question (compare_duration_ordinal) illposed.' if 'short' in question: answer = 'yes' if e_1_duration < e_2_duration else 'no' elif 'long' in question: answer = 'yes' if e_1_duration > e_2_duration else 'no' else: assert False, 'Duration illdefined in Question (compare_duration_ordinal).' return question, answer
def how_many_sounds_duration_ordinal(dataset, narrative, rel_diff=0.1): questions = ['How many [sound events,sounds] [roughly,approximately] as <D> as the <O> sound?', # noqa: E501 'How many [sound events,sounds] that are [roughly,approximately] as <D> as the <O> sound?', # noqa: E501 'How many [sound events,sounds] that are [roughly,approximately] as <D> as the <O> sound [did,could] you [hear,listen to]?', # noqa: E501 'How many [sound events,sounds] that are [roughly,approximately] as <D> as the <O> sound have you heard?', # noqa: E501 'How many [sound events,sounds] that have [roughly,approximately] the same duration as the <O> sound?', # noqa: E501 'How many [sound events,sounds] that have [roughly,approximately] the same duration as the <O> sound [did,could] you [hear,listen to]?', # noqa: E501 'How many [sound events,sounds] that have [roughly,approximately] the same duration as the <O> sound have you heard?', # noqa: E501 'What is the number of [sound events,sounds] [roughly,approximately] as <D> as the <O> sound?', # noqa: E501 'What is the number of [sound events,sounds] that are [roughly,approximately] as <D> as the <O> sound?', # noqa: E501 'What is the number of [sound events,sounds] that are [roughly,approximately] as <D> as the <O> sound [did,could] you [hear,listen to]?', # noqa: E501 'What is the number of [sound events,sounds] that are [roughly,approximately] as <D> as the <O> sound have you heard?', # noqa: E501 'What is the number of [sound events,sounds] that have [roughly,approximately] the same duration as the <O> sound?', # noqa: E501 'What is the number of [sound events,sounds] that have [roughly,approximately] the same duration as the <O> sound [did,could] you [hear,listen to]?', # noqa: E501 'What is the number of [sound events,sounds] that have [roughly,approximately] the same duration as the <O> sound have you heard?', # noqa: E501 ] question = str(np.random.choice(questions)) # sample question duration = sample_duration() # sample duration lst_events = get_lst_events(narrative) number, ordinal = sample_number(len(lst_events)) question = question.replace('<D>', duration) # insert duration question = question.replace('<O>', ordinal) # insert ordinal question = sanitize_question(question) # correct grammar lst_durations = get_lst_durations(narrative) evt_duration = lst_durations[number - 1] x_durations = [j for i, j in enumerate(lst_durations) if i != (number - 1)] rel_durations_diff = compute_rel_diff(np.array(x_durations), np.array(evt_duration)) # Assert a good margin in relative loudness assert np.sum(np.logical_and(rel_durations_diff > rel_diff, rel_durations_diff < (2 * rel_diff))) <= 0, \ 'Question (how_many_sounds_duration_ordinal) illposed.' answer = numbers_to_words(np.sum(rel_durations_diff <= rel_diff)) return question, answer