Advertisement
Guest User

set_sorter_vapicuno

a guest
May 5th, 2019
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. ### Written by vapicuno, 2019/05/05
  2. ### python 3.5, numpy 1.11.1
  3. ### Takes in fin which is in showdown teambuilder format
  4. ### Spits out fout which is a list of sets in alphabetical order.
  5. ### Within each pokemon, sets are ordered by frequency of use
  6.  
  7. import numpy as np
  8.  
  9. fin = 'linear_builder.txt'
  10. fout = 'linear_builder_sets.txt'
  11.  
  12. setlist = list()
  13. f = open(fin)
  14. line = f.readline()
  15. buffer = line
  16. linestatus = 0; # 0 = team not found, 1 = team found
  17. while line:
  18. line = f.readline()
  19. if linestatus == 0: # If importable has not been found
  20. mark = line.find('Ability:') # Use Ability to find importable set
  21. if mark == 0: # Find pokemon name
  22. pos2 = buffer.find(' (F) ')
  23. if pos2 == -1:
  24. pos2 = buffer.find(' (M) ')
  25. if pos2 == -1:
  26. pos2 = buffer.find(' @ ')
  27. if pos2 == -1:
  28. pos2 = len(buffer) - 2 # -2 account for newline
  29. if buffer[pos2-1] == ')':
  30. pos2 = pos2 - 1;
  31. pos1 = pos2 - 1;
  32. while buffer[pos1] != '(':
  33. pos1 = pos1 - 1;
  34. pos1 = pos1 + 1;
  35. else:
  36. pos1 = 0;
  37. positem = buffer.find(' @ ',pos2)
  38. mon = buffer[pos1:pos2]
  39. if positem != -1:
  40. itemtxt = buffer[positem:]
  41. else:
  42. itemtxt = ''
  43. montxt = mon + itemtxt # Make Name @ Item
  44. linestatus = 1
  45. buffer = line
  46. if linestatus == 1: # If importable has been found
  47. if line == '\n': # If linebreak has been found
  48. setlist.append(montxt) # Save as a string
  49. linestatus = 0
  50. else:
  51. montxt = montxt + line # Add remaining importable lines
  52. f.close()
  53. setlist.sort(); # Final sorted list of sets in string format
  54.  
  55. ## Sequentially runs through setlist
  56. ## For each mon, arrange sets by frequency of usage
  57. ## Writes to txt
  58. setlistsortedreduced = list() # final list
  59. subsetlist = list() # list of strings of full sets
  60. submoveslist = list() # List of sets, because moves are unordered
  61. subsetcount = list() # frequency list
  62. chosenmon = '';
  63. setlistlen = len(setlist)
  64. for n in np.arange(setlistlen):
  65. currentset = setlist[n]
  66. currentset = currentset.replace('Shiny: Yes \n','')
  67. currentmoves = set()
  68. nlindex = list() # newline indices
  69. lenset = len(currentset)
  70. postemp = lenset
  71. while len(nlindex) < 5:
  72. nlindex.append(currentset.rfind('\n',0,postemp)) # Extract move indices
  73. postemp = nlindex[-1]
  74. for ii in np.arange(0,4):
  75. currentmoves.add(currentset[nlindex[4-ii]+4:nlindex[3-ii]]) # Extract moves
  76. partition = nlindex[4]
  77.  
  78. posnewline = currentset.find('\n')
  79. posspace = currentset.find(' @ ')
  80. currentmon = currentset[:min(posspace,posnewline)]
  81. if chosenmon == currentmon:
  82. # determine set equivalence
  83. matchindex = -1; # matching set index
  84. idx = 0; # iterator
  85. while matchindex < 0 and idx < len(subsetlist):
  86. if subsetlist[idx][0:partition] == currentset[0:partition]:
  87. if submoveslist[idx] == currentmoves:
  88. matchindex = idx
  89. subsetcount[matchindex] += 1
  90. idx += 1
  91. if matchindex < 0:
  92. subsetlist.append(currentset)
  93. submoveslist.append(currentmoves)
  94. subsetcount.append(1)
  95. if chosenmon != currentmon or n == setlistlen-1:
  96. # all sets for a pokemon completed
  97. # sort the list by number of replicates
  98. # append to final list
  99. subsetsortindex = np.argsort(np.array(subsetcount))
  100. subsetsortindexrev = subsetsortindex[::-1]
  101. subsetsortlist = [subsetlist[i] for i in subsetsortindexrev]
  102. setlistsortedreduced.extend(subsetsortlist)
  103. # initialize new mon
  104. subsetlist = list()
  105. submoveslist = list()
  106. subsetcount = list()
  107. chosenmon = currentmon
  108. # append new mon and set
  109. subsetlist.append(currentset)
  110. submoveslist.append(currentmoves)
  111. subsetcount.append(1)
  112.  
  113. f = open(fout,'w')
  114. for s in setlistsortedreduced:
  115. f.write(s)
  116. f.write('\n')
  117. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement