def write_detected_issue_summaries()

in python/ts-to-word.py [0:0]


def write_detected_issue_summaries(document, speech_segments):
    """
    Scans the speech segments for any detected issues, and if there are any then a new table is added
    to the document.
    :param document: Word document structure to write the table into
    :param speech_segments: Call transcript structures
    """

    # Scan through the segments and extract the issues
    issues_detected = []
    for turn in speech_segments:
        for issue in turn.segmentIssuesDetected:
            new_issue = {"Speaker": turn.segmentSpeaker}
            new_issue["Timestamp"] = turn.segmentStartTime
            new_issue["Text"] = turn.segmentText[issue["Begin"]:issue["End"]]
            # May need a prefix or suffix for partial text
            if issue["Begin"] > 0:
                new_issue["Text"] = "..." + new_issue["Text"]
            if issue["End"] < len(turn.segmentText):
                new_issue["Text"] = new_issue["Text"] + "..."
            issues_detected.append(new_issue)

    if issues_detected:
        # Start with a new single-column section
        document.add_section(WD_SECTION.CONTINUOUS)
        section_ptr = document.sections[-1]._sectPr
        cols = section_ptr.xpath('./w:cols')[0]
        cols.set(qn('w:num'), '1')
        write_custom_text_header(document, "Issues Detected")

        # Table header information
        table = document.add_table(rows=1, cols=3)
        table.style = document.styles[TABLE_STYLE_STANDARD]
        hdr_cells = table.rows[0].cells
        hdr_cells[0].text = "Speaker"
        hdr_cells[1].text = "Turn Time"
        hdr_cells[2].text = "Detected Text"

        # Output each row
        for issue in issues_detected:
            # First column is the speaker
            row_cells = table.add_row().cells
            row_cells[0].text = issue["Speaker"]
            row_cells[1].text = convert_timestamp(issue["Timestamp"])
            row_cells[2].text = issue["Text"]

        # Formatting transcript table widths
        widths = (Cm(2.2), Cm(2.2), Cm(12.8))
        for row in table.rows:
            for idx, width in enumerate(widths):
                row.cells[idx].width = width

        # Finish with some spacing
        document.add_paragraph()