<commit_before>#!/usr/bin/env python

import networkx as nx
import json
from collections import Counter
import csv


class PrettyFloat(float):
    def __repr__(self):
        return '%.2f' % self


def pretty_floats(obj):
    if isinstance(obj, float):
        return PrettyFloat(obj)
    elif isinstance(obj, dict):
        return dict((k, pretty_floats(v)) for k, v in obj.items())
    elif isinstance(obj, (list, tuple)):
        return map(pretty_floats, obj)
    return obj


def normalize_dict_values(d):
    sum_vals = sum(d.values())
    output = {}
    for k, v in d.items():
        try:
            output[k] = float(v) / sum_vals
        except ZeroDivisionError:
            output[k] = 0
    return output


def robinhood(username, given_to_others, dict_scores):
    distribution_list = dict()

    for k, v in given_to_others.items():
        if k == username:
            # self-awarded credit
            # redistribute that credit out to the people who gave you credit
            # reference: https://stackoverflow.com/questions/21507375/how-does-pageranking-algorithm-deal-with-webpage-without-outbound-links
            distribution_list[username] = list()
            for k2, v2 in dict_scores.items():
                if username in v2 and k2 != username:
                    distribution_list[username].append(k2)

    for k, v in distribution_list.items():
        ration_per_user = given_to_others[k] / float(len(v))
        # print("%f total, %f per person" % (given_to_others[k], ration_per_user))
        for other_username in v:
            if other_username in given_to_others:
                given_to_others[other_username] += ration_per_user
            else:
                given_to_others[other_username] = ration_per_user

    return dict(given_to_others)
<commit_msg>Added ZeroDivisionError in robinhood func<commit_after>#!/usr/bin/env python

import networkx as nx
import json
from collections import Counter
import csv


class PrettyFloat(float):
    def __repr__(self):
        return '%.2f' % self


def pretty_floats(obj):
    if isinstance(obj, float):
        return PrettyFloat(obj)
    elif isinstance(obj, dict):
        return dict((k, pretty_floats(v)) for k, v in obj.items())
    elif isinstance(obj, (list, tuple)):
        return map(pretty_floats, obj)
    return obj


def normalize_dict_values(d):
    sum_vals = sum(d.values())
    output = {}
    for k, v in d.items():
        try:
            output[k] = float(v) / sum_vals
        except ZeroDivisionError:
            output[k] = 0
    return output


def robinhood(username, given_to_others, dict_scores):
    distribution_list = dict()

    for k, v in given_to_others.items():
        if k == username:
            # self-awarded credit
            # redistribute that credit out to the people who gave you credit
            # reference: https://stackoverflow.com/questions/21507375/how-does-pageranking-algorithm-deal-with-webpage-without-outbound-links
            distribution_list[username] = list()
            for k2, v2 in dict_scores.items():
                if username in v2 and k2 != username:
                    distribution_list[username].append(k2)

    for k, v in distribution_list.items():
        try:
            ration_per_user = given_to_others[k] / float(len(v))
        except ZeroDivisionError:
            ration_per_user = 0
        # print("%f total, %f per person" % (given_to_others[k], ration_per_user))
        for other_username in v:
            if other_username in given_to_others:
                given_to_others[other_username] += ration_per_user
            else:
                given_to_others[other_username] = ration_per_user

    return dict(given_to_others)

{'fraction_non_alphanumeric': 0.09481600859521891, 'fraction_numerical': 0.0083266183185603, 'mean_word_length': 2.496713615023474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7141069', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1092, 'n_words': 293}