Skip to content

Instantly share code, notes, and snippets.

@kennethreitz
Last active November 8, 2023 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethreitz/4a27ce871d0c2587ebe2c4fda9d9326b to your computer and use it in GitHub Desktop.
Save kennethreitz/4a27ce871d0c2587ebe2c4fda9d9326b to your computer and use it in GitHub Desktop.
something I threw together for tracking sentiment of keywords for a company, with the help of ai
import os
import marvin
from serpapi import Client as SerpApi
serpapi = SerpApi(api_key=os.environ['SERPAPI_API_KEY'])
@marvin.ai_fn
def get_suggested_keywords(org_name: str) -> list[str]:
"""
Given an `org_name`, returns a list of suggested keywords to rank / track for SEO for that organization.
"""
@marvin.ai_fn
def sentiment_score(texts: list[str]) -> list[float]:
"""
Given a list of `texts`, returns a list of numbers between 1 (positive) and
-1 (negative) indicating their respective sentiment scores.
"""
def get_keyword_serp(keyword: str, with_snippet_sentiment=False) -> dict:
s = serpapi.search(
engine="google",
q=keyword
)
if with_snippet_sentiment:
return score_serp_descriptions(s)
return s.as_dict()
def score_serp_descriptions(serp: dict) -> dict:
scores = [sentiment_score([result['snippet']]) for result in serp['organic_results']]
for i, score in enumerate(scores):
serp['organic_results'][i]['sentiment-score'] = score
return serp
# ----
company = "Apple"
print(f"Company name: {company}.")
serp = get_keyword_serp(company)
suggested_keywords = get_suggested_keywords(company)
print(f"Suggested keywords to track: {suggested_keywords}")
for keyword in suggested_keywords:
print()
print(f"Getting SERP for {keyword} and scoring snippet sentiment.")
s = get_keyword_serp(keyword, with_snippet_sentiment=True)
for result in s['organic_results']:
print(f"{result['position']}: {result['title']}: {result['sentiment-score']}")
@kennethreitz
Copy link
Author

kennethreitz commented Nov 8, 2023

$ python sentiment_serp_keyword_tracking_ai.py                                                                                                                            
Company name: Apple.
Suggested keywords to track: ['technology', 'innovation', 'products', 'iPhone', 'Mac', 'iPad']

Getting SERP for technology and scoring snippet sentiment.
1: Technology: [0.5]
2: Technology | Definition, Examples, Types, & Facts: [0.6]
3: What is technology? - OpenLearn: [0.1]
4: What Are The Types of Technology?: [0.5]
5: Technology - BBC News: [0.5]
6: Technology Definition & Meaning: [0.0]
7: MIT Technology Review: [0.8]
8: TECHNOLOGY Definition & Usage Examples: [0.0]
9: TECHNOLOGY | English meaning - Cambridge Dictionary: [0.5]
10: WIRED - The Latest in Technology, Science, Culture and ...: [0.8]

Getting SERP for innovation and scoring snippet sentiment.
1: Innovation Definition & Meaning: [0.5]
2: What is innovation?: [0.8]
3: Innovation: [0.25]
4: The eight essentials of innovation: [0.95]
5: Innovation in Business: What It Is & Why It's So Important: [0.5]
6: INNOVATION | English meaning - Cambridge Dictionary: [0.5]
7: Innovation: [0.5]
8: INNOVATION Definition & Usage Examples: [0.0]
9: What is innovation?: [0.6]
10: What is Innovation? Introduction and Definition for Practitioners: [0.8]

Getting SERP for products and scoring snippet sentiment.
1: Product (business): [0.5]
2: PRODUCT | definition in the Cambridge English Dictionary: [0.5]
3: What Is a Product? (+ Tips To Launch a Lovable Product): [0.5]
4: Browse All of Google's Products & Services: [-0.5]
5: Products and Services - Definitions, Examples, Differences: [0.1]
6: 17 Trending Products and Things to Sell Online (2023): [0.9]
7: What is a "Product"? | Definition and Overview: [0.2]
8: What is Product? Definition of Product, Product Meaning: [0.0]
9: Product Definition & Meaning: [0.2]
10: PRODUCT Definition & Usage Examples: [0.5]

Getting SERP for iPhone and scoring snippet sentiment.
1: iPhone - Apple: [0.8]
2: Apple: [0.8]
3: iPhone: [0.8]
4: New Apple iPhones & Accessories: [0.8]
5: Iphone: [0.8, 0.9, 0.7]
6: Newest Apple iPhones for Sale - T-Mobile: [0.9]
7: Best iPhone in 2023: Which Apple Phone Should You Buy?: [0.9]
8: Deals on Our Best New iPhones, iPads & Apple ...: [0.8]

Getting SERP for Mac and scoring snippet sentiment.
1: MAC Cosmetics | Beauty and Makeup Products: [0.7]
2: Mac: [0.97]
3: MAC Cosmetics: [0.8]
4: Apple: [0.6]
5: M·A·C Cosmetics (@maccosmetics): [0.8]
6: MAC Cosmetics, Makeup, & Skin Care: [0.8]
7: Mac (computer): [0.2]
8: MAC Cosmetics | Beauty and Makeup Products: [0.5]
9: M·A·C Cosmetics: [0.5]
10: Shop MAC Cosmetics Online: [0.8]

Getting SERP for iPad and scoring snippet sentiment.
1: iPad - Apple: [0.8]
2: iPad – Best Buy: [0.8]
3: Buy iPad 10.2-inch - Education: [0.8]
4: iPad: [0.5]
5: Ipad: [0.8]
6: Apple iPad: [-0.2]
7: iPad in Apple iPad - Walmart.com: [0.6]

@kennethreitz
Copy link
Author

just a neat idea. not too much to see here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment