Fix: remove spurious spaces between CJK characters when compressing (#131) - #255
Open
Beytab wants to merge 1 commit into
Open
Fix: remove spurious spaces between CJK characters when compressing (#131)#255Beytab wants to merge 1 commit into
Beytab wants to merge 1 commit into
Conversation
Author
|
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #131. When the input is Chinese (or other CJK text), the compressed output contains many spurious spaces between characters.
Root cause
In
PromptCompressor,tokenizer.convert_tokens_to_string(keep_words)joins words with ASCII spaces — an English word-boundary convention. CJK scripts have no word delimiter, so this inserts spurious spaces between CJK characters (and around CJK punctuation).Fix
Add a
remove_space_between_cjkhelper inllmlingua/utils.pythat strips an ASCII space sitting between two CJK characters, and apply it tokeep_strinprompt_compressor.py.The regex covers CJK ideographs (incl. Extension A / compatibility), Japanese kana, Korean hangul, and CJK/fullwidth punctuation blocks, so spaces around CJK punctuation (e.g.
,。:、!?) are removed too. It only removes a space when both neighbors are CJK, so English text and mixed CJK/ASCII spacing are left intact.Testing
Adds
tests/test_llmlingua2_chinese.pycovering the CJK space-removal behavior.