Skip to content

Bug: Unicode 大小寫轉換導致 span 位置錯誤 #2

Description

@HCYT

來源

問題描述

當文字包含某些 Unicode 字符(如土耳其語 İ)時,大小寫轉換會改變字串長度,導致 span_info 返回錯誤的位置。

重現步驟

from flashtext import KeywordProcessor

kp = KeywordProcessor(case_sensitive=False)
kp.add_keyword('Bay Area')

text = 'İ I love big Apple and Bay Area.'  # 土耳其語 İ

keywords_found = kp.extract_keywords(text, span_info=True)
# 返回: ('Bay Area', 24, 32)
# 實際: text[24:32] = 'ay Area.'  ❌ 位置偏移了

原因分析

len("İ")        # 1
len("İ".lower())  # 2 (變成 "i̇")

大小寫轉換改變了字串長度,但 span 計算沒有考慮這點。

相關 PR

PR#82 提供了修復方案:逐字符進行大小寫轉換以保持位置對應。

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions