def p_name(self): # create a short name of the type that is used in batting/pitching notes # 1st initial, last name, no accents p_name = format(replace_accents(self.first_name[0])) if self.middle_name: p_name = '{} {}'.format(p_name, replace_accents(self.middle_name)) p_name = '{} {}'.format(p_name, replace_accents(self.last_name)) if self.suffix: p_name = '{} {}'.format(p_name, replace_accents(self.suffix)) p_name = p_name.rstrip('.') return p_name
def get_player_from_order(order, pname, stat_name, game_id): pname = replace_accents(pname) # clean up yahoo's mess if game_id in ['mlb.g.380405123', 'mlb.g.380330106', 'mlb.g.380401306', 'mlb.g.380401206'] \ and pname == 'F Rivero': pname = 'F Vazquez' # 8/23/18 O P??rez elif game_id == 'mlb.g.380823102' and pname.startswith('O P') and pname.endswith('ez'): pname = 'O Perez' # 8/23/18 J Garc??a elif game_id == 'mlb.g.380823128' and pname.startswith('J Garc') and pname.endswith('a'): pname = 'J Garcia' pnames = [pname] if pname == 'M Wright Jr': pnames.append('M Wright') elif pname == 'V Nuno': pnames.append('V Nuno III') elif pname == 'D Poncedeleon': pnames.append('D Ponce de Leon') for pname in pnames: try: return order[pname] except KeyError: pass raise Exception('{} could not find {} in {}'.format(stat_name, pnames, order.keys()))
def get_or_create_player(session, yahoo_id, team, data, game_date): try: player = session.query(Player).filter( Player.yahoo_id == yahoo_id).one() if player.team_id != team.id and (player.last_team_change is None or player.last_team_change < game_date): log.info("{} changing from {} to {} on {} last change {}".format( player.name, player.team.name, team.name, game_date, player.last_team_change)) player.team = team player.last_team_change = game_date player.number = data['uniform_number'] player.date_updated = datetime.utcnow() except NoResultFound: log.info("Couldn't find {}, creating new player.".format(yahoo_id)) # TODO: DOB? player = Player(yahoo_id=yahoo_id, team=team, name=data['display_name'], first_name=data['first_name'], middle_name=None, last_name=data['last_name'], suffix=None, number=data['uniform_number'], last_team_change=game_date, lname=compress_string( replace_accents(data['display_name']))) player.pname = player.p_name() session.add(player) log.debug('Created: {} | {} | {} | {} | {}|{}|{}|{}'.format( player.yahoo_id, player.team.name, player.number, player.name, player.first_name, player.middle_name, player.last_name, player.suffix)) session.commit() return player
return psycopg2.connect(host='localhost', dbname='fant_b', user='******', password='') connection = get_conn() cursor = connection.cursor() sql = "SELECT id, name FROM player;" cursor.execute(sql) for tpl in cursor.fetchall(): id = tpl[0] name = tpl[1] lname = replace_accents(name) lname = compress_string(lname) cursor2 = connection.cursor() sql = "UPDATE player SET lname = '{}' WHERE id = {};".format(lname, id) cursor2.execute(sql) log.debug('{} => {}'.format(name, lname)) connection.commit() cursor2.close() cursor.close() connection.close()
def parse_batting_notes(away_batting, home_batting, pitching_notes, lineups, game_id): away_batting_order = parse_lineup(lineup=lineups['away_lineup_order']['B'], ltype='away batter', game_id=game_id) home_batting_order = parse_lineup(lineup=lineups['home_lineup_order']['B'], ltype='home batter', game_id=game_id) bstats = {} for i in range(0, 2): if i == 0: batting_order = away_batting_order ltype = 'away' source = away_batting else: batting_order = home_batting_order ltype = 'home' source = home_batting # 2B, 3B, GIDP stypes = ['2B', '3B', 'GIDP', 'SF'] for j in range(0, 4): stype = stypes[j] index = 0 start_index = -1 for dct in source: if dct['header'] == stype: start_index = index break index += 1 if start_index == -1: #log.info('No {} for {} team'.format(stype, ltype)) continue #log.debug('found {} at {} - {}'.format(stype, start_index, source[start_index]['text'])) source[start_index]['text'] = replace_accents(source[start_index]['text']) if stype in ['GIDP', 'SF']: raw = source[start_index]['text'].split(',') else: raw = source[start_index]['text'].split('),') #log.debug(raw) for item in raw: increment = 1 # "header": "2B", # "text": "K Bryant (1), W Contreras (1), J Heyward (1), T La Stella (1)" if stype in ['2B', '3B']: # there's a following digit if > 1, e.g. X Bogaerts 2 (2) m = re.match('(.*) (\d+) \(\d+.*', item) if m: bname = m.group(1).lstrip() increment = int(m.group(2)) else: m = re.match('(.*) \(\d+.*', item) if m: bname = m.group(1).lstrip() else: raise Exception('{} regex failed on {} [{}]'.format(stype, item, raw)) # "header": "GIDP", # "text": "J Bour, M Rojas" else: m = re.match('(.*) (\d+)', item) # there's a following digit if > 1, e.g. X Bogaerts 2 if m: bname = m.group(1).lstrip() increment = int(m.group(2)) else: bname = item.lstrip() # 8/23/18 Y D??az, E N????ez if game_id == 'mlb.g.380823102': if bname.startswith('Y D') and bname.endswith('az'): bname = 'Y Diaz' elif bname.startswith('E N') and bname.endswith('ez'): bname = 'E Nunez' # 8/23/18 E Su??rez, J B??ez elif game_id == 'mlb.g.380823116': if bname.startswith('E Su') and bname.endswith('ez'): bname = 'E Suarez' elif bname.startswith('J B') and bname.endswith('ez'): bname = 'J Baez' # 8/23/18 A Garc??a elif game_id == 'mlb.g.380823106': if bname.startswith('A Garc') and bname.endswith('a'): bname = 'A Garcia' elif game_id == 'mlb.g.380912124' and bname == 'D Poncedeleon': bname = 'D Ponce de Leon' if bname not in batting_order.keys(): raise Exception('Could not find {} {} in batting_order {}'. format(stype, bname, batting_order.keys())) batter = batting_order[bname] #log.debug('{} {}'.format(stype, batter.name)) if batter.id not in bstats.keys(): bstats[batter.id] = { '2B': 0, '3B': 0, 'SF': 0, 'HBP': 0, 'GIDP': 0, } bstats[batter.id][stype] += increment # HBP away_pitching_order = parse_lineup(lineup=lineups['away_lineup_order']['P'], ltype='away pitcher', game_id=game_id) home_pitching_order = parse_lineup(lineup=lineups['home_lineup_order']['P'], ltype='home pitcher', game_id=game_id) index = 0 start_index = -1 for dct in pitching_notes: if dct['header'] == 'HBP': start_index = index break index += 1 if start_index > -1: pitching_notes[start_index]['text'] = replace_accents(pitching_notes[start_index]['text']) raw = pitching_notes[start_index]['text'].split(',') #log.debug(raw) for item in raw: # { # "header": "HBP", # "text": "D Dietrich (by M Montgomery), J B\u00e1ez (by J Ure\u00f1a), A Rizzo (by J Ure\u00f1a), A Russell (by J Ure\u00f1a)" # }, increment = 1 m = re.match('(.*) (\d+) \(by (.*)\)', item) if m: bname = m.group(1).lstrip() increment = int(m.group(2).lstrip()) pname = m.group(3).lstrip() else: m = re.match('(.*) \(by (.*)\)', item) if m: bname = m.group(1).lstrip() pname = m.group(2).lstrip() else: raise Exception('{} regex failed on {}'.format('HBP', item)) # 8/23/18 R Acu??a Jr if game_id == 'mlb.g.380823128': if bname[:5] == 'R Acu' and bname[-2:] == 'Jr': bname = 'R Acuna Jr' elif game_id in['mlb.g.380403118', 'mlb.g.380419106', 'mlb.g.380527130', 'mlb.g.380624115', 'mlb.g.380803113', \ 'mlb.g.380902107', 'mlb.g.380915101']: if pname == 'M Wright Jr': pname = 'M Wright' elif game_id in['mlb.g.380821119']: if pname == 'D Poncedeleon': pname = 'D Ponce de Leon' elif game_id in['mlb.g.380905114', 'mlb.g.380926130']: if pname == 'V Nuno': pname = 'V Nuno III' if bname in away_batting_order.keys() and pname in home_pitching_order.keys(): batter = away_batting_order[bname] elif bname in home_batting_order.keys() and pname in away_pitching_order.keys(): batter = home_batting_order[bname] else: log.info('AB {}'.format(away_batting_order.keys())) log.info('HP {}'.format(home_pitching_order.keys())) log.info('HB {}'.format(home_batting_order.keys())) log.info('AP {}'.format(away_pitching_order.keys())) raise Exception('Could not find {} in batting orders and {} in pitching orders'.format(bname, pname)) if batter.id not in bstats.keys(): bstats[batter.id] = { '2B': 0, '3B': 0, 'SF': 0, 'HBP': 0, 'GIDP': 0, } bstats[batter.id]['HBP'] += increment else: #log.info('No {} found'.format('HBP')) pass return bstats
def parse_pitching_notes(pitching_notes, lineups, game_id): pitching_order = parse_lineup( lineup=lineups['away_lineup_order']['P'] + lineups['home_lineup_order']['P'], ltype='pitching lineups', game_id=game_id ) # Pitches-strikes pstats = {} index = 0 start_index = -1 for dct in pitching_notes: if dct['header'] == 'Pitches-strikes': start_index = index break index += 1 if start_index == -1: raise Exception('Pitches-strikes not found in pitching_notes') pitching_notes[start_index]['text'] = replace_accents(pitching_notes[start_index]['text']) raw = pitching_notes[start_index]['text'].split(',') pitchers_count = len(raw) for item in raw: m = re.match('(.*) (\d+)-(\d+).*', item) if not m: raise Exception('Pitches-strikes regex failed on {}'.format(item)) pname = m.group(1).lstrip() pitcher = get_player_from_order( order=pitching_order, pname=pname, stat_name='Pitches-strikes', game_id=game_id ) pstats[pitcher.id] = { 'pitches': m.group(2), 'strikes': m.group(3), 'ground': 0, 'fly': 0, 'batters': 0, } # Ground balls-fly balls seen = [] start_index += 1 if pitching_notes[start_index]['header'] != 'Ground balls-fly balls': raise Exception('Ground balls-fly balls not found') raw = pitching_notes[start_index]['text'].split(',') if pitchers_count != len(raw): log.warning('Expecting {} pitchers in Ground balls-fly balls, found {}'.format(pitchers_count, len(raw))) for item in raw: m = re.match('(.*) (\d+)-(\d+).*', item) if not m: raise Exception('Ground balls-fly balls regex failed on {}'.format(item)) pname = m.group(1).lstrip() pitcher = get_player_from_order( order=pitching_order, pname=pname, stat_name='Pitches-strikes', game_id=game_id ) pstats[pitcher.id]['ground'] = m.group(2) pstats[pitcher.id]['fly'] = m.group(3) # Batters faced start_index += 1 if pitching_notes[start_index]['header'] != 'Batters faced': raise Exception('Batters faced not found') raw = pitching_notes[start_index]['text'].split(',') # if pitchers_count != len(raw): # log.warning('Expecting {} pitchers in Batters faced, found {}'.format(pitchers_count, len(raw))) for item in raw: # it appears yahoo puts no number if the batters faced is just 1 batters = 1 m = re.match('(.*) (\d+)', item) if m: batters = m.group(2) pname = m.group(1).lstrip() # else: # pname = item.lstrip() # log.warning('Batters faced regex failed on {}'.format(item)) pitcher = get_player_from_order( order=pitching_order, pname=pname, stat_name='batters', game_id=game_id ) pstats[pitcher.id]['batters'] = batters return pstats
def process_voice_message(message): if not message.text: bot.reply_to(message, 'А где текст??!') return # normalize the input # convert pluses into accents text_with_accents = pluses_to_accents(message.text) # replace accents into pluses text_normalized = replace_accents(text_with_accents) if not text_normalized: bot.reply_to(message, 'Ошибка: а где текст?') return # add a dot if it is not set last_char = text_normalized[-1] if last_char not in ['.', '!', '?']: text_normalized = text_normalized + '.' text_len = len(text_normalized) if text_len > 150: bot.reply_to( message, f'Ошибка: ваш текст больше 150 символов, а именно {text_len}') return bot.reply_to( message, f'Текст c ударениями:\n\n{text_with_accents}\n\nНормализованный текст:\n\n{text_normalized}' ) # do the synthesizing audios = apply_tts(texts=[text_normalized], model=model, sample_rate=SAMPLE_RATE, symbols=SYMBOLS, device=device) s = 0 for n, audio_tensor in enumerate(audios): # form the filename filename = dirname(abspath('__file__')) + f'/files/file_{n}.wav' # save to the disk soundfile_backend.save(filename, audio_tensor, SAMPLE_RATE) # send back to the user audio = open(filename, 'rb') bot.send_voice(message.chat.id, audio) # remove WAV file remove(filename) # increment the counter s = s + 1 if s == 0: bot.reply_to(message, "Ничего не смог синтезировать :(")