Editor’s note: As 2021 winds down, we’re celebrating with a 12 Days of Christmas Countdown of the most popular, helpful expert articles on Search Engine Journal this year.
This collection was curated by our editorial team based on each article’s performance, utility, quality, and the value created for you, our readers.
Each day until December 24th, we’ll repost one of the best columns of the year, starting at No. 12 and counting down to No. 1. Today is number 11, originally published on July 28, 2021.
Andreas Voniatis did a fantastic job explaining how to create keyword clusters by search intent using Python. The images and screencaps make it easy to follow along, step by step, so even the most beginner Python user can follow along. Well done, Andreas!
Advertisement
Continue Reading Below
Thank you for contributing to Search Engine Journal and sharing your wisdom with readers.
Enjoy everyone!
There’s a lot to know about search intent, from using deep learning to infer search intent by classifying text and breaking down SERP titles using Natural Language Processing (NLP) techniques, to clustering based on semantic relevance with the benefits explained.
Not only do we know the benefits of deciphering search intent – we have a number of techniques at our disposal for scale and automation, too.
But often, those involve building your own AI. What if you don’t have the time nor the knowledge for that?
Advertisement
Continue Reading Below
In this column, you’ll learn a step-by-step process for automating keyword clustering by search intent using Python.
SERPs Contain Insights For Search Intent
Some methods require that you get all of the copy from titles of the ranking content for a given keyword, then feed it into a neural network model (which you have to then build and test), or maybe you’re using NLP to cluster keywords.
There is another method that enables you to use Google’s very own AI to do the work for you, without having to scrape all the SERPs content and build an AI model.
Let’s assume that Google ranks site URLs by the likelihood of the content satisfying the user query in descending order. It follows that if the intent for two keywords is the same, then the SERPs are likely to be similar.
For years, many SEO professionals compared SERP results for keywords to infer shared (or shared) search intent to stay on top of Core Updates, so this is nothing new.
The value-add here is the automation and scaling of this comparison, offering both speed and greater precision.
How To Cluster Keywords By Search Intent At Scale Using Python (With Code)
Begin with your SERPs results in a CSV download.
Advertisement
Continue Reading Below
1. Import The List Into Your Python Notebook.
import pandas as pd
import numpy as np
serps_input = pd.read_csv('data/sej_serps_input.csv')
serps_input
Below is the SERPs file now imported into a Pandas dataframe.
2. Filter Data For Page 1
We want to compare the Page 1 results of each SERP between keywords.
Advertisement
Continue Reading Below
We’ll split the dataframe into mini keyword dataframes to run the filtering function before recombining into a single dataframe, because we want to filter at keyword level:
The above shows all of the keyword SERP pair combinations, making it ready for SERP string comparison.
There is no open source library that compares list objects by order, so the function has been written for you below.
Advertisement
Continue Reading Below
The function ‘serp_compare’ compares the overlap of sites and the order of those sites between SERPs.
import py_stringmatching as sm
ws_tok = sm.WhitespaceTokenizer()
# Only compare the top k_urls results
def serps_similarity(serps_str1, serps_str2, k=15):
denom = k+1
norm = sum([2*(1/i - 1.0/(denom)) for i in range(1, denom)])
ws_tok = sm.WhitespaceTokenizer()
serps_1 = ws_tok.tokenize(serps_str1)[:k]
serps_2 = ws_tok.tokenize(serps_str2)[:k]
match = lambda a, b: [b.index(x)+1 if x in b else None for x in a]
pos_intersections = [(i+1,j) for i,j in enumerate(match(serps_1, serps_2)) if j is not None]
pos_in1_not_in2 = [i+1 for i,j in enumerate(match(serps_1, serps_2)) if j is None]
pos_in2_not_in1 = [i+1 for i,j in enumerate(match(serps_2, serps_1)) if j is None]
a_sum = sum([abs(1/i -1/j) for i,j in pos_intersections])
b_sum = sum([abs(1/i -1/denom) for i in pos_in1_not_in2])
c_sum = sum([abs(1/i -1/denom) for i in pos_in2_not_in1])
intent_prime = a_sum + b_sum + c_sum
intent_dist = 1 - (intent_prime/norm)
return intent_dist
# Apply the function
matched_serps['si_simi'] = matched_serps.apply(lambda x: serps_similarity(x.serp_string, x.serp_string_b), axis=1)
serps_compared = matched_serps[['keyword', 'keyword_b', 'si_simi']]
serps_compared
Now that the comparisons have been executed, we can start clustering keywords.
Advertisement
Continue Reading Below
We will be treating any keywords which have a weighted similarity of 40% or more.
We now have the potential topic name, keywords SERP similarity, and search volumes of each.
You’ll note that keyword and keyword_b have been renamed to topic and keyword, respectively.
Advertisement
Continue Reading Below
Now we’re going to iterate over the columns in the dataframe using the lamdas technique.
The lamdas technique is an efficient way to iterate over rows in a Pandas dataframe because it converts rows to a list as opposed to the .iterrows() function.
Here goes:
queries_in_df = list(set(keywords_filtered_nonnan.topic.to_list()))
topic_groups_numbered = {}
topics_added = []
def find_topics(si, keyw, topc):
i = 0
if (si >= simi_lim) and (not keyw in topics_added) and (not topc in topics_added):
i += 1
topics_added.append(keyw)
topics_added.append(topc)
topic_groups_numbered[i] = [keyw, topc]
elif si >= simi_lim and (keyw in topics_added) and (not topc in topics_added):
j = [key for key, value in topic_groups_numbered.items() if keyw in value]
topics_added.append(topc)
topic_groups_numbered[j[0]].append(topc)
elif si >= simi_lim and (not keyw in topics_added) and (topc in topics_added):
j = [key for key, value in topic_groups_numbered.items() if topc in value]
topics_added.append(keyw)
topic_groups_numbered[j[0]].append(keyw)
def apply_impl_ft(df):
return df.apply(
lambda row:
find_topics(row.si_simi, row.keyword, row.topic), axis=1)
apply_impl_ft(keywords_filtered_nonnan)
topic_groups_numbered = {k:list(set(v)) for k, v in topic_groups_numbered.items()}
topic_groups_numbered
Below shows a dictionary containing all the keywords clustered by search intent into numbered groups:
topic_groups_lst = []
for k, l in topic_groups_numbered.items():
for v in l:
topic_groups_lst.append([k, v])
topic_groups_dictdf = pd.DataFrame(topic_groups_lst, columns=['topic_group_no', 'keyword'])
topic_groups_dictdf
The search intent groups above show a good approximation of the keywords inside them, something that an SEO expert would likely achieve.
Advertisement
Continue Reading Below
Although we only used a small set of keywords, the method can obviously be scaled to thousands (if not more).
Activating The Outputs To Make Your Search Better
Of course, the above could be taken further using neural networks processing the ranking content for more accurate clusters and cluster group naming, as some of the commercial products out there already do.
For now, with this output you can:
Incorporate this into your own SEO dashboard systems to make your trends and SEO reporting more meaningful.
Build better paid search campaigns by structuring your Google Ads accounts by search intent for a higher Quality Score.
Merge redundant facet ecommerce search URLs.
Structure a shopping site’s taxonomy according to search intent instead of a typical product catalog.
Advertisement
Continue Reading Below
I’m sure there are more applications that I haven’t mentioned — feel free to comment on any important ones that I’ve not already mentioned.
In any case, your SEO keyword research just got that little bit more scalable, accurate, and quicker!
Denial of responsibility! Search Engine Codex is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.