Skip to content

⚡ Bolt: Optimize regex matching in keyword density analysis#229

Open
anchapin wants to merge 1 commit intomainfrom
bolt-keyword-density-optimization-15327457375750365719
Open

⚡ Bolt: Optimize regex matching in keyword density analysis#229
anchapin wants to merge 1 commit intomainfrom
bolt-keyword-density-optimization-15327457375750365719

Conversation

@anchapin
Copy link
Copy Markdown
Owner

@anchapin anchapin commented Apr 4, 2026

💡 What:

  • Extracted title_patterns and company_patterns to module-level pre-compiled regex objects (_TITLE_PATTERNS, _COMPANY_PATTERNS) in cli/utils/keyword_density.py.
  • Optimized the _count_keywords_in_resume method to avoid using the re.IGNORECASE flag inside a tight loop with re.findall. Now, it lowercases the entire resume text once and lowercases each keyword string before matching.

🎯 Why:
re.compile within hot code paths and particularly re.IGNORECASE evaluation inside text-search loops introduces significant performance overhead, especially as the text length or the number of keywords increases.

📊 Impact:

  • Eliminates repeated compilation of title and company regex patterns.
  • Testing indicates ~5% speed improvement on keyword counting logic loops for large blocks of text simply by removing re.IGNORECASE in favor of pre-lowercasing the target strings.

🔬 Measurement:

  • Run python -m pytest tests/test_keyword_density.py to ensure all keyword matching, job parsing, and density analysis logic operates exactly as before without regressions.

PR created automatically by Jules for task 15327457375750365719 started by @anchapin

Summary by Sourcery

Optimize keyword density analysis by precompiling regex patterns and reducing per-call regex overhead in resume and job parsing.

Enhancements:

  • Precompile job title and company regex patterns at the module level for reuse in job detail extraction.
  • Lowercase resume text and keywords once before matching to avoid repeated case-insensitive regex operations in keyword counting.

- Extracted `_TITLE_PATTERNS` and `_COMPANY_PATTERNS` to module-level pre-compiled regex objects in `cli/utils/keyword_density.py` to eliminate recompilation overhead on each method call.
- Optimized keyword counting in `_count_keywords_in_resume` by avoiding `re.IGNORECASE` in `re.findall`. Instead, lowercased the `all_text` blob once and lowercased each keyword string beforehand.

Co-authored-by: anchapin <6326294+anchapin@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 4, 2026

Reviewer's Guide

Optimizes keyword density analysis performance by pre-compiling job title/company regex patterns and switching keyword counting to operate on lowercased text instead of using case-insensitive regex flags in tight loops.

Class diagram for optimized keyword density analysis and regex patterns

classDiagram
    class KeywordDensityModule {
        +_TITLE_PATTERNS: list
        +_COMPANY_PATTERNS: list
    }

    class KeywordDensityAnalyzer {
        -_extract_job_details(job_description: str) Tuple_str_str
        -_count_keywords_in_resume(resume_data: ResumeYAML, keywords: List_Tuple_str_int) Dict_str_int
        -_get_all_text(resume_data: ResumeYAML) str
    }

    KeywordDensityModule <.. KeywordDensityAnalyzer : uses

    class _TITLE_PATTERNS {
        +pattern_1: Pattern  %% (?:job title|position|title):\s*([^\n]+) with IGNORECASE MULTILINE
        +pattern_2: Pattern  %% ^([^\n]+)\s*[-|]\s*[^|]+$ with IGNORECASE MULTILINE
        +pattern_3: Pattern  %% #\s*([^\n]+) with IGNORECASE MULTILINE
    }

    class _COMPANY_PATTERNS {
        +pattern_1: Pattern  %% (?:company|organization):\s*([^\n]+) with IGNORECASE
        +pattern_2: Pattern  %% (?:at|from)\s+([A-Z][^\n]+?)(?:\s+[-\u2014]|\s+$) with IGNORECASE
    }

    KeywordDensityModule o-- _TITLE_PATTERNS
    KeywordDensityModule o-- _COMPANY_PATTERNS
    KeywordDensityAnalyzer --> _TITLE_PATTERNS : search(job_description)
    KeywordDensityAnalyzer --> _COMPANY_PATTERNS : search(job_description)
Loading

File-Level Changes

Change Details Files
Pre-compile and reuse job title and company extraction regex patterns instead of creating them on each call.
  • Introduce module-level _TITLE_PATTERNS list of compiled regexes with IGNORECASE and MULTILINE flags for job title extraction.
  • Introduce module-level _COMPANY_PATTERNS list of compiled regexes with IGNORECASE flag for company extraction.
  • Update _extract_job_details to iterate over pre-compiled patterns and use pattern.search(job_description) instead of re.search with flags.
cli/utils/keyword_density.py
Optimize keyword counting logic to avoid case-insensitive regex overhead in tight loops.
  • Lowercase the full resume text once via _get_all_text(resume_data).lower().
  • Lowercase each keyword before matching while preserving the original keyword as the dict key.
  • Use re.findall without IGNORECASE on the lowercased text and keyword to count whole-word occurrences.
cli/utils/keyword_density.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant