Python: extract keywords from non-English text for topic selection#7130
Open
he-yufeng wants to merge 1 commit into
Open
Python: extract keywords from non-English text for topic selection#7130he-yufeng wants to merge 1 commit into
he-yufeng wants to merge 1 commit into
Conversation
_WORD_PATTERN matched only ASCII (`[a-z0-9]...`), so a message written in CJK, Cyrillic or any other non-Latin script produced an empty keyword set. _select_topics returns early on an empty keyword set, so non-English users never had memory topic files loaded automatically. Make the pattern Unicode-aware (`[^\W_][\w-]+`, a letter/digit start plus word chars/hyphen), which is the exact Unicode generalization of the old pattern: English tokenization is unchanged and CJK/Cyrillic text now yields keywords.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes memory topic auto-loading for non-English user messages by making keyword extraction Unicode-aware in the Python harness memory subsystem.
Changes:
- Update
_WORD_PATTERNto match 2+ character “word-like” tokens across Unicode scripts (CJK/Cyrillic/etc.), preventing empty keyword sets for non-Latin input. - Add a regression test ensuring non-English keyword extraction works and that basic English extraction remains unchanged.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/packages/core/agent_framework/_harness/_memory.py |
Replaces the ASCII-only keyword regex with a Unicode-aware pattern so non-English messages produce keywords for topic selection. |
python/packages/core/tests/core/test_harness_memory.py |
Adds a regression test covering CJK and Cyrillic keyword extraction plus a simple English invariance check. |
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.
Motivation and Context
_WORD_PATTERNin_harness/_memory.pywasre.compile(r"[a-z0-9][a-z0-9_-]{1,}", flags=re.IGNORECASE)— ASCII-only._extract_keywordsruns it over the input messages, and_select_topicsreturns early when the keyword set is empty. So a message written in CJK, Cyrillic, or any other non-Latin script yields no keywords, and non-English users never get their memory topic files auto-loaded.Description
Make the pattern Unicode-aware:
re.compile(r"[^\W_][\w-]+").[^\W_]is a Unicode letter or digit (excluding underscore) and[\w-]is a word char or hyphen, so this is the exact Unicode generalization of the old pattern — English tokenization is byte-for-byte unchanged, and CJK/Cyrillic/etc. text now produces keywords. Verified the invariant directly:Related Issue
Fixes #6989
Contribution Checklist
pytest test_harness_memory.py -k non_english, ruff format + check clean)