A spaCy pipeline component for extracting keywords from text using cosine similarity.
Project description
Keyword spaCy
Keyword spaCy is a spaCy pipeline component for extracting keywords from text using cosine similarity. The basis for this comes from KeyBERT: A Minimal Method for Keyphrase Extraction using BERT, a transformer-based approach to keyword extraction. The methods employed by Keyword spaCy follow this methodology closely. It allows users to specify the range of n-grams to consider and can operate in a strict mode, which limits results to the specified n-gram range.
Installation
Before using Keyword spaCy, make sure you have spaCy installed:
pip install keyword-spacy
Then, download the en_core_web_md
model:
python -m spacy download en_core_web_md
Usage
To use the Keyword Extractor, first, create a spaCy nlp
object:
import spacy
nlp = spacy.load("en_core_web_md")
Then, add the KeywordExtractor
to the pipeline:
nlp.add_pipe("keyword_extractor", last=True, config={"top_n": 10, "min_ngram": 3, "max_ngram": 3, "strict": True})
Now you can process text and extract keywords:
text = "Natural language processing is a fascinating domain of artificial intelligence. It allows computers to understand and generate human language."
doc = nlp(text)
print("Top Keywords:", doc._.keywords)
Output:
Top Keywords: ['generate human language', 'Natural language processing']
Configuration
The KeywordExtractor
can be configured using the following parameters:
top_n
: The number of top keywords to extract.min_ngram
: The minimum size for n-grams.max_ngram
: The maximum size for n-grams.strict
: If set toTrue
, only n-grams within themin_ngram
tomax_ngram
range are considered. IfFalse
, individual tokens and the specified range of n-grams are considered.
Methodology
The methodology employed by Keyword spaCy is inspired by KeyBERT. It utilizes cosine similarity between tokens (and n-grams) and the entire document to determine the relevance of terms. The most similar terms are then considered as keywords.
References
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for keyword_spacy-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2758309a2e6ad98a7048539b6e629b4c4725611cbd91a0b99900aad22051189c |
|
MD5 | 74ce9194367804e46d8bd21f71bbe010 |
|
BLAKE2b-256 | 9957b7810da078de4745b899e3b4f7dc5d0f6bd7ddfe87a9bf3cd0ae7dbffbb5 |