Initial Query

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.

LLM Generation

Query

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.

Output

Certainly! Below is the implementation of the `merge()` function based on your requirements. This function will: 1. **Call `parse_transcript(content)`** to parse the transcript into segments. 2. **Merge the words into sentences** that end with a period (`.`). 3. **Return a list of sentences**. Here's the complete code with detailed comments for clarity:

 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import re
from datetime import timedelta

def parse_transcript(content):
    """
    Parses the transcript content into a list of segments.

    Each segment is a tuple containing:
    (start_seconds, end_seconds, text)
    """
    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:
        # Convert start_time and end_time to total seconds
        start_parts = start_time.split(':')
        end_parts = end_time.split(':')
        
        start_seconds = (int(start_parts[0]) * 3600 +
                         int(start_parts[1]) * 60 +
                         float(start_parts[2]))
        end_seconds = (int(end_parts[0]) * 3600 +
                       int(end_parts[1]) * 60 +
                       float(end_parts[2]))
        
        transcript_segments.append((start_seconds, end_seconds, text.strip()))
    
    return transcript_segments

def merge(content):
    """
    Merges words from the transcript into sentences that end with a period.

    Args:
        content (str): The raw transcript content.

    Returns:
        List[str]: A list of merged sentences.
    """
    # Parse the transcript into segments
    segments = parse_transcript(content)
    
    sentences = []           # List to store the final sentences
    current_sentence = []    # Temporary list to accumulate words
    
    for _, _, text in segments:
        stripped_text = text.strip()
        
        if stripped_text == '.':
            # If the current segment is a period, finalize the current sentence
            if current_sentence:
                sentence = ' '.join(current_sentence) + '.'
                sentences.append(sentence)
                current_sentence = []  # Reset for the next sentence
        else:
            # Append the word to the current sentence
            current_sentence.append(stripped_text)
    
    # Optionally, handle any remaining words that don't end with a period
    if current_sentence:
        # You can choose to append or ignore this last partial sentence
        # Here, we'll append it without adding a period
        sentence = ' '.join(current_sentence)
        sentences.append(sentence)
    
    return sentences

# Example Usage
if __name__ == "__main__":
    transcript_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(transcript_content)
    for sentence in merged_sentences:
        print(sentence)
### Explanation of the `merge()` Function: 1. **Parsing the Transcript**: - The function starts by calling `parse_transcript(content)` to convert the raw transcript into structured segments. - Each segment contains the start time, end time (both in seconds), and the text. 2. **Merging Words into Sentences**: - It initializes two lists: `sentences` to store the final sentences and `current_sentence` to accumulate words. - It iterates through each segment: - **If the text is a period (`.`)**: - It joins the accumulated words in `current_sentence` with spaces. - Appends a period at the end to form a complete sentence. - Adds this sentence to the `sentences` list. - Resets `current_sentence` for the next sentence. - **If the text is a regular word**: - It appends the word to `current_sentence`. - **After the loop**, it checks if there are any remaining words in `current_sentence` that didn't end with a period and appends them as the last sentence. 3. **Output**: - The function returns a list of merged sentences. ### Sample Output for the Provided Example:
1
2
the robust ness of neural networks.
And this is joint
In this example: - The first sentence ends with a period (`.`), so it's properly merged and included. - The second set of words `"And this is joint"` doesn't end with a period, but it's still included as the last sentence. If you prefer to exclude such incomplete sentences, you can modify the function accordingly. ### Optional Enhancements: - **Handling Multiple Sentences**: If your transcript contains multiple sentences separated by periods, the `merge()` function will correctly split them into separate entries in the `sentences` list. - **Punctuation and Capitalization**: Depending on the complexity of the transcript, you might want to handle other punctuation marks or ensure proper capitalization at the start of each sentence. Feel free to adjust the function based on the specific nuances of your transcript data!

Extract Code

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
from datetime import timedelta

def parse_transcript(content):
    """
    Parses the transcript content into a list of segments.

    Each segment is a tuple containing:
    (start_seconds, end_seconds, text)
    """
    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:
        # Convert start_time and end_time to total seconds
        start_parts = start_time.split(':')
        end_parts = end_time.split(':')
        
        start_seconds = (int(start_parts[0]) * 3600 +
                         int(start_parts[1]) * 60 +
                         float(start_parts[2]))
        end_seconds = (int(end_parts[0]) * 3600 +
                       int(end_parts[1]) * 60 +
                       float(end_parts[2]))
        
        transcript_segments.append((start_seconds, end_seconds, text.strip()))
    
    return transcript_segments

def merge(content):
    """
    Merges words from the transcript into sentences that end with a period.

    Args:
        content (str): The raw transcript content.

    Returns:
        List[str]: A list of merged sentences.
    """
    # Parse the transcript into segments
    segments = parse_transcript(content)
    
    sentences = []           # List to store the final sentences
    current_sentence = []    # Temporary list to accumulate words
    
    for _, _, text in segments:
        stripped_text = text.strip()
        
        if stripped_text == '.':
            # If the current segment is a period, finalize the current sentence
            if current_sentence:
                sentence = ' '.join(current_sentence) + '.'
                sentences.append(sentence)
                current_sentence = []  # Reset for the next sentence
        else:
            # Append the word to the current sentence
            current_sentence.append(stripped_text)
    
    # Optionally, handle any remaining words that don't end with a period
    if current_sentence:
        # You can choose to append or ignore this last partial sentence
        # Here, we'll append it without adding a period
        sentence = ' '.join(current_sentence)
        sentences.append(sentence)
    
    return sentences

Run Code Interpreter

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import re
from datetime import timedelta

def parse_transcript(content):
    """
    Parses the transcript content into a list of segments.

    Each segment is a tuple containing:
    (start_seconds, end_seconds, text)
    """
    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:
        # Convert start_time and end_time to total seconds
        start_parts = start_time.split(':')
        end_parts = end_time.split(':')
        
        start_seconds = (int(start_parts[0]) * 3600 +
                         int(start_parts[1]) * 60 +
                         float(start_parts[2]))
        end_seconds = (int(end_parts[0]) * 3600 +
                       int(end_parts[1]) * 60 +
                       float(end_parts[2]))
        
        transcript_segments.append((start_seconds, end_seconds, text.strip()))
    
    return transcript_segments

def merge(content):
    """
    Merges words from the transcript into sentences that end with a period.

    Args:
        content (str): The raw transcript content.

    Returns:
        List[str]: A list of merged sentences.
    """
    # Parse the transcript into segments
    segments = parse_transcript(content)
    
    sentences = []           # List to store the final sentences
    current_sentence = []    # Temporary list to accumulate words
    
    for _, _, text in segments:
        stripped_text = text.strip()
        
        if stripped_text == '.':
            # If the current segment is a period, finalize the current sentence
            if current_sentence:
                sentence = ' '.join(current_sentence) + '.'
                sentences.append(sentence)
                current_sentence = []  # Reset for the next sentence
        else:
            # Append the word to the current sentence
            current_sentence.append(stripped_text)
    
    # Optionally, handle any remaining words that don't end with a period
    if current_sentence:
        # You can choose to append or ignore this last partial sentence
        # Here, we'll append it without adding a period
        sentence = ' '.join(current_sentence)
        sentences.append(sentence)
    
    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')
And got the output: Traceback (most recent call last): File "/usr/src/app/main.py", line 88, in <module> assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' AssertionError: Wrong answer; got ['the robust ness of neural networks.', 'And this is joint'] instead of [' the robustness of neural networks.', ' And this is joint']

Substring Evaluation

Testing if the previous output contains the string All tests passed: False

Initial Query

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.

LLM Generation

Query

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.

Output

Sure! You can create a `merge()` function that utilizes the `parse_transcript` function to consolidate words into complete sentences ending with a period. Below is the implementation of the `merge()` function along with an example of how to use it.

 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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:
        # Split the start_time and end_time into components
        start_hours, start_minutes, start_rest = start_time.split(':')
        start_seconds, start_milliseconds = start_rest.split('.')
        end_hours, end_minutes, end_rest = end_time.split(':')
        end_seconds, end_milliseconds = end_rest.split('.')

        # Convert start and end times to total seconds
        start_td = timedelta(
            hours=int(start_hours),
            minutes=int(start_minutes),
            seconds=int(start_seconds),
            milliseconds=int(start_milliseconds)
        )
        end_td = timedelta(
            hours=int(end_hours),
            minutes=int(end_minutes),
            seconds=int(end_seconds),
            milliseconds=int(end_milliseconds)
        )

        start_seconds_total = start_td.total_seconds()
        end_seconds_total = end_td.total_seconds()

        transcript_segments.append((start_seconds_total, end_seconds_total, text.strip()))

    return transcript_segments

def merge(content):
    """
    Parses the transcript content and merges words into complete sentences ending with a period.

    Args:
        content (str): The transcript content as a string.

    Returns:
        List[str]: A list of complete sentences.
    """
    segments = parse_transcript(content)
    sentences = []
    current_sentence = []

    for _, _, text in segments:
        word = text.strip()
        if word == '.':
            if current_sentence:
                # Join the words and add a period at the end
                sentence = ' '.join(current_sentence) + '.'
                sentences.append(sentence)
                current_sentence = []
        else:
            current_sentence.append(word)

    # If there's any remaining words without a ending period, add them as a sentence
    if current_sentence:
        sentence = ' '.join(current_sentence)
        sentences.append(sentence)

    return sentences

# Example Usage
if __name__ == "__main__":
    transcript_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
    """

    sentences = merge(transcript_content)
    for idx, sentence in enumerate(sentences, 1):
        print(f"Sentence {idx}: {sentence}")
### Explanation of the `merge()` Function 1. **Parsing the Transcript:** - The `merge()` function first calls `parse_transcript(content)` to obtain a list of tuples, each containing the start time, end time, and the word/text from the transcript. 2. **Merging Words into Sentences:** - It initializes two empty lists: `sentences` to store the final sentences and `current_sentence` to accumulate words for the current sentence. - It iterates over each segment in the parsed transcript: - **If the word is a period (`.`):** - It joins the accumulated words in `current_sentence` with spaces and appends a period at the end to form a complete sentence. - This sentence is then added to the `sentences` list. - The `current_sentence` list is reset for the next sentence. - **If the word is not a period:** - It appends the word to the `current_sentence` list. 3. **Handling the Last Sentence:** - After iterating through all segments, if there are any remaining words in `current_sentence` that do not end with a period, they are joined and added as the last sentence. ### Output of the Example When you run the provided example, the output will be:
1
2
Sentence 1: the robust ness of neural networks.
Sentence 2: And this is joint
This demonstrates that the `merge()` function successfully combines individual words into complete sentences based on the period delimiter.

Extract Code

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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:
        # Split the start_time and end_time into components
        start_hours, start_minutes, start_rest = start_time.split(':')
        start_seconds, start_milliseconds = start_rest.split('.')
        end_hours, end_minutes, end_rest = end_time.split(':')
        end_seconds, end_milliseconds = end_rest.split('.')

        # Convert start and end times to total seconds
        start_td = timedelta(
            hours=int(start_hours),
            minutes=int(start_minutes),
            seconds=int(start_seconds),
            milliseconds=int(start_milliseconds)
        )
        end_td = timedelta(
            hours=int(end_hours),
            minutes=int(end_minutes),
            seconds=int(end_seconds),
            milliseconds=int(end_milliseconds)
        )

        start_seconds_total = start_td.total_seconds()
        end_seconds_total = end_td.total_seconds()

        transcript_segments.append((start_seconds_total, end_seconds_total, text.strip()))

    return transcript_segments

def merge(content):
    """
    Parses the transcript content and merges words into complete sentences ending with a period.

    Args:
        content (str): The transcript content as a string.

    Returns:
        List[str]: A list of complete sentences.
    """
    segments = parse_transcript(content)
    sentences = []
    current_sentence = []

    for _, _, text in segments:
        word = text.strip()
        if word == '.':
            if current_sentence:
                # Join the words and add a period at the end
                sentence = ' '.join(current_sentence) + '.'
                sentences.append(sentence)
                current_sentence = []
        else:
            current_sentence.append(word)

    # If there's any remaining words without a ending period, add them as a sentence
    if current_sentence:
        sentence = ' '.join(current_sentence)
        sentences.append(sentence)

    return sentences

Run Code Interpreter

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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:
        # Split the start_time and end_time into components
        start_hours, start_minutes, start_rest = start_time.split(':')
        start_seconds, start_milliseconds = start_rest.split('.')
        end_hours, end_minutes, end_rest = end_time.split(':')
        end_seconds, end_milliseconds = end_rest.split('.')

        # Convert start and end times to total seconds
        start_td = timedelta(
            hours=int(start_hours),
            minutes=int(start_minutes),
            seconds=int(start_seconds),
            milliseconds=int(start_milliseconds)
        )
        end_td = timedelta(
            hours=int(end_hours),
            minutes=int(end_minutes),
            seconds=int(end_seconds),
            milliseconds=int(end_milliseconds)
        )

        start_seconds_total = start_td.total_seconds()
        end_seconds_total = end_td.total_seconds()

        transcript_segments.append((start_seconds_total, end_seconds_total, text.strip()))

    return transcript_segments

def merge(content):
    """
    Parses the transcript content and merges words into complete sentences ending with a period.

    Args:
        content (str): The transcript content as a string.

    Returns:
        List[str]: A list of complete sentences.
    """
    segments = parse_transcript(content)
    sentences = []
    current_sentence = []

    for _, _, text in segments:
        word = text.strip()
        if word == '.':
            if current_sentence:
                # Join the words and add a period at the end
                sentence = ' '.join(current_sentence) + '.'
                sentences.append(sentence)
                current_sentence = []
        else:
            current_sentence.append(word)

    # If there's any remaining words without a ending period, add them as a sentence
    if current_sentence:
        sentence = ' '.join(current_sentence)
        sentences.append(sentence)

    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')
And got the output: Traceback (most recent call last): File "/usr/src/app/main.py", line 88, in <module> assert answer == expected, f'Wrong answer; got {answer} instead of {expected}' AssertionError: Wrong answer; got ['the robust ness of neural networks.', 'And this is joint'] instead of [' the robustness of neural networks.', ' And this is joint']

Substring Evaluation

Testing if the previous output contains the string All tests passed: False