Skip to content

TableMatch.get_pred_html is not adding spaces between words in a table #37

Description

@asfoorial

The get_pred_html in rapid_doc.model.table.rapid_table_self.table_matcher.main.TableMatch does not add spaces between words in a table cell. Below is a fix, hopefully someone will merge it with the code.

def get_pred_html(
self,
pred_structures: List[str],
matched_index: Dict[int, List[int]],
ocr_contents: List[Tuple[str, float]],
):

    end_html = []
    td_index = 0
    for tag in pred_structures:
        if "</td>" not in tag:
            end_html.append(tag)
            continue

        if "<td></td>" == tag:
            end_html.extend("<td>")

        if td_index in matched_index.keys():
            b_with = False
            if (
                "<b>" in ocr_contents[matched_index[td_index][0]]
                and len(matched_index[td_index]) > 1
            ):
                b_with = True
                end_html.extend("<b>")

            contents = [] # Bug fixing line
            for i, td_index_index in enumerate(matched_index[td_index]):
                content = ocr_contents[td_index_index][0]
                if len(matched_index[td_index]) > 1:
                    if len(content) == 0:
                        continue

                    
                    if content[0] == " ":
                        content = content[1:]

                    if "<b>" in content:
                        content = content[3:]

                    if "</b>" in content:
                        content = content[:-4]
                    

                    if len(content) == 0:
                        continue

                    if i != len(matched_index[td_index]) - 1 and " " != content[-1]:
                        # content += " " #用字符级别的框,每个字符都会多空格
                        content += ""
                contents.append(content) # Bug fixing line
                # end_html.extend(content) #Bug fixing removed line
            end_html.extend(' '.join(contents)) # Bug fixing line

            if b_with:
                end_html.extend("</b>")

        if "<td></td>" == tag:
            end_html.append("</td>")
        else:
            end_html.append(tag)

        td_index += 1

    # Filter <thead></thead><tbody></tbody> elements
    filter_elements = ["<thead>", "</thead>", "<tbody>", "</tbody>"]
    end_html = [v for v in end_html if v not in filter_elements]
    return "".join(end_html), end_html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions