# -*- coding: utf-8 -*- """ 検索トレンドをもとにしたレコメンドロジック """ import sys import pandas as pd import datetime as dt sys.path.append("/var/local/mode2") from .base import ModelBase from lib.WebRecommend.utils.dataAccess import dataAccess class SearchTrend(ModelBase): def __init__(self, domain=False): self.recommend_category = 'search_trend' self.domain = domain def calcTrend(self, **kwargs): # 検索数のトレンドを計算 da = dataAccess(self.domain) keyword_list = da.getKeywordList() gtrend_res = da.getSearchTrend(keyword_list) res = [] if gtrend_res['data'] != None: try: df = pd.DataFrame( gtrend_res['data'], columns=gtrend_res['label']) df = df.set_index('date') df = df.sort_index() cols = filter(lambda a: a != 'date', gtrend_res['label']) for l in cols: pct_change = df[l].pct_change().dropna() head = pct_change.head(10) tail = pct_change.tail(10) head_total_change = 0.0 head_count_plus = 0 head_count_minus = 0 tail_total_change = 0.0 tail_count_plus = 0 tail_count_minus = 0 # サイコロジカルライン計算 for i in head: head_total_change += i if i > 0: head_count_plus += 1 else: head_count_minus += 1 for i in tail: tail_total_change += i if i > 0: tail_count_plus += 1 else: tail_count_minus += 1 print('head: '+str(head_total_change) + ' tail: '+str(tail_total_change)) diff = abs(head_total_change - tail_total_change) if diff > 0.1: if head_total_change > tail_total_change: trend = 'down' else: trend = 'up' else: trend = 'nochange' res.append(dict(label=l, head_total_change=head_total_change, tail_total_change=tail_total_change, head_count_plus=head_count_plus, head_count_minus=head_count_minus, tail_count_plus=tail_count_plus, tail_count_minus=tail_count_minus, trend=trend)) except Exception as e: print(e) exc_type, exc_obj, tb = sys.exc_info() lineno = tb.tb_lineno print(lineno) pass return res def getRecommend(self): # 検索数のトレンド情報からレコメンド文を生成 res = [] st = self.calcTrend() for i in st: print(i) if i['trend'] == 'up': try: text = "「" + i['label'] + "」というワードの検索が上昇傾向のようです。"\ "ウェブサイトのコンテンツ内にもこのワードを入れるとアクセス数の増加が期待できます。" except: text = "「" + i['label'].decode('utf-8') + "」というワードの検索が上昇傾向のようです。"\ "ウェブサイトのコンテンツ内にもこのワードを入れるとアクセス数の増加が期待できます。" res.append(dict(date=str(dt.date.today()), text=text)) if i['trend'] == 'down': try: text = "「" + i['label'] + "」というワードの検索が下落傾向のようです。"\ "サイト分析ワードの再設定を行うことで現在の人気を把握し、"\ "そのワードをウェブサイトに増やすことでアクセス数の増加が期待できます。" except: text = "「" + i['label'].decode('utf-8') + "」というワードの検索が下落傾向のようです。"\ "サイト分析ワードの再設定を行うことで現在の人気を把握し、"\ "そのワードをウェブサイトに増やすことでアクセス数の増加が期待できます。" res.append(dict(date=str(dt.date.today()), text=text)) return res