🔍 Code Extractor

function get_adjusted_top_k

Maturity: 55

Calculates an adjusted top_k value for multi-language search operations by multiplying the base value by the number of languages to ensure sufficient results per language.

File:
/tf/active/vicechatdev/docchat/config.py
Lines:
171 - 189
Complexity:
simple

Purpose

This function is designed to scale search result limits in multi-language contexts. When searching across multiple languages, it multiplies the base top_k parameter by the number of languages to maintain consistent result quality per language. For single-language searches, it returns the base value unchanged. This ensures that when results are distributed across languages, each language still receives an adequate number of results.

Source Code

def get_adjusted_top_k(base_top_k: int, num_languages: int = None) -> int:
    """
    Calculate adjusted top_k based on number of languages
    
    Args:
        base_top_k: Base number of results desired
        num_languages: Number of languages to search (default: SUPPORTED_LANGUAGES)
    
    Returns:
        Adjusted top_k for multi-language search
    """
    if num_languages is None:
        num_languages = len(SUPPORTED_LANGUAGES)
    
    # For multi-language search, multiply by number of languages
    # to get sufficient results in each language
    if num_languages > 1:
        return base_top_k * num_languages
    return base_top_k

Parameters

Name Type Default Kind
base_top_k int - positional_or_keyword
num_languages int None positional_or_keyword

Parameter Details

base_top_k: The base number of search results desired. This represents the target number of results you want per language or in total for a single language. Must be a positive integer. Example: if you want 10 results per language, pass 10.

num_languages: Optional parameter specifying the number of languages to search across. If None (default), the function uses the length of SUPPORTED_LANGUAGES constant from the module. Must be a positive integer if provided. For single-language searches, pass 1 to return the base_top_k unchanged.

Return Value

Type: int

Returns an integer representing the adjusted top_k value. If num_languages > 1, returns base_top_k multiplied by num_languages. If num_languages == 1, returns base_top_k unchanged. The returned value is used as the limit for search queries to ensure adequate results across all languages.

Usage Example

# Assuming SUPPORTED_LANGUAGES = ['en', 'es', 'fr'] is defined in module

# Example 1: Auto-detect number of languages from SUPPORTED_LANGUAGES
adjusted_k = get_adjusted_top_k(base_top_k=10)
# Returns: 30 (10 * 3 languages)

# Example 2: Explicitly specify number of languages
adjusted_k = get_adjusted_top_k(base_top_k=5, num_languages=4)
# Returns: 20 (5 * 4 languages)

# Example 3: Single language search
adjusted_k = get_adjusted_top_k(base_top_k=15, num_languages=1)
# Returns: 15 (no multiplication for single language)

# Example 4: Use in a search query
base_results = 10
languages_to_search = ['en', 'de', 'ja']
top_k = get_adjusted_top_k(base_results, len(languages_to_search))
# top_k = 30, ensuring ~10 results per language

Best Practices

  • Always ensure base_top_k is a positive integer to avoid unexpected results
  • When using default num_languages (None), ensure SUPPORTED_LANGUAGES is properly defined in the module scope
  • For single-language searches, explicitly pass num_languages=1 to avoid unnecessary multiplication
  • Consider the performance implications of large top_k values when searching across many languages
  • Use this function consistently across your codebase to maintain uniform result distribution across languages
  • Document the expected SUPPORTED_LANGUAGES constant format if relying on default behavior

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_adjusted_topk 76.3% similar

    A test function that validates the adjusted top_k calculation by testing multiple base values against the number of supported languages and logging the results.

    From: /tf/active/vicechatdev/docchat/test_multilanguage.py
  • function test_language_detection_and_translation 38.2% similar

    A test function that validates multi-language query processing capabilities including language detection, translation, and query expansion across multiple supported languages.

    From: /tf/active/vicechatdev/docchat/test_multilanguage.py
  • function get_all_dbo_texttranslations 31.1% similar

    Retrieves all nodes labeled as dbo_TextTranslations from a Neo4j graph database with a configurable limit on the number of results returned.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_all_parameter_medicationtypes 28.8% similar

    Retrieves Parameter_MedicationTypes nodes from a Neo4j graph database with a configurable limit on the number of results returned.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function extract_total_references 27.2% similar

    Extracts the total count of references from markdown-formatted content by first checking for a header line with the total, then falling back to manually counting reference entries.

    From: /tf/active/vicechatdev/enhanced_word_converter_fixed.py
← Back to Browse