Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Written by vapicuno, 2019/05/05
- ### python 3.5, numpy 1.11.1
- ### Takes in fin which is in showdown teambuilder format
- ### Spits out fout which is a list of sets in alphabetical order.
- ### Within each pokemon, sets are ordered by frequency of use
- import numpy as np
- fin = 'linear_builder.txt'
- fout = 'linear_builder_sets.txt'
- setlist = list()
- f = open(fin)
- line = f.readline()
- buffer = line
- linestatus = 0; # 0 = team not found, 1 = team found
- while line:
- line = f.readline()
- if linestatus == 0: # If importable has not been found
- mark = line.find('Ability:') # Use Ability to find importable set
- if mark == 0: # Find pokemon name
- pos2 = buffer.find(' (F) ')
- if pos2 == -1:
- pos2 = buffer.find(' (M) ')
- if pos2 == -1:
- pos2 = buffer.find(' @ ')
- if pos2 == -1:
- pos2 = len(buffer) - 2 # -2 account for newline
- if buffer[pos2-1] == ')':
- pos2 = pos2 - 1;
- pos1 = pos2 - 1;
- while buffer[pos1] != '(':
- pos1 = pos1 - 1;
- pos1 = pos1 + 1;
- else:
- pos1 = 0;
- positem = buffer.find(' @ ',pos2)
- mon = buffer[pos1:pos2]
- if positem != -1:
- itemtxt = buffer[positem:]
- else:
- itemtxt = ''
- montxt = mon + itemtxt # Make Name @ Item
- linestatus = 1
- buffer = line
- if linestatus == 1: # If importable has been found
- if line == '\n': # If linebreak has been found
- setlist.append(montxt) # Save as a string
- linestatus = 0
- else:
- montxt = montxt + line # Add remaining importable lines
- f.close()
- setlist.sort(); # Final sorted list of sets in string format
- ## Sequentially runs through setlist
- ## For each mon, arrange sets by frequency of usage
- ## Writes to txt
- setlistsortedreduced = list() # final list
- subsetlist = list() # list of strings of full sets
- submoveslist = list() # List of sets, because moves are unordered
- subsetcount = list() # frequency list
- chosenmon = '';
- setlistlen = len(setlist)
- for n in np.arange(setlistlen):
- currentset = setlist[n]
- currentset = currentset.replace('Shiny: Yes \n','')
- currentmoves = set()
- nlindex = list() # newline indices
- lenset = len(currentset)
- postemp = lenset
- while len(nlindex) < 5:
- nlindex.append(currentset.rfind('\n',0,postemp)) # Extract move indices
- postemp = nlindex[-1]
- for ii in np.arange(0,4):
- currentmoves.add(currentset[nlindex[4-ii]+4:nlindex[3-ii]]) # Extract moves
- partition = nlindex[4]
- posnewline = currentset.find('\n')
- posspace = currentset.find(' @ ')
- currentmon = currentset[:min(posspace,posnewline)]
- if chosenmon == currentmon:
- # determine set equivalence
- matchindex = -1; # matching set index
- idx = 0; # iterator
- while matchindex < 0 and idx < len(subsetlist):
- if subsetlist[idx][0:partition] == currentset[0:partition]:
- if submoveslist[idx] == currentmoves:
- matchindex = idx
- subsetcount[matchindex] += 1
- idx += 1
- if matchindex < 0:
- subsetlist.append(currentset)
- submoveslist.append(currentmoves)
- subsetcount.append(1)
- if chosenmon != currentmon or n == setlistlen-1:
- # all sets for a pokemon completed
- # sort the list by number of replicates
- # append to final list
- subsetsortindex = np.argsort(np.array(subsetcount))
- subsetsortindexrev = subsetsortindex[::-1]
- subsetsortlist = [subsetlist[i] for i in subsetsortindexrev]
- setlistsortedreduced.extend(subsetsortlist)
- # initialize new mon
- subsetlist = list()
- submoveslist = list()
- subsetcount = list()
- chosenmon = currentmon
- # append new mon and set
- subsetlist.append(currentset)
- submoveslist.append(currentmoves)
- subsetcount.append(1)
- f = open(fout,'w')
- for s in setlistsortedreduced:
- f.write(s)
- f.write('\n')
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement