Skip to content

Instantly share code, notes, and snippets.

@olih
Last active December 20, 2015 18:18
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 olih/6174396 to your computer and use it in GitHub Desktop.
Save olih/6174396 to your computer and use it in GitHub Desktop.
Analysis of adwords
#!/usr/bin/python
#
#Creator: 2013, Olivier Huin (https://github.com/olih)
#License: Eclipse Public License - v 1.0
#Contributors:
#
import sys, getopt
import csv
M=1000000
def analyse(infile, word, mincomp,maxcomp, maxcpc):
r=[]
with open(infile, 'rb') as f:
reader = csv.reader(f, delimiter='\t')
next(reader, None) # skip the headers
for row in reader:
[search, competition_str, cpc_str, impr_str]=row[1:5]
competition=0
cpc=0
impr=0
if competition_str:competition=float(competition_str)
if cpc_str:cpc=float(cpc_str)
if impr_str:impr=float(impr_str)
criteria= True
if competition<mincomp: criteria= False
if competition>maxcomp: criteria= False
if cpc>maxcpc: criteria= False
if not word in search: criteria= False
if criteria:
starsearch=str.replace(search,word,"***")
select=[starsearch, competition, cpc]
r.append(select)
rs=sorted(r, key=lambda column: column[1], reverse=True)
return r
def wordStats(analysed,weighted=False):
r={}
for row in analysed:
[starsearch, competition, cpc]=row
words=starsearch.split(" ")
weight= 1.0
if weighted:
weight=competition
for word in words:
rank=r.get(word,0)
rank=rank+weight
r[word]=rank
items=r.items()
rs=sorted(items, key=lambda col: col[1], reverse=True)
return rs
#Filename containing the csv from google
#Any keyword to be found in search term
def summary(filename,keyword):
data=analyse(filename,keyword,20,M, 5.00)
print "For keyword "+keyword+":\n"
for s in data: print s
print "Stats\n"
print wordStats(data,False)
print "\n"
print wordStats(data,True)
def wordle(filename,keyword):
r=""
data=analyse(filename,keyword,20,M, 5.00)
stats=wordStats(data,True)
for wordstats in stats:
r=r+ "%s: %d\n" % wordstats
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment