'How to assign weights to keywords using python
There are keywords categorized from its HTML tags and need to assign weights to the tags and get the calculated total weight for each keyword separately.
H1 tag keywords: ['jquery']
H2 tag keywords:['aws', 'jquery']
p tag keywords:['country', 'jquery', 'aws']
need to assign weights for tags (example format)
H1 - 10
H2 - 5
p - 3
an get the Calculated weights from keyword ( only total is needed)
jquery : 10 + 5 + 3 = 18
aws : 5 + 3 = 8
country : 3 = 3
Solution 1:[1]
There are many different ways to solve this, and although this one is not the most elegant one, if we assume you have different lists for h1, h2 and p, you can do as following:
h1list = [e.text for e in soup.find_all('h1')]
h2list = [e.text for e in soup.find_all('h2')]
plist = [e.text for e in soup.find_all('p')]
weightsdict = { 'h1': 10, 'h2': 5, 'p':3 }
stringtoeval = ['jquery', 'aws', 'country']
for s in stringtoeval:
print(s, weightsdict['h1']*h1list.count(s)+weightsdict['h2']*h2list.count(s)+weightsdict['p']*plist.count(s))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
