[Free Script] Bulk Keyword Deduplication / Similarity Check

BlogPro

Elite Member
Jr. Executive VIP
Jr. VIP
Joined
Apr 23, 2012
Messages
2,845
Reaction score
5,856
Hey,

So been a while since I shared something here. Nonetheless, here goes -

The Problem

A lot of us deal with large caches of keywords on a daily basis - either through scraping or Semrush or Ahrefs. As we get these keywords, there's some similarity between them. We often have keywords like best nft to buy, best nft to buy today, best nft's to buy right now and best nft to buy now

Now imagine assigning each of these keywords to a separate page on your website - it would be tantamount to keyword cannibalization. Or if you're scraping - this would be akin to a lot of wasted resource.

//

The Magic of the script

To somewhat tackle this, I had written a quick script a while ago here - however, it used recursion and was a huge pain in the ass for large sets of keywords.

And since this was a recurring issue, I decided to tackle it once and for all. This one uses Agglomerative Clustering and the paraphrase-distilroberta-base-v1 and computes similarity to resolve the issue.

It then does three things

1. It removes the duplicate keywords that match the similarity threshold
2. Creates a new CSV file with just the unique keywords
3. Creates a new Excel file with three columns - Removed Keywords, Their Corresponding Retained Keyword, Similarity between the three

//

Example

I gave it 10,000 keywords - with everybody's favorite topics NFT - On an old Macbook Pro it took 6 minutes to get me 8597 unique keywords, removed 1402 keywords

Original Keywords I gave the script - Check here
Deduplicated Keywords the script returned - Check here
Excel File the Script generated - Check Here

//

How to use the script?


You can download the script through the below link or by clicking here - click the green "Code" button and download as Zip

Step 1: Prepare your keyword list

Create a CSV file containing your list of keywords - Name this file keywords.csv. Each keyword should be on a separate line.

Step 2: Install the required packages

Make sure you have Python 3.6 or higher installed on your system. Then, install the required packages by running the following command in your terminal or command prompt - please navigate to the directory where the script is present. If you're using something like VSCode things become easier.

pip install -r requirements.txt

The requirements.txt is included in the below Github package.

Step 3: Run the script

Open your terminal or command prompt and navigate to the directory where the deduplication.py script is located. Run the script with the following command:

python deduplication.py

Step 4: Set the similarity threshold

During the execution of the script, you'll be prompted to enter the similarity threshold. The default value is 0.8. If the similarity between two keywords is higher than the threshold, they will be considered part of the same cluster and one of them will be removed.

I personally prefer 0.9 - but I have added the option for you to enter the threshold so you can play around with those numbers till you get the desired output.

Step 5: Specify the output file name

You'll also be prompted to enter the name of the output Excel file. The default name is keywords_output.xlsx. You can choose a different name if you prefer. Something like nft-final.xlsx

Step 6: Check the output files

Once the script finishes running, it will generate two output files in the same directory - unique_keywords.csv & keywords_output.xlsx (or the name you entered)

//

How does it work?

  1. Reads keywords from a CSV file: The script reads your list of keywords from a CSV file, storing the keywords in a list.

  2. Generate keyword embeddings: The script leverages the SentenceTransformer library to create embeddings for each keyword. Sentence transformers are pre-trained neural network models that can convert text into numerical vectors (embeddings) that capture semantic information. The script uses the "paraphrase-distilroberta-base-v1" model for this purpose.

  3. Compute similarity matrix: The script calculates a similarity matrix for the keyword embeddings using the cosine similarity metric. Cosine similarity values range from 0 to 1. In this case, 0 would mean not similar at all - 1 would mean extremely similar. Try staying in the higher ranges.

  4. Perform Agglomerative Clustering: Based on the similarity matrix, the script applies the Agglomerative Clustering algorithm to group similar keywords together. This algorithm creates a hierarchical clustering structure, merging pairs of clusters iteratively until the specified similarity threshold is reached.

  5. Set similarity threshold: You can define a similarity threshold (default is 0.8) during the execution of the script. If the similarity between two keywords is higher than this threshold, they will be considered part of the same cluster and one of them will be removed.

  6. Generate output files:The script creates two output files for you:
    • unique_keywords.csv - Contains the list of unique keywords after removing similar ones.
    • keywords_output.xlsx (or the name you entered) - An Excel file with color-coded information on removed keywords, retained keywords, and their similarity. Removed keywords are highlighted in yellow.

Google Colab Version Here
 
Last edited by a moderator:
Awesome! This is exactly what I've been looking for! Does it work for all languages?
 
Awesome! This is exactly what I've been looking for! Does it work for all languages?

The model I have used paraphrase-distilroberta-base-v1 is specifically trained on English Language only.

What you can do is, change the model to a multi-lingual. A cursory search for multilinqual sentence-transformer models shows me - https://metatext.io/models/sentence-transformers-paraphrase-xlm-r-multilingual-v1 - which seems to be trained on 50+ languages

It's present on Huggingface

So please replace the following line

Python:
model = SentenceTransformer("paraphrase-distilroberta-base-v1")

with

Python:
model = SentenceTransformer("paraphrase-xlm-r-multilingual-v1")

And it should work

P.S - I tested several models and techniques - even ANN with Annoy/Faiss and landed upon this model. So I can't vouch for the veracity of the model - since grammar et al differs a lot. You will need to run your own tests and play around with the threshold a little.
 
The model I have used paraphrase-distilroberta-base-v1 is specifically trained on English Language only.

What you can do is, change the model to a multi-lingual. A cursory search for multilinqual sentence-transformer models shows me - https://metatext.io/models/sentence-transformers-paraphrase-xlm-r-multilingual-v1 - which seems to be trained on 50+ languages

It's present on Huggingface

So please replace the following line

Python:
model = SentenceTransformer("paraphrase-distilroberta-base-v1")

with

Python:
model = SentenceTransformer("paraphrase-xlm-r-multilingual-v1")

And it should work

P.S - I tested several models and techniques - even ANN with Annoy/Faiss and landed upon this model. So I can't vouch for the veracity of the model - since grammar et al differs a lot. You will need to run your own tests and play around with the threshold a little.
Thank you so much! I will try it out :)
 
Excellent work and nice share! Many thanks.

NOTE: For Linux it's slightly different
1) You need Python 3.9 and above
2) Installation of spacy and model looks like:
pip3.9 install spacy python3.9 -m spacy download en_core_web_lg
 
Excellent work and nice share! Many thanks.

NOTE: For Linux it's slightly different
1) You need Python 3.9 and above
2) Installation of spacy and model looks like:
pip3.9 install spacy python3.9 -m spacy download en_core_web_lg

You seem to be a bit confused bud.

Spacy was used in the old script with recursion. The new one in this thread uses agglomerative clustering and the paraphrase-distilroberta-base-v1 model.

Two completely different models.

This is the new script - https://github.com/iamBlogPro/Similarity-Check-Advanced
 
Thank you - the Skype messages have been overwhelming.

There's a bunch of questions. I'd appreciate it, if you can ask it here only.

I'll answer a few -

1. Yes - when you run the script the first time, it will download the model - which is a 500 MB Download
2. I tested on a MacOS with 10,000 keywords and it took about 4 minutes in total.
3. Yes, it will ask you in the terminal for your similarity threshold. If it confuses you - think of it like a percentage. So if you enter 0.90 - you're telling the script "Remove any keyword that is 90% similar to the others". If you enter "0.80" - you're asking for 80% or more - so on and so forth.
4. Yes, you can run multiple passes with the script.
5. No I don't intend to make a web version as of yet - it is resource intensive and may not be worth my time.

I also have a few requests on keyword clustering script. I am planning on implementing Jaccard Index - let me know if there is an interest for it.
 
I also have a few requests on keyword clustering script. I am planning on implementing Jaccard Index - let me know if there is an interest for it.

Great share! Wouldn't also definitely be interested in the clustering script :)
 
Just ran the de-dupe tool - 1660 keywords in around 45 secs on a Mac mini M1. Thanks for the script @BlogPro!
Cheers man! What did you think of the output?

Did it do what you were looking for?

Great share! Would also definitely be interested in the clustering script :)

Let me finish running some experiments. And I'll publish that too. :)

Is there anything else you'd liked coded? (This question is open to everyone).
 
Cheers man! What did you think of the output?

Did it do what you were looking for?

Yes, when I looked through the keywords_output file, it had detected a lot of very similar keywords. I had it set on .90.

Is there anything else you'd liked coded? (This question is open to everyone).

I'm currently created a AI blog posting script(s) and It would be great if I could add and image or two to each article that's scraped from Pexels, etc. I'm currently scraping images using a script and Unsplash API but manually uploading these to WordPress then using a plugin to assign a (random) featured image to each post.
 
thanks for sharing, i was in need for something like this.
 
Oh.. many errors are popping up. may be Not an issue with the code as i m not a coding guy.
 
Back
Top