<commit_before># Ported from Pokemon Showdown:
# https://github.com/Zarel/Pokemon-Showdown-Client/blob/8beb563042cac1acb64ec88c552ef21fb28f0f39/js/client-teambuilder.js#L1236-L1352

HiddenPower = (if module? then require('../../../../shared/hidden_power') else window.HiddenPower ?= {})

@PokeBattle ?= {}
@PokeBattle.parseTeam = (teamString) ->
  text = teamString.split('\n')
  team = []
  for line in text
    line = line.replace(/^\s+/, '').replace(/\s+$/, '')
    if line.length == 0 || line == '---'
      if pokemon then team.push(pokemon)
      pokemon = null
    else if !pokemon
      [ pokemonLine, item ] = line.split(' @ ')
      possibleGender = pokemonLine[-4...]
      if possibleGender == ' (M)'
        gender = 'M'
        pokemonLine = pokemonLine[...-4]
      else if possibleGender == ' (F)'
        gender = 'F'
        pokemonLine = pokemonLine[...-4]
      else
        gender = null
      pokemon = {}
      # Ignore nicknames for now
      if pokemonLine.match(/.*?\s*\((.*)\)/)
        pokemonLine = RegExp.$1
      pokemon.name = pokemonLine
      pokemon.gender = gender  if gender
      pokemon.item   = item    if item
    else if startsWith(line, "Trait: ")
      pokemon.ability = without(line, "Trait: ")
    else if line == 'Shiny: Yes'
      pokemon.shiny = true
    else if startsWith(line, "Level: ")
      pokemon.level = Number(without(line, "Level: ")) || 100
    else if startsWith(line, 'Happiness: ')
      pokemon.happiness = Number(without(line, "Happiness: ")) || 0
    else if startsWith(line, 'Ability: ')
      pokemon.ability = without(line, "Ability: ")
    else if startsWith(line, 'EVs: ')
      evs = without(line, 'EVs: ').split(/\//g)
      pokemon.evs = {}
      for ev in evs
        ev = ev.replace(/^\s+/, '').replace(/\s+$/, '')
        [ numberString, rawStat ] = ev.split(/\s+/)
        pokemon.evs[statsHash[rawStat]] = Number(numberString) || 0
    else if startsWith(line, 'IVs')
      ivs = without(line, 'IVs: ').split(/\//g)
      pokemon.ivs = {}
      for iv in ivs
        iv = iv.replace(/^\s+/, '').replace(/\s+$/, '')
        [ numberString, rawStat ] = iv.split(/\s+/)
        pokemon.ivs[statsHash[rawStat]] = Number(numberString) || 0
    else if line.match(/^([A-Za-z]+) (N|n)ature/)
      pokemon.nature = RegExp.$1
    else if startsWith(line, '-') || startsWith(line, '~')
      moveName = line.substr(1).replace(/^\s+/, '')
      if startsWith(moveName, 'Hidden Power [')
        if !pokemon.ivs
          moveName.match(/Hidden Power \[\s*(.*)\s*\]/)
          hiddenPowerType = RegExp.$1.toLowerCase()
          pokemon.ivs = HiddenPower.BW.ivs[hiddenPowerType] || {}
        moveName = 'Hidden Power'
      pokemon.moves ?= []
      pokemon.moves.push(moveName)
  return team

startsWith = (string, suffix) ->
  string[...suffix.length] == suffix

without = (string, suffix) ->
  string[suffix.length...]

statsHash =
  'HP'   : 'hp'
  'Atk'  : 'attack'
  'Def'  : 'defense'
  'SAtk' : 'specialAttack'
  'SDef' : 'specialDefense'
  'Spe'  : 'speed'<commit_msg>Team parser: Refactor, no more dependency.<commit_after>HiddenPower = (if module? then require('../../../../shared/hidden_power') else window.HiddenPower ?= {})

@PokeBattle ?= {}
@PokeBattle.parseTeam = (teamString) ->
  text = teamString.split('\n')
  team = []
  pokemonRegex = /^(.*?)\s*(\(M\)|\(F\)|)?(?:\s*@\s*(.*))?$/
  pokemon = null
  for line in text
    line = line.trim()
    if line.length == 0
      pokemon = null
    else if !pokemon
      [ all, pokemonLine, gender, item ] = line.match(pokemonRegex)
      pokemon = {}
      team.push(pokemon)

      # Ignore nicknames for now
      pokemonLine    = RegExp.$1  if pokemonLine.match(/.*?\s*\((.*)\)/)
      pokemon.name   = pokemonLine.trim()
      pokemon.gender = gender[1]  if gender  # (M) and (F)
      pokemon.item   = item    if item
    else if line.match(/^(?:Trait|Ability):\s+(.*)$/i)
      pokemon.ability = RegExp.$1
    else if line.match(/^Level:\s+(.*)$/i)
      pokemon.level = Number(RegExp.$1) || 100
    else if line.match(/^Happiness:\s+(.*)$/i)
      pokemon.happiness = Number(RegExp.$1) || 0
    else if line.match(/^Shiny: Yes$/i)
      pokemon.shiny = true
    else if line.match(/^EVs: (.*)$/i)
      evs = RegExp.$1.split(/\//g)
      pokemon.evs = {}
      for ev in evs
        ev = ev.trim()
        [ numberString, rawStat ] = ev.split(/\s+/)
        pokemon.evs[statsHash[rawStat]] = Number(numberString) || 0
    else if line.match(/^IVs: (.*)$/i)
      ivs = RegExp.$1.split(/\//g)
      pokemon.ivs = {}
      for iv in ivs
        iv = iv.trim()
        [ numberString, rawStat ] = iv.split(/\s+/)
        pokemon.ivs[statsHash[rawStat]] = Number(numberString) || 0
    else if line.match(/^([A-Za-z]+) nature/i)
      pokemon.nature = RegExp.$1
    else if line.match(/^[\-\~]\s*(.*)/)
      moveName = RegExp.$1
      if /Hidden Power \[/.test(moveName)
        if !pokemon.ivs
          moveName.match(/Hidden Power \[\s*(.*)\s*\]/i)
          hiddenPowerType = RegExp.$1.toLowerCase()
          pokemon.ivs = HiddenPower.BW.ivs[hiddenPowerType] || {}
        moveName = 'Hidden Power'
      pokemon.moves ?= []
      pokemon.moves.push(moveName)
  return team

statsHash =
  'HP'   : 'hp'
  'Atk'  : 'attack'
  'Def'  : 'defense'
  'SAtk' : 'specialAttack'
  'SDef' : 'specialDefense'
  'Spe'  : 'speed'
{'fraction_non_alphanumeric': 0.18917910447761194, 'fraction_numerical': 0.011380597014925373, 'mean_word_length': 2.393037974683544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 1, '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': '357745', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1974, 'n_words': 384}