Implement dataset statistics
Browse files- .gitignore +3 -0
- utils/discord_parse.py +19 -2
- utils/stats.py +22 -0
.gitignore
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Byte-compiled / optimized / DLL files
|
| 2 |
__pycache__/
|
| 3 |
*.py[cod]
|
|
|
|
| 1 |
+
stats.json
|
| 2 |
+
data/
|
| 3 |
+
|
| 4 |
# Byte-compiled / optimized / DLL files
|
| 5 |
__pycache__/
|
| 6 |
*.py[cod]
|
utils/discord_parse.py
CHANGED
|
@@ -45,6 +45,8 @@ def worker_parse(filename, out_filename=None, **kwargs):
|
|
| 45 |
with open(out_filename, 'w', encoding='utf-8') as f:
|
| 46 |
f.write('⁂\n'+format_channel(metadata)+'\n⁂\n')
|
| 47 |
f.write('\n'.join(msgs))
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def worker_dl(channel_id_path, auth_token):
|
| 50 |
# channel_id_path is a json
|
|
@@ -65,6 +67,16 @@ def worker_dl(channel_id_path, auth_token):
|
|
| 65 |
for filename in glob.glob('*.json'):
|
| 66 |
os.rename(filename, f'raw/discord/{filename}')
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if __name__ == '__main__':
|
| 69 |
parser = argparse.ArgumentParser(description='Process Discord JSONs')
|
| 70 |
parser.add_argument('-f', '--file', type=str, help='file to process', required=False)
|
|
@@ -73,6 +85,7 @@ if __name__ == '__main__':
|
|
| 73 |
parser.add_argument('-o', '--out_dir', type=str, help='directory to output', required=False, default='./data/discord')
|
| 74 |
parser.add_argument('-d', '--dl', type=str, help='json file containing channel IDs to download', required=False)
|
| 75 |
parser.add_argument('-t', '--token', type=str, help='discord auth token', required=False)
|
|
|
|
| 76 |
args = parser.parse_args()
|
| 77 |
|
| 78 |
if args.dl:
|
|
@@ -82,7 +95,9 @@ if __name__ == '__main__':
|
|
| 82 |
worker_dl(args.dl, args.token)
|
| 83 |
exit()
|
| 84 |
if args.file:
|
| 85 |
-
worker_parse(args.file, anonymous=args.anonymous)
|
|
|
|
|
|
|
| 86 |
exit()
|
| 87 |
if args.in_dir and args.out_dir:
|
| 88 |
if not os.path.exists(args.out_dir):
|
|
@@ -90,7 +105,9 @@ if __name__ == '__main__':
|
|
| 90 |
files = glob.glob(args.in_dir+'/*.json')
|
| 91 |
for file in files:
|
| 92 |
try:
|
| 93 |
-
worker_parse(file, out_filename=args.out_dir+'/'+file.split('/')[-1].replace('.json', '.txt'), anonymous=args.anonymous)
|
|
|
|
|
|
|
| 94 |
except json.JSONDecodeError:
|
| 95 |
print(f'JSON Validation error in "{file}", skipping.')
|
| 96 |
continue
|
|
|
|
| 45 |
with open(out_filename, 'w', encoding='utf-8') as f:
|
| 46 |
f.write('⁂\n'+format_channel(metadata)+'\n⁂\n')
|
| 47 |
f.write('\n'.join(msgs))
|
| 48 |
+
|
| 49 |
+
return (metadata, len(msgs))
|
| 50 |
|
| 51 |
def worker_dl(channel_id_path, auth_token):
|
| 52 |
# channel_id_path is a json
|
|
|
|
| 67 |
for filename in glob.glob('*.json'):
|
| 68 |
os.rename(filename, f'raw/discord/{filename}')
|
| 69 |
|
| 70 |
+
def dump_stats(s):
|
| 71 |
+
stats = {}
|
| 72 |
+
if os.path.exists('stats.json'):
|
| 73 |
+
stats = json.load(open('stats.json', 'r', encoding='utf-8'))
|
| 74 |
+
else:
|
| 75 |
+
stats = {'discord': {}}
|
| 76 |
+
stats['discord'][s[0]['guild']+' - '+s[0]['name']] = s[1]
|
| 77 |
+
with open('stats.json', 'w', encoding='utf-8') as f:
|
| 78 |
+
json.dump(stats, f)
|
| 79 |
+
|
| 80 |
if __name__ == '__main__':
|
| 81 |
parser = argparse.ArgumentParser(description='Process Discord JSONs')
|
| 82 |
parser.add_argument('-f', '--file', type=str, help='file to process', required=False)
|
|
|
|
| 85 |
parser.add_argument('-o', '--out_dir', type=str, help='directory to output', required=False, default='./data/discord')
|
| 86 |
parser.add_argument('-d', '--dl', type=str, help='json file containing channel IDs to download', required=False)
|
| 87 |
parser.add_argument('-t', '--token', type=str, help='discord auth token', required=False)
|
| 88 |
+
parser.add_argument('-s', '--stats', action='store_true', help='write to stats')
|
| 89 |
args = parser.parse_args()
|
| 90 |
|
| 91 |
if args.dl:
|
|
|
|
| 95 |
worker_dl(args.dl, args.token)
|
| 96 |
exit()
|
| 97 |
if args.file:
|
| 98 |
+
s = worker_parse(args.file, anonymous=args.anonymous)
|
| 99 |
+
if args.stats:
|
| 100 |
+
dump_stats(s)
|
| 101 |
exit()
|
| 102 |
if args.in_dir and args.out_dir:
|
| 103 |
if not os.path.exists(args.out_dir):
|
|
|
|
| 105 |
files = glob.glob(args.in_dir+'/*.json')
|
| 106 |
for file in files:
|
| 107 |
try:
|
| 108 |
+
s = worker_parse(file, out_filename=args.out_dir+'/'+file.split('/')[-1].replace('.json', '.txt'), anonymous=args.anonymous)
|
| 109 |
+
if args.stats:
|
| 110 |
+
dump_stats(s)
|
| 111 |
except json.JSONDecodeError:
|
| 112 |
print(f'JSON Validation error in "{file}", skipping.')
|
| 113 |
continue
|
utils/stats.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
if __name__ == '__main__':
|
| 5 |
+
# load stats.json
|
| 6 |
+
stats = {}
|
| 7 |
+
if os.path.exists('stats.json'):
|
| 8 |
+
stats = json.load(open('stats.json', 'r', encoding='utf-8'))
|
| 9 |
+
else:
|
| 10 |
+
print('No stats.json found.')
|
| 11 |
+
exit(1)
|
| 12 |
+
|
| 13 |
+
print('-- Convo1 Dataset Stats --')
|
| 14 |
+
print(f'Datasets Available: {len(stats)}')
|
| 15 |
+
|
| 16 |
+
for dataset in stats.keys():
|
| 17 |
+
total = 0
|
| 18 |
+
print(f'\n-- Dataset Name: {dataset} --')
|
| 19 |
+
for guild in sorted(stats[dataset].items(), key=lambda x: x[1], reverse=True):
|
| 20 |
+
print(f'{guild[0]}: {guild[1]}')
|
| 21 |
+
total += guild[1]
|
| 22 |
+
print(f'\nTotal Messages in {dataset}: {total}\n')
|