codekasten/adstreme/KeywordGenerator/kg_calc.py
2006-04-20 12:51:33 +00:00

105 lines
3 KiB
Python

#!/usr/bin/env python
#TODO: funktion, die aus parametern schoene listen macht.
import string
class KeywordGenerator:
def __init__(self):
import string,sys
return
def add_prefix_to_strings(self,prefix,stringlist):
"""gets a string and a list of strings
returns a list of strings each prefixed with given string."""
ret = []
for item in stringlist:
ret.append(prefix+item)
return ret
def quotes_and_braces(self,keywords):
"""gets a string with keywords, returns
given string
"given string"
[given string]
"""
ret = '%s\n' % keywords
ret += '"%s"\n' % keywords
ret += '[%s]\n' % keywords
return ret
def arg_string_to_list(self,s):
"""gets a string.
removes all occurences of " and splits with , as separator."""
s = s.replace('"','')
ret = s.split(',')
return ret
def generate_keywords(self,l1,l2,l3):
"""
gets 3 lists of keyword parts.
returns a string containing every possible combination of above
keywords and its generated variants from quotes_and_braces."
"""
ret = ""
list1 = self.arg_string_to_list(l1)
list2 = self.arg_string_to_list(l2)
list3 = self.arg_string_to_list(l3)
for list2item in list2:
tmplist1 = []
tmplist1 = self.add_prefix_to_strings(list2item+" ",list3)
for list1item in list1:
tmplist2 = []
tmplist2 = self.add_prefix_to_strings(list1item+" ",tmplist1)
for tmplist2item in tmplist2:
ret += self.quotes_and_braces(tmplist2item)
ret += "\n"
return ret[:-3]+ string.replace(ret[-3:],"\n","")#cut the tailing '\n\n\n'
'''
def test():
#sample data
#test quotes_and_brace
keywords = "fair clothing"
expected_result = """fair clothing\n"fair clothing"\n[fair clothing]\n"""
if quotes_and_braces(keywords) != expected_result:
raise "test of quotes_and_braces() failed!"
#test add_prefix_to_strings
pref = "fair "
strlist = ["clothing","clothings"]
expected_result = ["fair clothing","fair clothings"]
if add_prefix_to_strings(pref,strlist) != expected_result:
raise "test of add_prefix_to_strings() failed!"
#test generate_keywords
list1 = ["fair"]
list2 = ["clothing","clothings"]
list3 = ["buy"]
expected_result = """fair clothing buy\n"fair clothing buy"\n[fair clothing buy]\n\n\nfair clothings buy\n"fair clothings buy"\n[fair clothings buy]\n\n\n"""
if generate_keywords(list1,list2,list3) != expected_result:
raise "test of generate_keywords() failed!"
#test arg_string_to_list(s)
s = '"clothing,clothings"'
expected_result = ['clothing', 'clothings']
if arg_string_to_list(s) != expected_result:
raise "test of arg_string_to_list() failed!"
#test get_arguments()
(list1,list2,list3) = get_arguments()
if __name__ == "__main__":
#test()
if len(sys.argv) !=4 :
print ' %s is a script for generating keyword lists suitable for google adwords.' % sys.argv[0]
print ' usage: %s "part1,alterative part1" "part2, another part2" ",verb"' % sys.argv[0]
else:
(list1,list2,list3) = get_arguments()
print generate_keywords(list1,list2,list3)
'''