Text highlighting of text returned by csv.reader is incorrect on first record

I am reading one or more text files, using csv.reader to concatenate the files into a single list, broken down by paragraph. When I display the list in my edit window, text highlighting in the first paragraph of each file is incorrect.

I use spacy.blank() so I can tokenize the text of a paragraph. Each paragraph is saved as a dictionary that contains the spacy doc for that paragraph (text, tokens, etc.) and a currently empty list for annotation labels.

    def merge_data_files(self, flist:list):
        self.nlp = sp.blank("en")
        for file in flist:
            with open(file, 'r', encoding='utf-8') as f:
                reader = csv.reader(f, delimiter='n')
                for row in reader:
                    self.data_df.append({'Doc': self.tokenize(row[0]), 'Labels': []})

    def tokenize(self, text):
        return self.nlp(text) 

In my edit window, I catch the left mouse button release event and manage text highlighting by the cursor. The behaviour I want is: if only a portion of a span of tokens is highlighted, highlighting will expand to the beginning of the first token and the end of the last token in the span. I get this behaviour for every paragraph except the first one of each file.

Event handling code.

    def lftBtnRel(self):
        cursor = self.caseWindow.textCursor()
        if cursor.hasSelection():
            span = self.doc.char_span(cursor.selectionStart(), 
                                      cursor.selectionEnd(), 
                                      alignment_mode="expand")
            print(span, span.start_char, span.end_char)
            cursor.setPosition(span.start_char, QTextCursor.MoveMode.MoveAnchor)
            cursor.movePosition(QTextCursor.MoveOperation.NextCharacter, 
                                QTextCursor.MoveMode.KeepAnchor, 
                                span.end_char - span.start_char)
            self.caseWindow.setTextCursor(cursor)

    def eventFilter(self, obj, event):
        if event.type() == QEvent.Type.MouseButtonRelease:
            if event.button() == Qt.MouseButton.LeftButton:
                self.lftBtnRel()

        return super().eventFilter(obj, event)

This example text demonstrates the problem. This is the first paragraph of one or my text files.

In the first part of this book, and particularly in Chapters 4–7, I have presented the basis of this book’s argument by presenting some of the salient characteristics of my approach, such as taking a long-term ex-ante perspective, learning from the past about the present for the future, using complex systems thinking, etc.

Highlighting ‘4’ in the token ‘4-7’ should highlight the entire token, but nothing gets highlighted. Investigation showed that highlighting the first character of any token results in no highlighting, because the value returned in the span is the preceding space. If any other part of a token is highlighted (say ‘e’ in the token ‘learning’) all but first character of the token is highlighted, with the addition of the following space. That is, highlighting ‘e’ in ‘learning’ results in the span of ‘earning ‘ being selected for highlighting.

Again, this only happens for the first paragraph of each files concatenated in the list, every other paragraph is handled correctly.

I’m at a loss as to whether this is a problem with csv.reader, with spacy and char_span or some combination. Any help or ideas would be greatly appreciated.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật