Skip to content

get_dependencies

Loop through all functions in the API Parse and preprocess each doc string from every function Extract and return all dependencies as a dict with function and parameter names as keys

Source code in library_analyzer/processing/dependencies/_get_dependency.py
def get_dependencies(api: API) -> APIDependencies:
    """
    Loop through all functions in the API
    Parse and preprocess each doc string from every function
    Extract and return all dependencies as a dict with function and parameter names as keys
    """
    nlp = spacy.load(PIPELINE)

    matcher = DependencyMatcher(nlp.vocab)
    spacy_id_to_pattern_id_mapping: Dict = dict()
    for pattern_id, pattern in dependency_matcher_patterns.items():
        matcher.add(pattern_id, [pattern])
        spacy_id_to_pattern_id_mapping[nlp.vocab.strings[pattern_id]] = pattern_id
    all_dependencies: Dict = dict()
    endpoint_functions = api.functions

    for function_name, function in endpoint_functions.items():
        parameters = function.parameters
        all_dependencies[function_name] = {}
        for parameter in parameters:
            docstring = parameter.documentation.description
            docstring_preprocessed = preprocess_docstring(docstring)
            doc = nlp(docstring_preprocessed)
            param_dependencies = []
            for sentence in doc.sents:
                sentence_dependency_matches = matcher(sentence)
                sentence_dependencies = extract_dependencies_from_docstring(
                    parameter,
                    parameters,
                    sentence,
                    sentence_dependency_matches,
                    spacy_id_to_pattern_id_mapping,
                )
                if sentence_dependencies:
                    param_dependencies.extend(sentence_dependencies)
            if param_dependencies:
                all_dependencies[function_name][parameter.name] = param_dependencies

    return APIDependencies(dependencies=all_dependencies)