<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
選手の情報に関する振る舞いを提供するService
"""

import locale
from datetime import datetime

from service.const import POSITION_PITCHER, POSITION_BATTER
from service.stats import Stats
from noball_django.settings import APPLICATION_NAME


__author__ = 'shinyorke'


class MLBService(object):

    # サブドメイン名
    SUB_DOMAIN = 'mlb'
    # Queryのキー名
    QUERY_KEY = 'query_word'

    # 右/左/両の略称
    SHORT_NAME_RIGHT = 'R'
    SHORT_NAME_LEFT = 'L'
    SHORT_NAME_SWITCH = 'S'

    # 通貨記号（アメリカなので＄）
    CURRENCY = '$'
    # SALARYの分母(million)
    BASE_SALARY = 1000000

    # ピタゴラス勝率のべき乗
    PYTHAGORIAN_POWER = 1.8

    def __init__(self, encode):
        locale.setlocale(locale.LC_NUMERIC, encode)

    def get_base_context(self):
        """
        戻り値定数セット
        :return: 戻り値(固定)
        """
        return {
            'APPLICATION_NAME': APPLICATION_NAME,
            'QUERY_KEY': MLBService.QUERY_KEY,
            'SUB_DOMAIN': MLBService.SUB_DOMAIN,
            'PLAYER_BATTER': POSITION_BATTER,
            'PLAYER_PITCHER': POSITION_PITCHER,
            'MENU_ENABLE': True,
        }

    def get_pythagoras_dataset(self, teams):
        """
        ピタゴラス勝率を含んだデータセットを返す
        :param teams: Team Model List
        :return: pythagoras % list
        """
        datasets = []
        for t in teams:
            d = {
                'team': t.teamID,
                'name': t.name,
                'div': t.divID,
                'rank': t.Rank,
                'g': t.G,
                'w': t.W,
                'l': t.L,
                'r': t.R,
                'er': t.ER,
                'win_p': MLBService.calc_winning_percentage(t.W, t.G),
                'pyt_p': MLBService.calc_pythagorean_expectation(t.R, t.RA)
            }
            d['pyt_p_w'] = round(t.G * d['pyt_p'], 0)
            d['pyt_p_l'] = t.G - d['pyt_p_w']
            datasets.append(d)
        return datasets

    def get_sabr_value_batter(self, player, stats, salary):
        """
        SABR Stats for batter
        :param player: Player master model
        :param stats: Player stats model
        :param salary: Player salary model
        :return: (dict)sabar stats data
        """
        # SABR Data(batter)
        _datasets = {
            'rc_list': [],
            'babip_list': [],
            'dunn_list': []
        }

        for items in stats:
            # 複数Recordあり得るので足し込む
            year = items[0]
            hr, bb, so, pa = 0, 0, 0, 0
            h, ab, sf, rc = 0, 0, 0, 0
            hbp, cs, gidp, sh, sb, ibb = 0, 0, 0, 0, 0, 0
            single, _2b, _3b = 0, 0, 0
            for batting in items[1]:
                hr = hr + batting.HR
                bb = bb + batting.BB
                so = so + batting.SO
                pa = pa + Stats.pa(batting.AB, batting.BB, batting.HBP, batting.SF, batting.SH)
                h = h + batting.H
                ab = ab + batting.AB
                sf = sf + batting.SF
                hbp = hbp + batting.HBP
                cs = cs + batting.CS
                gidp = gidp + batting.GIDP
                sh = sh + batting.SH
                sb = sb + batting.SB
                ibb = ibb + batting.IBB
                _2b = _2b + batting._2B
                _3b = _3b + batting._3B



            single = Stats.single(h, hr, _2b, _3b)
            rc = Stats.rc(h, bb, hbp, cs, gidp, sf, sh, sb, so, ab, ibb, single, _2b, _3b, hr)

            _datasets['rc_list'].append(
                {
                    'year': year,
                    'rc': Stats.rc27(rc, ab, h, sh, sf, cs, gidp)
                }
            )

            _datasets['dunn_list'].append(
                {
                    'year': year,
                    'dunn': Stats.adam_dunn_batter(hr, bb, so, pa)
                }
            )
            _datasets['babip_list'].append(
                {
                    'year': year,
                    'avg': Stats.avg(h, ab),
                    'babip': Stats.babip(h, hr, ab, so, sf)
                }
            )
        return _datasets

    def get_sabr_value_pitcher(self, player, stats, salary):
        """
        SABR Stats for batter
        :param player: Player master model<commit_msg>View the Batter and Pitcher SABR Stats(work in progress)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
選手の情報に関する振る舞いを提供するService
"""

import locale
from datetime import datetime

from service.const import POSITION_PITCHER, POSITION_BATTER
from service.stats import Stats
from noball_django.settings import APPLICATION_NAME


__author__ = 'shinyorke'


class MLBService(object):

    # サブドメイン名
    SUB_DOMAIN = 'mlb'
    # Queryのキー名
    QUERY_KEY = 'query_word'

    # 右/左/両の略称
    SHORT_NAME_RIGHT = 'R'
    SHORT_NAME_LEFT = 'L'
    SHORT_NAME_SWITCH = 'S'

    # 通貨記号（アメリカなので＄）
    CURRENCY = '$'
    # SALARYの分母(million)
    BASE_SALARY = 1000000

    # ピタゴラス勝率のべき乗
    PYTHAGORIAN_POWER = 1.8

    def __init__(self, encode):
        locale.setlocale(locale.LC_NUMERIC, encode)

    def get_base_context(self):
        """
        戻り値定数セット
        :return: 戻り値(固定)
        """
        return {
            'APPLICATION_NAME': APPLICATION_NAME,
            'QUERY_KEY': MLBService.QUERY_KEY,
            'SUB_DOMAIN': MLBService.SUB_DOMAIN,
            'PLAYER_BATTER': POSITION_BATTER,
            'PLAYER_PITCHER': POSITION_PITCHER,
            'MENU_ENABLE': True,
        }

    def get_pythagoras_dataset(self, teams):
        """
        ピタゴラス勝率を含んだデータセットを返す
        :param teams: Team Model List
        :return: pythagoras % list
        """
        datasets = []
        for t in teams:
            d = {
                'team': t.teamID,
                'name': t.name,
                'div': t.divID,
                'rank': t.Rank,
                'g': t.G,
                'w': t.W,
                'l': t.L,
                'r': t.R,
                'er': t.ER,
                'win_p': MLBService.calc_winning_percentage(t.W, t.G),
                'pyt_p': MLBService.calc_pythagorean_expectation(t.R, t.RA)
            }
            d['pyt_p_w'] = round(t.G * d['pyt_p'], 0)
            d['pyt_p_l'] = t.G - d['pyt_p_w']
            datasets.append(d)
        return datasets

    def get_sabr_value_batter(self, player, stats, salary):
        """
        SABR Stats for batter
        :param player: Player master model
        :param stats: Player stats model
        :param salary: Player salary model
        :return: (dict)sabar stats data
        """
        # SABR Data(batter)
        _datasets = {
            'rc_list': [],
            'babip_list': [],
            'dunn_list': []
        }

        for items in stats:
            # 複数Recordあり得るので足し込む
            year = items[0]
            hr, bb, so, pa = 0, 0, 0, 0
            h, ab, sf, rc = 0, 0, 0, 0
            hbp, cs, gidp, sh, sb, ibb = 0, 0, 0, 0, 0, 0
            single, _2b, _3b = 0, 0, 0
            for batting in items[1]:
                hr = hr + batting.HR
                bb = bb + batting.BB
                so = so + batting.SO
                h = h + batting.H
                ab = ab + batting.AB
                sf = sf + batting.SF
                hbp = hbp + batting.HBP
                cs = cs + batting.CS
                gidp = gidp + batting.GIDP
                sh = sh + batting.SH
                sb = sb + batting.SB
                ibb = ibb + batting.IBB
                _2b = _2b + batting._2B
                _3b = _3b + batting._3B



            pa = pa + Stats.pa(pa, bb, hbp, sf, sh)
            single = Stats.single(h, hr, _2b, _3b)
            rc = Stats.rc(h, bb, hbp, cs, gidp, sf, sh, sb, so, ab, ibb, single, _2b, _3b, hr)

            _datasets['rc_list'].append(
                {
                    'year': year,
                    'rc': Stats.rc27(rc, ab, h, sh, sf, cs, gidp)
                }
            )

            _datasets['dunn_list'].append(
                {
                    'year': year,
                    'dunn': Stats.adam_dunn_batter(hr, bb, so, pa)
                }
            )
            _datasets['babip_list'].append(
                {
                    'year': year,
                    'avg': Stats.avg(h, ab),
                    'babip': Stats.babip(h, hr, ab, so, sf)
                }
            )
        return _datasets

    def get_sabr_value_pitcher(self, player, stats, salary):
        """
        SABR Stats for batter
        :param player: Player master model
{'fraction_non_alphanumeric': 0.11329818394844757, 'fraction_numerical': 0.010310486233157586, 'mean_word_length': 1.3502202643171806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3211791', 'n_tokens_mistral': 3209, 'n_tokens_neox': 2937, 'n_words': 647}