tokenizer: `position_factor` mixes character-length with token-index · Issue #3811 · ModelEngine-Group/nexent · GitHub
Skip to content

tokenizer: position_factor mixes character-length with token-index #3811

Open

Description

@ngo-software

The position-weighting heuristic in sdk/nexent/core/nlp/tokenizer.py:52 compares a token index (idx) against one-third of the input character length:

position_factor = 1.2 if idx < 3 or idx > len(text) / 3 else 1.0

The intent — based on the comment one line above — is "words at the beginning and end of the sentence are more important". But len(text) / 3 is computed against the raw character string, not the number of tokens. For any reasonable Chinese query of more than ~10 characters, len(text)/3 is already larger than the actual number of tokens, so the idx > len(text)/3 branch never fires and only the first three tokens get the boost. For short queries the opposite happens: nearly every token gets boosted.

Repro

from nexent.core.nlp.tokenizer import calculate_term_weights
# 30-char sentence with ~10 tokens; len(text)/3 == 10 �� "end-of-sentence" boost never applies
print(calculate_term_weights("人工智能在医疗影像识别中的应用与前景分析"))

Expected

position_factor should be a function of position within the token stream, e.g.:

n_tokens_estimate = max(1, len(text) // 2)  # or pre-tokenize first
position_factor = 1.2 if idx < 3 or idx >= n_tokens_estimate - 3 else 1.0

A cleaner fix is to materialize pseg.cut(text) into a list first and use len(words) as the denominator.

Severity

Medium — search ranking quality is degraded but the function still returns something usable.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions