-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhearthstoneJSONParser.py
74 lines (53 loc) · 2.32 KB
/
hearthstoneJSONParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# A file to open Hearthstone cards from the command line, then export them in a file format readable to the neural network
# Andrew Thomas 3/31/2017
import json
def main(filename : str, ratingsFilename: str) -> None:
with open(filename) as fp:
cards = json.load(fp)
with open(ratingsFilename) as fp:
scores = json.load(fp)['Cards']
scores = list(map(lambda card: card['Name'].lower(), scores))
cards_sorted = sorted(cards, key=lambda card: card['name'].lower())
longest = 0
with open(filename + '_formatted_rnn.txt', 'w') as fp_2:
with open(filename + '_formatted.txt', 'w') as fp:
for card in cards:
name = card.get('name', '')
attack = card.get('attack', '')
cardClass = card.get('cardClass', '')
cost = card.get('cost', '')
health = card.get('health', '')
text = card.get('text', '').replace('\n', ' ').replace('<b>', '').replace('</b>', '').replace('<i>', '').replace('</i>', '').replace('[x]', '')
cardType = card.get('type', '')
rarity = card.get('rarity', '')
tribe = card.get('race', '')
# 0 - name
# 1 - tribe
# 2 - rarity
# 3 - cardClass
# 4 - cardType
# 5 - cost
# 6 - attack
# 7 - health
# 8 - text
entry = '|0{0}|1{1}|2{2}|3{3}|4{4}|5{5}|6{6}|7{7}|8{8}'.format(
name,
tribe,
rarity,
cardClass,
cardType,
cost,
attack,
health,
text)
fp_2.write(entry + '\n')
if card['name'].lower() in scores:
if len(entry) > longest:
longest = len(entry)
longest_entry = entry
entry = (entry + (' ' * 160))[:160] + '\n'
fp.write(entry)
print('Longest entry: {}\n{}'.format(longest, longest_entry))
if __name__=='__main__':
import sys
sys.exit(main(sys.argv[1], sys.argv[2]))