Skip to content

Instantly share code, notes, and snippets.

@kurokikaze

kurokikaze/process.py

Last active Aug 29, 2015
Embed
What would you like to do?
Переработка текстовых экспортированных кубов с CubeTutor в формат, подходящий для SPMF
import re, os
from random import shuffle
r_unwanted = re.compile("[\n\t\r]")
r_file = re.compile("cube([\d]+).txt") # шаблон имени файла
dic = dict()
num = 1000 # с чего начинаем нумерацию карт
censored = ["Temple Garden",
"Blood Crypt",
"Steam Vents",
"Watery Grave",
"Godless Shrine",
"Stomping Ground",
"Hallowed Fountain",
"Sacred Foundry",
"Overgrown Tomb",
"Breeding Pool",
"Lightning Bolt",
"Oblivion Ring",
"Swords to Plowshares",
"Mulldrifter",
"Mana Leak",
"Counterspell",
"Eternal Witness"]
def f7(seq):
''' Делаем массив уникальным '''
seen = set()
seen_add = seen.add
return [ x for x in seq if x not in seen and not seen_add(x)]
def processFile(number):
''' Перерабатываем отдельный файл куба в строку айдишников '''
global dic
global num
f = open("cubes\cube" + str(number) + ".txt",'r')
cards = f.readlines()
shuffle(cards)
clean_cards = []
for card in cards:
card = card.strip()
if (card not in censored):
if (card in dic):
cardnum = dic[card]
else:
cardnum = num
dic[card] = num
num = num + 1
clean_cards.append(str(cardnum))
clean_cards = f7(clean_cards)
clean_cards.sort()
return ' '.join(clean_cards)
def dumpDic():
''' Сохраняем словарь на диск '''
global dic
strings = []
dic_file = open("dictionary.txt", "w")
for key in dic.keys():
dic_file.write(key + ' ' + str(dic[key]) + '\n')
cubes = []
def loadCubes(path):
''' Загружаем файлы '''
global cubes
files = os.listdir(path)
shuffle(files)
for file in files:
match = r_file.match(file)
if match:
data = processFile(int(match.group(1)))
if len(data) > 0:
cubes.append(data)
loadCubes(".\cubes")
cubes_file = open("cubes.txt", "w")
cubes_file.write('\n'.join(cubes))
dumpDic()
import re
dic = dict()
def loadDic():
''' Загружаем словарь '''
global dic
f = open("dictionary.txt",'r')
records = f.readlines()
for record in records:
parts = record.rsplit(' ', 1)
dic[int(parts[1])] = parts[0]
def loadResults(file):
''' Загружаем файл правил, выданный SPMF и сохраняем обработанный результат в translated.txt '''
global dic
f = open(file, 'r')
o = open('translated.txt', 'w')
records = f.readlines()
for record in records:
parts = record.rsplit('#SUP: ')
support = int(parts[1].split(' ')[0])
card_ids = parts[0].strip(' ').split(' ')
cards = []
for card_id in card_ids:
if re.match("^\d+$", card_id) and int(card_id) in dic:
cards.append(dic[int(card_id)])
else:
cards.append(card_id)
o.write('(' + str(support) + ') ' + ' '.join(cards) + '\n')
o.close()
loadDic()
print('Dic len: ' + str(len(dic)))
loadResults('output.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.