Skip to content

Add scoring bonus for matches toward the end of path #2

@SakolB

Description

@SakolB

Description

Currently, the FZF v2 scoring algorithm gives bonuses for:

  • First character match (bonusFirstChar)
  • Path separator match (bonusPathSeparator)
  • Consecutive matches (bonusConsecutive)

However, there's no bonus for matches that occur toward the end/right side of the path. Matches closer to the end of the path should be scored higher, as they typically match more specific parts of the directory name.

Rationale

Given a query like "test":

  • /project/old/test - match at position 12 (end)
  • /test/old/project - match at position 1 (beginning)

The first path should score higher because the match is closer to the actual directory name rather than an ancestor directory.

Expected Behavior

Matches occurring toward the right/end of the path should receive a scoring bonus proportional to their position. This gives preference to:

  1. Matches in the filename/directory name itself
  2. Matches in recent path components
  3. Matches closer to the target rather than in ancestor directories

Current Behavior

All matches are scored equally based on:

  • Base match score
  • Position-based bonuses (first char, path separator)
  • Consecutive match bonus
  • Gap penalties

But no consideration for how far right the match occurs in the path.

Proposed Solution

Add a position-based bonus in the scoring algorithm:

// In the constants section (around line 10-18):
const bonusEndPosition = 8

// In the DP loop where bonuses are calculated (after line 223):
// Calculate position bonus - matches toward the end get higher scores
positionBonus := (j * bonusEndPosition) / tgLen
bonus += positionBonus

This would reward matches that occur in the latter portion of the path.

How it works:

  • Character at position 1: gets bonus ≈ 0
  • Character at mid-point: gets bonus ≈ bonusEndPosition/2 = 4
  • Character at end: gets bonus = bonusEndPosition = 8

Example:
For query "test" in paths:

  • /project/old/test - characters match at positions ~13-16, each gets high bonus
  • /test/old/project - characters match at positions ~1-4, each gets low bonus

The path with the match toward the end accumulates a higher total score.

Implementation Location

  • internal/ranker/ranker.go - in the score() function
  • Specifically in the FZF v2 DP algorithm where bonuses are calculated (around line 216-223)

Considerations

  • The bonus should be subtle enough not to override other important factors (consecutive matches, path separators)
  • May need to tune the bonusEndPosition constant value through testing
  • Should integrate smoothly with existing scoring logic
  • Scaling options (start with linear, can adjust later):
    • Linear (proposed): (j * bonusEndPosition) / tgLen - balanced approach
    • Quadratic: (j * j * bonusEndPosition) / (tgLen * tgLen) - strongly favors end
    • Threshold: Only give bonus if j > tgLen * 2/3 - bonus only for last third

Acceptance Criteria

  • Matches toward the end of paths receive higher scores
  • The bonus scales proportionally with position (further right = higher bonus)
  • Existing scoring behavior (path separators, consecutive matches) is preserved
  • Manual testing shows improved ranking for end-of-path matches
  • No performance degradation in the scoring algorithm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions