(
    doc: Doc,
    *,
    normalize: Optional[str] = "lemma",
    ngrams: int | Collection[int] = (1, 2, 3),
    include_pos: Optional[str | Collection[str]] = ("NOUN", "PROPN", "ADJ"),
    window_size: int = 2,
    topn: int | float = 10,
)
return ext_utils.get_filtered_topn_terms(
        sorted_term_scores, topn, match_threshold=0.8
    )
Extract key terms from a document using the YAKE algorithm.

    Args:
        doc: spaCy ``Doc`` from which to extract keyterms.
            Must be sentence-segmented; optionally POS-tagged.
        normalize: If "lemma", lemmatize terms; if "lower", lowercase terms;
            if "norm", use the norm of the terms (as set in a language's tokenizer exceptions);
            if None, use the form of terms as they appeared in ``doc``.

            .. note:: Unlike the other keyterm extraction functions, this one
               doesn't accept a callable for ``normalize``.

        ngrams: n of which n-grams to consider as keyterm candidates.
            For example, `(1, 2, 3)`` includes all unigrams, bigrams, and trigrams,
            while ``2`` includes bigrams only.
        include_pos: One or more POS tags with which to filter for good candidate keyterms.
            If None, include tokens of all POS tags
            (which also allows keyterm extraction from docs without POS-tagging.)
        window_size: Number of words to the right and left of a given word
            to use as context when computing the "relatedness to context"
            component of its score. Note that the resulting sliding window's
            full width is ``1 + (2 * window_size)``.
        topn: Number of top-ranked terms to return as key terms.
            If an integer, represents the absolute number; if a float, value
            must be in the interval (0.0, 1.0], which is converted to an int by
            ``int(round(len(candidates) * topn))``

    Returns:
        Sorted list of top ``topn`` key terms and their corresponding YAKE scores.

    References:
        Campos, Mangaravite, Pasquali, Jorge, Nunes, and Jatowt. (2018).
        A Text Feature Based Automatic Keyword Extraction Method for Single Documents.
        Advances in Information Retrieval. ECIR 2018.
        Lecture Notes in Computer Science, vol 10772, pp. 684-691.
Extract key terms from a document using the YAKE algorithm.
Extract key terms from a document using the YAKE algorithm .
def yake(
    doc: Doc,
    *,
    normalize: Optional[str] = "lemma",
    ngrams: int | Collection[int] = (1, 2, 3),
    include_pos: Optional[str | Collection[str]] = ("NOUN", "PROPN", "ADJ"),
    window_size: int = 2,
    topn: int | float = 10,
) -> List[Tuple[str, float]]:
    """
    Extract key terms from a document using the YAKE algorithm.

    Args:
        doc: spaCy ``Doc`` from which to extract keyterms.
            Must be sentence-segmented; optionally POS-tagged.
        normalize: If "lemma", lemmatize terms; if "lower", lowercase terms;
            if "norm", use the norm of the terms (as set in a language's tokenizer exceptions);
            if None, use the form of terms as they appeared in ``doc``.

            .. note:: Unlike the other keyterm extraction functions, this one
               doesn't accept a callable for ``normalize``.

        ngrams: n of which n-grams to consider as keyterm candidates.
            For example, `(1, 2, 3)`` includes all unigrams, bigrams, and trigrams,
            while ``2`` includes bigrams only.
        include_pos: One or more POS tags with which to filter for good candidate keyterms.
            If None, include tokens of all POS tags
            (which also allows keyterm extraction from docs without POS-tagging.)
        window_size: Number of words to the right and left of a given word
            to use as context when computing the "relatedness to context"
            component of its score. Note that the resulting sliding window's
            full width is ``1 + (2 * window_size)``.
        topn: Number of top-ranked terms to return as key terms.
            If an integer, represents the absolute number; if a float, value
            must be in the interval (0.0, 1.0], which is converted to an int by
            ``int(round(len(candidates) * topn))``

    Returns:
        Sorted list of top ``topn`` key terms and their corresponding YAKE scores.

    References:
        Campos, Mangaravite, Pasquali, Jorge, Nunes, and Jatowt. (2018).
        A Text Feature Based Automatic Keyword Extraction Method for Single Documents.
        Advances in Information Retrieval. ECIR 2018.
        Lecture Notes in Computer Science, vol 10772, pp. 684-691.
    """
    # validate / transform args
    ngrams = utils.to_collection(ngrams, int, tuple)
    include_pos = utils.to_collection(include_pos, str, set)
    if isinstance(topn, float):
        if not 0.0 < topn <= 1.0:
            raise ValueError(
                f"topn = {topn} is invalid; "
                "must be an int, or a float between 0.0 and 1.0"
            )

    # bail out on empty docs
    if not doc:
        return []

    stop_words: Set[str] = set()
    seen_candidates: Set[str] = set()
    # compute key values on a per-word basis
    word_occ_vals = _get_per_word_occurrence_values(
        doc, normalize, stop_words, window_size
    )
    # doc doesn't have any words...
    if not word_occ_vals:
        return []

    word_freqs = {w_id: len(vals["is_uc"]) for w_id, vals in word_occ_vals.items()}
    word_scores = _compute_word_scores(doc, word_occ_vals, word_freqs, stop_words)
    # compute scores for candidate terms based on scores of constituent words
    term_scores: Dict[str, float] = {}
    # do single-word candidates separately; it's faster and simpler
    if 1 in ngrams:
        candidates = _get_unigram_candidates(doc, include_pos)
        _score_unigram_candidates(
            candidates,
            word_freqs,
            word_scores,
            term_scores,
            stop_words,
            seen_candidates,
            normalize,
        )
    # now compute combined scores for higher-n ngram and candidates
    candidates = list(
        ext_utils.get_ngram_candidates(
            doc, [n for n in ngrams if n > 1], include_pos=include_pos,
        )
    )
    attr_name = _get_attr_name(normalize, True)
    ngram_freqs = itertoolz.frequencies(
        " ".join(getattr(word, attr_name) for word in ngram) for ngram in candidates
    )
    _score_ngram_candidates(
        candidates, ngram_freqs, word_scores, term_scores, seen_candidates, normalize,
    )
    # build up a list of key terms in order of increasing score
    if isinstance(topn, float):
        topn = int(round(len(seen_candidates) * topn))
    sorted_term_scores = sorted(
        term_scores.items(), key=operator.itemgetter(1), reverse=False,
    )
    return ext_utils.get_filtered_topn_terms(
        sorted_term_scores, topn, match_threshold=0.8
    )
def yake ( doc : Doc , * , normalize : Optional [ str ] = "lemma" , ngrams : int | Collection [ int ] = ( 1 , 2 , 3 ) , include_pos : Optional [ str | Collection [ str ] ] = ( "NOUN" , "PROPN" , "ADJ" ) , window_size : int = 2 , topn : int | float = 10 , ) -> List [ Tuple [ str , float ] ] : # validate / transform args ngrams = utils . to_collection ( ngrams , int , tuple ) include_pos = utils . to_collection ( include_pos , str , set ) if isinstance ( topn , float ) : if not 0.0 < topn <= 1.0 : raise ValueError ( f"topn = {topn} is invalid; " "must be an int, or a float between 0.0 and 1.0" ) # bail out on empty docs if not doc : return [ ] stop_words : Set [ str ] = set ( ) seen_candidates : Set [ str ] = set ( ) # compute key values on a per-word basis word_occ_vals = _get_per_word_occurrence_values ( doc , normalize , stop_words , window_size ) # doc doesn't have any words... if not word_occ_vals : return [ ] word_freqs = { w_id : len ( vals [ "is_uc" ] ) for w_id , vals in word_occ_vals . items ( ) } word_scores = _compute_word_scores ( doc , word_occ_vals , word_freqs , stop_words ) # compute scores for candidate terms based on scores of constituent words term_scores : Dict [ str , float ] = { } # do single-word candidates separately; it's faster and simpler if 1 in ngrams : candidates = _get_unigram_candidates ( doc , include_pos ) _score_unigram_candidates ( candidates , word_freqs , word_scores , term_scores , stop_words , seen_candidates , normalize , ) # now compute combined scores for higher-n ngram and candidates candidates = list ( ext_utils . get_ngram_candidates ( doc , [ n for n in ngrams if n > 1 ] , include_pos = include_pos , ) ) attr_name = _get_attr_name ( normalize , True ) ngram_freqs = itertoolz . frequencies ( " " . join ( getattr ( word , attr_name ) for word in ngram ) for ngram in candidates ) _score_ngram_candidates ( candidates , ngram_freqs , word_scores , term_scores , seen_candidates , normalize , ) # build up a list of key terms in order of increasing score if isinstance ( topn , float ) : topn = int ( round ( len ( seen_candidates ) * topn ) ) sorted_term_scores = sorted ( term_scores . items ( ) , key = operator . itemgetter ( 1 ) , reverse = False , ) return ext_utils . get_filtered_topn_terms ( sorted_term_scores , topn , match_threshold = 0.8 )