Given a parse function like def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta(hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:])).total_seconds() end_seconds = timedelta(hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:])).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments for data like [00:00:14.240 --> 00:00:14.420] the [00:00:14.420 --> 00:00:14.860] robust [00:00:14.860 --> 00:00:15.020] ness [00:00:15.020 --> 00:00:15.140] of [00:00:15.140 --> 00:00:15.500] neural [00:00:15.500 --> 00:00:15.870] networks [00:00:15.870 --> 00:00:16.200] . [00:00:16.200 --> 00:00:16.410] And [00:00:16.410 --> 00:00:16.700] this [00:00:16.700 --> 00:00:16.840] is [00:00:16.840 --> 00:00:17.200] joint write a merge() function that (1) calls parse and (2) merges words into sentences that end in period. Start with the first word and end with the period. Return a list of strings.
Given a parse function like def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta(hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:])).total_seconds() end_seconds = timedelta(hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:])).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments for data like [00:00:14.240 --> 00:00:14.420] the [00:00:14.420 --> 00:00:14.860] robust [00:00:14.860 --> 00:00:15.020] ness [00:00:15.020 --> 00:00:15.140] of [00:00:15.140 --> 00:00:15.500] neural [00:00:15.500 --> 00:00:15.870] networks [00:00:15.870 --> 00:00:16.200] . [00:00:16.200 --> 00:00:16.410] And [00:00:16.410 --> 00:00:16.700] this [00:00:16.700 --> 00:00:16.840] is [00:00:16.840 --> 00:00:17.200] joint write a merge() function that (1) calls parse and (2) merges words into sentences that end in period. Start with the first word and end with the period. Return a list of strings.
Below is one way to write the merge() function. In this example, merge() takes the transcript text, uses your parse_transcript() function to get a list of segments, and then walks through the words accumulating them until a word that ends with a period is encountered. At that point it joins the accumulated words into a sentence, appends it to the result list, and then resets the accumulator for the next sentence. Note that in the sample transcript data the period is in its own segment (“.”) so the sentence will be built from all the segments up to and including that one. Here’s the complete code: ------------------------------------------------ import re from datetime import timedelta def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]\s+(.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta( hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:]) ).total_seconds() end_seconds = timedelta( hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:]) ).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments def merge(content): segments = parse_transcript(content) sentences = [] sentence_words = [] for start, end, word in segments: word = word.strip() # If you want to avoid an extra space before punctuation, # you can later join words accordingly. For simplicity we join with spaces. sentence_words.append(word) # Check if the current word ends with a period. if word.endswith('.'): sentence = ' '.join(sentence_words) sentences.append(sentence) sentence_words = [] # reset for the next sentence # Optionally, if there are leftover words that did not end with a period, # you might want to add them as an incomplete sentence. # For example: # if sentence_words: # sentences.append(' '.join(sentence_words)) return sentences # Example usage with the provided data: if __name__ == '__main__': content = """[00:00:14.240 --> 00:00:14.420] the [00:00:14.420 --> 00:00:14.860] robust [00:00:14.860 --> 00:00:15.020] ness [00:00:15.020 --> 00:00:15.140] of [00:00:15.140 --> 00:00:15.500] neural [00:00:15.500 --> 00:00:15.870] networks [00:00:15.870 --> 00:00:16.200] . [00:00:16.200 --> 00:00:16.410] And [00:00:16.410 --> 00:00:16.700] this [00:00:16.700 --> 00:00:16.840] is [00:00:16.840 --> 00:00:17.200] joint""" merged_sentences = merge(content) for s in merged_sentences: print(s) # Expected output: # the robust ness of neural networks . ------------------------------------------------ How It Works: 1. The parse_transcript() function uses regex to extract start and end times along with the word text. 2. In merge(), we iterate over the list of segments. Each word gets appended to an ongoing list. 3. When a word is encountered whose text ends with a period (e.g. “.”), that signals the end of the sentence. We then join the accumulated words (with a space between each) to form the sentence, append it to our list of sentences, and clear the accumulator. 4. Finally, merge() returns the list of sentences. This solution meets the requirements: it calls parse_transcript() and merges words into complete sentences that end in a period, starting with the very first word.
I extracted the following code from that output:
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 | import re from datetime import timedelta def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]\s+(.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta( hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:]) ).total_seconds() end_seconds = timedelta( hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:]) ).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments def merge(content): segments = parse_transcript(content) sentences = [] sentence_words = [] for start, end, word in segments: word = word.strip() sentence_words.append(word) if word.endswith('.'): sentence = ' '.join(sentence_words) sentences.append(sentence) sentence_words = [] return sentences |
Running the following program:
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 | import re from datetime import timedelta def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]\s+(.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta( hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:]) ).total_seconds() end_seconds = timedelta( hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:]) ).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments def merge(content): segments = parse_transcript(content) sentences = [] sentence_words = [] for start, end, word in segments: word = word.strip() sentence_words.append(word) if word.endswith('.'): sentence = ' '.join(sentence_words) sentences.append(sentence) sentence_words = [] return sentences def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta(hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:])).total_seconds() end_seconds = timedelta(hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:])).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments answer = merge("[00:00:14.240 --> 00:00:14.420] the\n[00:00:14.420 --> 00:00:14.860] robust\n[00:00:14.860 --> 00:00:15.020] ness\n[00:00:15.020 --> 00:00:15.140] of\n[00:00:15.140 --> 00:00:15.500] neural\n[00:00:15.500 --> 00:00:15.870] networks\n[00:00:15.870 --> 00:00:16.200] .\n[00:00:16.200 --> 00:00:16.410] And\n[00:00:16.410 --> 00:00:16.700] this\n[00:00:16.700 --> 00:00:16.840] is\n[00:00:16.840 --> 00:00:17.200] joint\n") expected = [' the robustness of neural networks.', ' And this is joint'] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 58, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got ['the robust ness of neural networks .'] instead of [' the robustness of neural networks.', ' And this is joint']
Testing if the previous output contains the string All tests passed
: False
Given a parse function like def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta(hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:])).total_seconds() end_seconds = timedelta(hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:])).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments for data like [00:00:14.240 --> 00:00:14.420] the [00:00:14.420 --> 00:00:14.860] robust [00:00:14.860 --> 00:00:15.020] ness [00:00:15.020 --> 00:00:15.140] of [00:00:15.140 --> 00:00:15.500] neural [00:00:15.500 --> 00:00:15.870] networks [00:00:15.870 --> 00:00:16.200] . [00:00:16.200 --> 00:00:16.410] And [00:00:16.410 --> 00:00:16.700] this [00:00:16.700 --> 00:00:16.840] is [00:00:16.840 --> 00:00:17.200] joint write a merge() function that (1) calls parse and (2) merges words into sentences that end in period. Start with the first word and end with the period. Return a list of strings.
Given a parse function like def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta(hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:])).total_seconds() end_seconds = timedelta(hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:])).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments for data like [00:00:14.240 --> 00:00:14.420] the [00:00:14.420 --> 00:00:14.860] robust [00:00:14.860 --> 00:00:15.020] ness [00:00:15.020 --> 00:00:15.140] of [00:00:15.140 --> 00:00:15.500] neural [00:00:15.500 --> 00:00:15.870] networks [00:00:15.870 --> 00:00:16.200] . [00:00:16.200 --> 00:00:16.410] And [00:00:16.410 --> 00:00:16.700] this [00:00:16.700 --> 00:00:16.840] is [00:00:16.840 --> 00:00:17.200] joint write a merge() function that (1) calls parse and (2) merges words into sentences that end in period. Start with the first word and end with the period. Return a list of strings.
Below is one acceptable solution. In this example the merge() function accepts the full transcript (a string), calls parse_transcript() to obtain a list of (start_time, end_time, word) tuples, then loops through these words accumulating them until encountering a word that ends with a period. When such a word is found, it joins the accumulated words into a sentence (taking care to fix spacing before punctuation) and adds it to the list of sentences. You can adjust how you want to treat tokens that are just punctuation. Here’s one implementation: ------------------------------------------------ import re from datetime import timedelta def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]\s+(.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta( hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:]) ).total_seconds() end_seconds = timedelta( hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:]) ).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments def merge(content): segments = parse_transcript(content) sentences = [] current_words = [] for _, _, token in segments: # Remove extra spaces tok = token.strip() if not tok: continue current_words.append(tok) # If the token ends with a period, complete the sentence. # We check either if the token is just a period or if it ends with one. if tok == '.' or tok.endswith('.'): # Join words with a space then remove any space before the period. sentence = " ".join(current_words).replace(" .", ".") sentences.append(sentence) current_words = [] # reset for the next sentence # Optionally, if there is an incomplete sentence left, you can either add it or ignore it. # In this example we ignore any words after the last period. return sentences # Example usage: if __name__ == "__main__": transcript_text = """[00:00:14.240 --> 00:00:14.420] the [00:00:14.420 --> 00:00:14.860] robust [00:00:14.860 --> 00:00:15.020] ness [00:00:15.020 --> 00:00:15.140] of [00:00:15.140 --> 00:00:15.500] neural [00:00:15.500 --> 00:00:15.870] networks [00:00:15.870 --> 00:00:16.200] . [00:00:16.200 --> 00:00:16.410] And [00:00:16.410 --> 00:00:16.700] this [00:00:16.700 --> 00:00:16.840] is [00:00:16.840 --> 00:00:17.200] joint""" merged_sentences = merge(transcript_text) for sentence in merged_sentences: print(sentence) # Expected output: # "the robust ness of neural networks." ------------------------------------------------ Explanation: 1. The parse_transcript() function uses a regular expression to extract the three fields from each line: the start time, end time, and word/text. 2. The merge() function then loops over the parsed segments (ignoring the time stamps) and accumulates each word. 3. When a word ending in a period (or a lone period) is encountered, the words so far are joined into a sentence. The replace(" .", ".") call fixes the extra space that might appear before punctuation. 4. Only complete sentences (those ending with a period) are added to the returned list. This meets the requirements of calling parse and then merging the words into sentences.
I extracted the following code from that output:
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 | import re from datetime import timedelta def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]\s+(.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta( hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:]) ).total_seconds() end_seconds = timedelta( hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:]) ).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments def merge(content): segments = parse_transcript(content) sentences = [] current_words = [] for _, _, token in segments: # Remove extra spaces tok = token.strip() if not tok: continue current_words.append(tok) # If the token ends with a period, complete the sentence. # We check either if the token is just a period or if it ends with one. if tok == '.' or tok.endswith('.'): # Join words with a space then remove any space before the period. sentence = " ".join(current_words).replace(" .", ".") sentences.append(sentence) current_words = [] # reset for the next sentence # Optionally, if there is an incomplete sentence left, you can either add it or ignore it. # In this example we ignore any words after the last period. return sentences |
Running the following program:
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 | import re from datetime import timedelta def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\]\s+(.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta( hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:]) ).total_seconds() end_seconds = timedelta( hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:]) ).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments def merge(content): segments = parse_transcript(content) sentences = [] current_words = [] for _, _, token in segments: # Remove extra spaces tok = token.strip() if not tok: continue current_words.append(tok) # If the token ends with a period, complete the sentence. # We check either if the token is just a period or if it ends with one. if tok == '.' or tok.endswith('.'): # Join words with a space then remove any space before the period. sentence = " ".join(current_words).replace(" .", ".") sentences.append(sentence) current_words = [] # reset for the next sentence # Optionally, if there is an incomplete sentence left, you can either add it or ignore it. # In this example we ignore any words after the last period. return sentences def parse_transcript(content): transcript_segments = [] pattern = r'\[(\d{2}:\d{2}:\d{2}\.\d{3}) --> (\d{2}:\d{2}:\d{2}\.\d{3})\] (.*)' matches = re.findall(pattern, content) for start_time, end_time, text in matches: start_seconds = timedelta(hours=int(start_time[:2]), minutes=int(start_time[3:5]), seconds=int(start_time[6:8]), milliseconds=int(start_time[9:])).total_seconds() end_seconds = timedelta(hours=int(end_time[:2]), minutes=int(end_time[3:5]), seconds=int(end_time[6:8]), milliseconds=int(end_time[9:])).total_seconds() transcript_segments.append((start_seconds, end_seconds, text)) return transcript_segments answer = merge("[00:00:14.240 --> 00:00:14.420] the\n[00:00:14.420 --> 00:00:14.860] robust\n[00:00:14.860 --> 00:00:15.020] ness\n[00:00:15.020 --> 00:00:15.140] of\n[00:00:15.140 --> 00:00:15.500] neural\n[00:00:15.500 --> 00:00:15.870] networks\n[00:00:15.870 --> 00:00:16.200] .\n[00:00:16.200 --> 00:00:16.410] And\n[00:00:16.410 --> 00:00:16.700] this\n[00:00:16.700 --> 00:00:16.840] is\n[00:00:16.840 --> 00:00:17.200] joint\n") expected = [' the robustness of neural networks.', ' And this is joint'] assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' print('All tests passed') |
Traceback (most recent call last):
File "/usr/src/app/main.py", line 67, in <module>
assert answer == expected, f'Wrong answer; got {answer} instead of {expected}'
^^^^^^^^^^^^^^^^^^
AssertionError: Wrong answer; got ['the robust ness of neural networks.'] instead of [' the robustness of neural networks.', ' And this is joint']
Testing if the previous output contains the string All tests passed
: False