fix(memory): match a Latin word embedded in unspaced-script text - #6773
Open
LHMQ878 wants to merge 1 commit into
Open
fix(memory): match a Latin word embedded in unspaced-script text#6773LHMQ878 wants to merge 1 commit into
LHMQ878 wants to merge 1 commit into
Conversation
InMemoryMemoryService tokenizes an event with \w+, which cannot split a script that writes without spaces, so 私はPythonでADKを使っています comes back as a single token. A non-Latin query word is matched as a substring of the event text, but a Latin one is matched only against whole tokens, so searching that event for "Python" or "ADK" returned nothing. An event token that mixes scripts is now also indexed by its ASCII and non-ASCII runs, which makes the embedded Latin word a token of its own while still keeping a partial word such as "thon" from matching. Related: google#5501
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.
Link to Issue or Description of Change
Related: #5501 (same code path, the Latin-word half of the problem was left open)
Problem:
InMemoryMemoryService.search_memorytokenizes an event withre.findall(r'\w+', text). Japanese and Chinese are written without spaces, so a whole sentence comes back as a single token:私はPythonでADKを使っていますtokenizes to{'私はpythonでadkを使っています'}.The Unicode fix in b8ea1e8 handles a non-Latin query by also matching it as a substring of the event text, but a Latin query word is still only matched against whole tokens. So a Latin word embedded in unspaced-script text is unreachable — which is exactly how Japanese/Chinese users write about code, product names and identifiers:
Solution:
Index an event token that mixes scripts by its ASCII and non-ASCII runs as well as by the token itself (new
_extract_searchable_words), so私はpythonでadkを使っていますalso yields私は,python,で,adk,を使っています.This makes the embedded Latin word a token of its own and is matched exactly, so the partial-word guard the existing tests assert (
'thon'must not matchPython) still holds — a plain "substring-match Latin words too" fallback would have broken it. Pure-ASCII text produces the same token set as before, so ranking and result bounding are unchanged. Only the event side is affected; query tokenization is untouched, so the match counts used for ranking stay one-per-query-word.Testing Plan
Unit Tests:
Added six cases to the existing
test_search_memory_non_latinparametrization — three that fail onmainand three guards:私はPythonでADKを使っていますPython私はPythonでADKを使っていますadk我用Python写代码python私はPythonでADKを使っています使って私はPythonでADKを使っていますJava私はPythonでADKを使っていますthonOn
main, the three new positives fail:With the change:
pre-commit run --files <changed files>passes (isort, pyink, ruff, addlicense, codespell, ADK compliance checks).Manual End-to-End (E2E) Tests:
Same store, searched before and after the change:
PythonADK使ってthonJavaChecklist