-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathn_gram_mongo.py
More file actions
209 lines (140 loc) · 4.76 KB
/
n_gram_mongo.py
File metadata and controls
209 lines (140 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from mongoengine import *
import pandas as pd
import pymongo
from odo import odo
from pprint import pprint
import multiprocessing as mp
import click
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.test_database
from nltk.tokenize import word_tokenize
from nltk.util import ngrams
result = db['result']
def _connect_mongo(host, port, username, password, db):
""" A util for making a connection to mongo """
if username and password:
mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db)
conn = MongoClient(mongo_uri)
else:
conn = MongoClient(host, port)
return conn[db]
def clean(text):
try:
text = str(text)
a = text .replace('[','').replace(']','').replace('"','')
return a
except:
return 'error'
def get_ngrams(text, n=1 ):
n_grams = ngrams(word_tokenize(text), n)
#return [' '.join(grams) for grams in n_grams]
return [' '.join(grams) for grams in n_grams]
def import_data(name):
print('reading file......')
df = pd.read_excel('{}'.format(name), encoding="ISO-8859-1")
print('start processing file')
df.columns = df.columns.str.replace(".", "_")
cols = [col for col in df.columns if
col in ['Keyword', 'Campaign', 'Ad group', 'Status', 'Clicks', 'Impressions', 'Cost', 'Conversions',
'Match type']]
df = df[cols]
print('clean up keywords')
df['Keyword'] = df['Keyword'].map(clean)
print('Delete All Records first')
db.nword2.remove({})
print('Clear All Saved Records first')
db.result.remove({})
ng = odo(df, db.nword2)
return df
ngword = []
def get_word(n,df ):
global ngword
ke = df['Keyword'].tolist()
for x in ke:
ngword.extend(get_ngrams(x,n))
return list(set(ngword))
#pprint(word.find_one())
def get_mongo_data(word):
x = db.nword2.find({"Keyword": {'$regex': ".*{}.*".format(word)}})
click = []
imp = []
conv = []
cost = []
for cur in x:
#pprint(cur)
click.append(cur['Clicks'])
imp.append(cur['Impressions'])
conv.append(cur['Conversions'])
cost.append(cur['Cost'])
#pprint(x.count())
# pprint(x['Avg_ CPC'].sum())
# pprint(sum(cl))
# click = [ int(y['Clicks']) for y in x ]
# cost = [ int(c['Cost']) for c in x]
# imp = [ int(im['Impressions']) for im in x]
# conv = [ int(con['Conversions']) for con in x]
#pprint('click : {}'.format(sum(click)))
# pprint(click)
#pprint('Cost : {} '.format(round(sum(cost), 2)))
#pprint('imp : {} '.format(sum(imp)))
#pprint('conv : {}'.format(sum(conv)))
#print('keyword : {}'.format(word))
return db.result.insert_one({
'keyword':word,
'click':sum(click),
'imp': sum(imp),
'cost' : round(sum(cost), 2),
'conv': sum(conv),
'CTR': sum(click)/sum(imp) if sum(imp) else 0,
'CPC': round(sum(cost), 2)/sum(click) if sum(click) else 0,
'conv_rate': sum(conv)/sum(click) if sum(click) else 0
})
@click.command()
@click.option('--name', prompt='files name',help='file name (xlsx)')
@click.option('--n', type=int,help='n of n_gram',default= 1,prompt='ngram numbers')
def run(name,n):
df = import_data('{}'.format(name))
print('generate n_gram_keyword......')
asy = get_word(n, df)
# pprint(get_word(1,df))
# pprint(get_mongo_data('women'))
pool = mp.Pool()
print('start n gram......')
p = pool.map(get_mongo_data,asy)
#for x in asy:
#get_mongo_data(x)
pprint(db.result.find_one())
print("Done......")
collection = db.result
data = pd.DataFrame(list(collection.find()))
del data['_id']
data.to_excel('{}_gram.xlsx'.format(n), sheet_name='{}_gram'.format(n))
if __name__ == '__main__':
run()
"""
x = db.nword2.find({"Keyword" : {'$regex' : ".*women.*"}})
click = []
imp = []
conv =[]
cost = []
for cur in x:
pprint(cur)
click.append(cur['Clicks'])
imp.append(cur['Impressions'])
conv.append(cur['Conversions'])
cost.append(cur['Cost'])
pprint(x.count())
#pprint(x['Avg_ CPC'].sum())
#pprint(sum(cl))
#click = [ int(y['Clicks']) for y in x ]
#cost = [ int(c['Cost']) for c in x]
#imp = [ int(im['Impressions']) for im in x]
#conv = [ int(con['Conversions']) for con in x]
pprint('click : {}'.format(sum(click)))
#pprint(click)
pprint('Cost : {} '.format(round(sum(cost),2)))
pprint('imp : {} '.format(sum(imp)))
pprint(sum(conv))
#odo('mongodb://hostname/db::collection', pd.DataFrame)
"""