Split analyze_keywords.py into multiple scripts
This commit is contained in:
parent
32f8c20b2e
commit
5cec2e8e24
5 changed files with 88 additions and 81 deletions
19
analyze_all_set_keywords.py
Normal file
19
analyze_all_set_keywords.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
"""Find keyword count in each set.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import keyword_stats
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
f = open('set_codes.txt')
|
||||||
|
codes = [line.strip() for line in f.readlines()]
|
||||||
|
output_file = open('set_keyword_frequency.csv', 'w+')
|
||||||
|
output_file.write('Set,Keywords\n')
|
||||||
|
for code in codes:
|
||||||
|
if code == 'CON':
|
||||||
|
code = 'CON_'
|
||||||
|
filename = f'scryfall/sets/{code}.json'
|
||||||
|
k_dict = keyword_stats.keyword_count(filename)
|
||||||
|
print(f'{code}: {len(k_dict)}')
|
||||||
|
output_file.write(f'{code},{len(k_dict)}\n')
|
||||||
|
output_file.close()
|
||||||
|
f.close()
|
||||||
11
analyze_cube_keywords.py
Normal file
11
analyze_cube_keywords.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
"""Print a keyword report for a cards.json file
|
||||||
|
"""
|
||||||
|
|
||||||
|
import cube_json
|
||||||
|
import keyword_stats
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
cube_list = cube_json.load_cube_from_csv('cube_csvs/TheElegantCube_2020-11-17_5.0.4.csv', {'core'})
|
||||||
|
cube_json_filename = cube_json.create_cube_json(cube_list, 'cubes/TheElegantCube_2020-11-17_5.0.4.json')
|
||||||
|
keyword_dict = keyword_stats.keyword_count(cube_json_filename)
|
||||||
|
keyword_stats.keyword_report(keyword_dict)
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
import json
|
|
||||||
import os
|
|
||||||
|
|
||||||
import cube_json
|
|
||||||
import word_count
|
|
||||||
|
|
||||||
def get_keywords(card):
|
|
||||||
if 'keywords' in card.keys():
|
|
||||||
return card['keywords']
|
|
||||||
elif 'card_faces' in card:
|
|
||||||
keywords = []
|
|
||||||
for face in card['card_faces']:
|
|
||||||
if 'keywords' in face:
|
|
||||||
keywords += face['keywords']
|
|
||||||
return keywords
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
def keyword_count(filename):
|
|
||||||
# json file must contain a list of Scryfall card objects
|
|
||||||
f = open(filename, encoding='utf8')
|
|
||||||
d = json.load(f)
|
|
||||||
keywords = []
|
|
||||||
for card in d:
|
|
||||||
keywords += get_keywords(card)
|
|
||||||
f.close()
|
|
||||||
k_unique = set(keywords)
|
|
||||||
k_dict = {}
|
|
||||||
for keyword in k_unique:
|
|
||||||
k_dict[keyword] = keywords.count(keyword)
|
|
||||||
return k_dict
|
|
||||||
|
|
||||||
|
|
||||||
def keyword_report(k_dict):
|
|
||||||
k_lists = []
|
|
||||||
for keyword in k_dict:
|
|
||||||
k_lists += [[k_dict[keyword], keyword]]
|
|
||||||
k_lists = sorted(k_lists, reverse=True)
|
|
||||||
for n, keyword in k_lists:
|
|
||||||
print(keyword, n)
|
|
||||||
print(f'\nTotal unique keywords: {len(k_dict)}')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
### Example use: print a keyword report for a cards.json file ###
|
|
||||||
# cube_list = cube_json.load_cube_from_csv('cube_csvs/TheElegantCube_2020-11-17_5.0.4.csv', {'core'})
|
|
||||||
# cube_json_filename = cube_json.create_cube_json(cube_list, 'cubes/TheElegantCube_2020-11-17_5.0.4.json')
|
|
||||||
# keyword_dict = keyword_count(cube_json_filename)
|
|
||||||
# keyword_report(keyword_dict)
|
|
||||||
|
|
||||||
|
|
||||||
# ### Example use: count keywords in a list of cube.json files ###
|
|
||||||
# output = open('cube_keyword_frequency.csv', 'w+')
|
|
||||||
# all_cube_files = os.listdir('cubes')
|
|
||||||
# output_file = open('cube_keyword_frequency.csv', 'w+')
|
|
||||||
# output_file.write('Cube,Keywords\n')
|
|
||||||
# for filename in all_cube_files:
|
|
||||||
# if filename.endswith('json'):
|
|
||||||
# k_dict = keyword_count('cubes/' + filename)
|
|
||||||
# output_file.write(f"{filename.strip('json')},{len(k_dict)}\n")
|
|
||||||
# print(filename, len(k_dict))
|
|
||||||
# output_file.close()
|
|
||||||
|
|
||||||
|
|
||||||
# ### Example use: find keyword count in each set ###
|
|
||||||
# f = open('set_codes.txt')
|
|
||||||
# codes = [line.strip() for line in f.readlines()]
|
|
||||||
# output_file = open('set_keyword_frequency.csv', 'w+')
|
|
||||||
# output_file.write('Set,Keywords\n')
|
|
||||||
# for code in codes:
|
|
||||||
# if code == 'CON':
|
|
||||||
# code = 'CON_'
|
|
||||||
# filename = f'scryfall/sets/{code}.json'
|
|
||||||
# k_dict = keyword_count(filename)
|
|
||||||
# print(f'{code}: {len(k_dict)}')
|
|
||||||
# output_file.write(f'{code},{len(k_dict)}\n')
|
|
||||||
# output_file.close()
|
|
||||||
# f.close()
|
|
||||||
|
|
||||||
pass
|
|
||||||
20
analyze_multiple_cubes_keywords.py
Normal file
20
analyze_multiple_cubes_keywords.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
"""Count keywords in a list of cube.json files.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import keyword_stats
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
output = open('cube_keyword_frequency.csv', 'w+')
|
||||||
|
all_cube_files = os.listdir('cubes')
|
||||||
|
output_file = open('cube_keyword_frequency.csv', 'w+')
|
||||||
|
output_file.write('Cube,Keywords\n')
|
||||||
|
for filename in all_cube_files:
|
||||||
|
if filename.endswith('json'):
|
||||||
|
k_dict = keyword_stats.keyword_count('cubes/' + filename)
|
||||||
|
output_file.write(f"{filename.strip('json')},{len(k_dict)}\n")
|
||||||
|
print(filename, len(k_dict))
|
||||||
|
output_file.close()
|
||||||
|
|
||||||
38
keyword_stats.py
Normal file
38
keyword_stats.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
def get_keywords(card):
|
||||||
|
if 'keywords' in card.keys():
|
||||||
|
return card['keywords']
|
||||||
|
elif 'card_faces' in card:
|
||||||
|
keywords = []
|
||||||
|
for face in card['card_faces']:
|
||||||
|
if 'keywords' in face:
|
||||||
|
keywords += face['keywords']
|
||||||
|
return keywords
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def keyword_count(filename):
|
||||||
|
# json file must contain a list of Scryfall card objects
|
||||||
|
f = open(filename, encoding='utf8')
|
||||||
|
d = json.load(f)
|
||||||
|
keywords = []
|
||||||
|
for card in d:
|
||||||
|
keywords += get_keywords(card)
|
||||||
|
f.close()
|
||||||
|
k_unique = set(keywords)
|
||||||
|
k_dict = {}
|
||||||
|
for keyword in k_unique:
|
||||||
|
k_dict[keyword] = keywords.count(keyword)
|
||||||
|
return k_dict
|
||||||
|
|
||||||
|
|
||||||
|
def keyword_report(k_dict):
|
||||||
|
k_lists = []
|
||||||
|
for keyword in k_dict:
|
||||||
|
k_lists += [[k_dict[keyword], keyword]]
|
||||||
|
k_lists = sorted(k_lists, reverse=True)
|
||||||
|
for n, keyword in k_lists:
|
||||||
|
print(keyword, n)
|
||||||
|
print(f'\nTotal unique keywords: {len(k_dict)}')
|
||||||
Loading…
Add table
Add a link
Reference in a new issue