def covid_ratio(): df = {} rows = [] cols = [] for item in db.view('views/covid-19', group=True, group_level=2, stale="update_after"): city = item.key[0] category = item.key[1] value = item.value if category == 'covid-19': df[city] = value for key, value in df.items(): for k, v in total_tw().items(): if key == k: covid_ratio = value / v rows.append({'c': [{'v': key}, {'v': covid_ratio}]}) pairs = [('', 'Tweet', 'string'), ('', 'COVID-19', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def map_data(): rows = [] for item in db.view('coordinates/sentiment'): for each in item.key[:-1]: rows.append([each[0], each[1], item.key[-1]]) data = {'tweets': rows} return jsonify(data)
def google_map_data(): geo_list = [] for row in list( db.view('mapviews/latestTweets', descending=True, limit=200)): geo_list.append( [row.value['geo'][1], row.value['geo'][0], row.value['text']]) data = {'geo': geo_list} return jsonify(data)
def total_tw_food(): food_dict = {} for item in db.view('views/food_view', group=True, group_level=1, stale="update_after"): city = " ".join(item.key) food_dict[city] = item.value return food_dict
def google_map_data(): geo_list = [] for row in list(db.view('mapviews/latestTweets', descending=True, limit=200)): geo_list.append([row.value['geo'][1], row.value['geo'][0], row.value['text']]) data = { 'geo': geo_list } return jsonify(data)
def time_category(): rows = [] col = [] for item in db.view('general_views/time_view', group=True, group_level=1): rows.append({'c': [{'v': " ".join(item.key)}, {'v': item.value}]}) pairs = [('', 'Tweet', 'string'), ('', 'tweet number', 'number')] for pair in pairs: col.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': col, 'rows': rows} return jsonify(response)
def sentiment_composition(): rows = [] cols = [] for item in db.view('sentiment/new-view', group=True, group_level=1): if item.key is not None: rows.append({'c': [{'v': item.key}, {'v': item.value}]}) pairs = [('', 'Tweet', 'string'), ('', 'Number', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def device_data(): rows = [] for row in list(db.view('mapviews/devices', group=True)): if row.value >= 100: rows.append({'c': [{'v': BeautifulSoup(row.key).a.text}, {'v': row.value}]}) response = { 'cols': __cols([('Device', 'string'), ('Tweets', 'number')]), 'rows': rows } return jsonify(response)
def sentiment_data(): counter = Counter() for row in list(db.view('mapviews/c2e2')): if row.value: counter[row.value['sentiScore']] += 1 counter.pop(0) # exclude neu(0) data = { 'cols': __cols([('Score', 'number'), ('Count', 'number')]), 'rows': [{'c': [{'v': key}, {'v': val}]} for (key, val) in counter.iteritems()] } return jsonify(data)
def lang_data(m=None): rows = [] for row in list(db.view('mapviews/language', group=True)): if m and row.key in ['en', 'und']: continue if row.value >= 100: rows.append( {'c': [{'v': lang_codes[row.key]['name'] if row.key in lang_codes else row.key}, {'v': row.value}]}) response = { 'cols': __cols([('Language', 'string'), ('Tweets', 'number')]), 'rows': rows } return jsonify(response)
def food_type_ratio(): rows = [] cols = [] for item in db.view('general_views/food_view', group=True, group_level=1, stale="update_after"): rows.append({'c': [{'v': " ".join(item.key)}, {'v': item.value}]}) pairs = [('', 'Food Type', 'string'), ('', 'Amount', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def food_ratio(): rows = [] cols = [] for item in db.view('views/food_view', group=True, group_level=1): city = item.key[0] value = item.value for k, v in total_tw().items(): if city == k: food_ratio = value / v rows.append({'c': [{'v': city}, {'v': food_ratio}]}) pairs = [('', 'Tweet', 'string'), ('', 'Food', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def device_data(): rows = [] for row in list(db.view('mapviews/devices', group=True)): if row.value >= 100: rows.append({ 'c': [{ 'v': BeautifulSoup(row.key).a.text }, { 'v': row.value }] }) response = { 'cols': __cols([('Device', 'string'), ('Tweets', 'number')]), 'rows': rows } return jsonify(response)
def time_line_data(): pos_counter, neg_counter, neu_counter = Counter(), Counter(), Counter() for row in list(db.view('chicago/chicagoBulls')): sentiment = row.value['senti'] time_stamp = calendar.timegm((datetime.fromtimestamp(row.key)).replace( hour=0, minute=0, second=0, microsecond=0).timetuple()) * 1000 if sentiment == 'pos': pos_counter[time_stamp] += 1 elif sentiment == 'neg': neg_counter[time_stamp] += 1 else: neu_counter[time_stamp] += 1 return jsonify({ "positive": pos_counter.items(), "negative": neg_counter.items(), "neutral": neu_counter.items() })
def time_line_data(): pos_counter, neg_counter, neu_counter = Counter(), Counter(), Counter() for row in list(db.view('chicago/chicagoBulls')): sentiment = row.value['senti'] time_stamp = calendar.timegm( (datetime.fromtimestamp(row.key)).replace(hour=0, minute=0, second=0, microsecond=0).timetuple()) * 1000 if sentiment == 'pos': pos_counter[time_stamp] += 1 elif sentiment == 'neg': neg_counter[time_stamp] += 1 else: neu_counter[time_stamp] += 1 return jsonify({ "positive": pos_counter.items(), "negative": neg_counter.items(), "neutral": neu_counter.items() })
def time_ratio(): df = {} rows = [] cols = [] for item in db.view('views/time_view', group=True, group_level=2, stale="update_after"): city = item.key[0] category = item.key[1] value = item.value if category != None: if city not in df: df[city] = [value] else: df[city] += [value] for key, value in df.items(): total = value[0] + value[1] + value[2] + value[3] afternoon_ratio = value[0] / total evening_ratio = value[1] / total morning_ratio = value[2] / total night_ratio = value[3] / total rows.append({ 'c': [{ 'v': key }, { 'v': morning_ratio }, { 'v': afternoon_ratio }, { 'v': evening_ratio }, { 'v': night_ratio }] }) pairs = [('', 'Tweet', 'string'), ('', 'Morning', 'number'), ('', 'Afternoon', 'number'), ('', 'Evening', 'number'), ('', 'Night', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def sentiment_ratio(): df = {} rows = [] cols = [] for item in db.view('views/sentiment_view', group=True, group_level=2, stale="update_after"): city = item.key[0] category = item.key[1] value = item.value if city != 'others': if category != None: if city not in df: df[city] = [value] else: df[city] += [value] for key, value in df.items(): total = value[0] + value[1] + value[2] negative_ratio = value[0] / total #negative_r = '{:.0f}%'.format(negative_ratio*100) neutral_ratio = value[1] / total #neutral_r = '{:.0f}%'.format(neutral_ratio*100) positive_ratio = value[2] / total #positive_r = '{:.0f}%'.format(positive_ratio*100) rows.append({ 'c': [{ 'v': key }, { 'v': negative_ratio }, { 'v': neutral_ratio }, { 'v': positive_ratio }] }) pairs = [('', 'Tweet', 'string'), ('', 'Negative', 'number'), ('', 'Neutral', 'number'), ('', 'Positive', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def sentiment_data(): counter = Counter() for row in list(db.view('mapviews/c2e2')): if row.value: counter[row.value['sentiScore']] += 1 counter.pop(0) # exclude neu(0) data = { 'cols': __cols([('Score', 'number'), ('Count', 'number')]), 'rows': [{ 'c': [{ 'v': key }, { 'v': val }] } for (key, val) in counter.iteritems()] } return jsonify(data)
def Adelaide_ratio(): rows = [] cols = [] total_num_food = 0 for k, v in total_tw_food().items(): total_num_food += v for item in db.view('views/food_view', group=True, group_level=3): if item.key[0] == 'Adelaide': for city, v in total_tw_food().items(): if city == item.key[0]: ratio = item.value / v rows.append({'c': [{'v': item.key[2]}, {'v': ratio}]}) pairs = [('', 'Food Type', 'string'), ('', 'Food Type Proportion in Adelaide', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def lang_data(m=None): rows = [] for row in list(db.view('mapviews/language', group=True)): if m and row.key in ['en', 'und']: continue if row.value >= 100: rows.append({ 'c': [{ 'v': lang_codes[row.key]['name'] if row.key in lang_codes else row.key }, { 'v': row.value }] }) response = { 'cols': __cols([('Language', 'string'), ('Tweets', 'number')]), 'rows': rows } return jsonify(response)
def food_category_ratio(): df = {} rows = [] cols = [] for item in db.view('views/food_view', group=True, group_level=2, stale="update_after"): city = item.key[0] category = item.key[1] value = item.value if category != None: if city not in df: df[city] = [value] else: df[city] += [value] for key, value in df.items(): total = value[0] + value[1] healthy_ratio = value[0] / total #healthy_r = '{:.0f}%'.format(healthy_ratio*100) unhealthy_ratio = value[1] / total #unhealthy_r = '{:.0f}%'.format(unhealthy_ratio*100) rows.append( {'c': [{ 'v': key }, { 'v': healthy_ratio }, { 'v': unhealthy_ratio }]}) pairs = [('', 'Tweet', 'string'), ('', 'Healthy Food', 'number'), ('', 'Unhealthy Food', 'number')] for pair in pairs: cols.append({'id': pair[0], 'label': pair[1], 'type': pair[2]}) response = {'cols': cols, 'rows': rows} return jsonify(response)
def total_tw(): sentiment_dict = {} for item in db.view('views/sentiment_view', group=True, group_level=1): city = " ".join(item.key) sentiment_dict[city] = item.value return sentiment_dict