#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  7 09:14:35 2017

@author: apogee_tpaschke
"""


import pandas as pd 
import pprint
import itertools
from collections import Counter
import re 
import csv 
import numpy as np 


#*****Before you start make sure your file is in the same format as betterment_page_path.csv'*******

num_paths = 75
file = 'betterment_page_path_cleaned.csv'
output = 'betterment_page_path.csv'

#create empty list to populate later
path_list = []

#read in your csv file
df = pd.read_csv('/Users/apogee_tpaschke/Downloads/'+file, encoding = 'mac_roman', error_bad_lines=False , low_memory = False)

df.columns = ['visitor','activity','date','time','site','path','referr']

#create new variables that reference values beore and after to be used in calculation logic 
df['prev_visitor'] = df.visitor.shift(1)
df['prev_activity'] = df.activity.shift(1)
df['prev_path'] = df.path.shift(1)
df['next_visitor'] = df.visitor.shift(-1)
df['next_time'] = df.time.shift(-1)





      


#print(df.head(50))

#==============================================================================
# def form_start(x):
#         if (x['visitor'] == x['prev_visitor'] 
#         and x['activity'] == 'Application Start'
#         and x['time'] != x['next_time']):
#             visits = 1
#             return visits
# 
# 
# def num_visits(x):
#     if x.apply(app_start) == 1 and x['visitor'] == x.visitor.shift(2):
#         visits = 2
#         print(visits)
#         return visits 
#     
# num = df.apply(form_start, axis=1)
# print(num)
#==============================================================================



#this function evaluates if a user had a valid application start and returns the page path they were on prior to starting
def path(x):
    if (x['visitor'] == x['prev_visitor'] 
        and x['activity'] == 'Application Start'
        and x['prev_activity'] != 'Application Start'
        and x['time'] != x['next_time']):
        path_list.append(x['prev_path'])
        return x['prev_path']   
df.apply(path, axis=1)


# this counts how many similar pages there were for application starts and returns the rank 
counts = Counter(path_list)
top_terms=counts.most_common(num_paths)
tm = pd.DataFrame(top_terms)
tm.to_csv('top_application_start_terms.csv')


#print(df.head (n=25))



    























