11import os
22import json
33import random
4+ import sys
45
56
67def word_to_json (file_path = "Words/words.txt" , num_normal = 10 , num_hard = 5 , bonus_normal = 1 , bonus_hard = 1 , output_path = None ):
78 if not os .path .exists (file_path ):
89 raise FileNotFoundError (f"Error: File '{ file_path } ' does not exist." )
910
11+ print (f"[debug] word_to_json reading file_path={ file_path } " , file = sys .stderr )
12+
1013 with open (file_path , 'r' ) as file :
1114 data = file .read ()
1215
16+ print (f"[debug] file size={ len (data )} chars, first 200: { repr (data [:200 ])} " , file = sys .stderr )
17+
1318 sections = data .split ('====================' )
1419 result = {}
1520
16- for section in sections :
21+ print (f"[debug] found { len (sections )} sections" , file = sys .stderr )
22+
23+ for i , section in enumerate (sections ):
1724 if not section .strip ():
25+ print (f"[debug] section { i } : empty, skipping" , file = sys .stderr )
1826 continue
1927 lines = section .strip ().splitlines ()
2028 topic = lines [0 ].strip ('>' ).strip ()
2129 words = [word .strip () for word in lines [1 :] if word .strip ()]
30+ print (f"[debug] section { i } : topic={ repr (topic )} , words_count={ len (words )} , words={ words } " , file = sys .stderr )
2231
2332 normal_pool = {w for w in words if 4 <= len (w ) <= 11 }
2433 hard_pool = {w for w in words if 6 <= len (w ) <= 15 }
2534
35+ print (f"[debug] normal_pool size={ len (normal_pool )} , hard_pool size={ len (hard_pool )} " , file = sys .stderr )
36+
2637 def _sample (pool , count , result_list ):
2738 sample = random .sample (list (pool ), count )
2839 pool .difference_update (sample )
@@ -57,12 +68,16 @@ def _sample(pool, count, result_list):
5768 hard_pool , topic_result ["Bonus" ]["Hard" ] = _sample (hard_pool , hard_w_count , topic_result ["Bonus" ]["Hard" ])
5869
5970 result [topic ] = topic_result
71+ print (f"[debug] result keys so far: { list (result .keys ())} " , file = sys .stderr )
72+
73+ print (f"[debug] FINAL result keys: { list (result .keys ())} " , file = sys .stderr )
6074
6175 if output_path is None :
6276 output_path = os .path .join (os .path .dirname (__file__ ), "Words" , "words.json" )
77+ print (f"[debug] writing result to output_path={ output_path } " , file = sys .stderr )
6378 with open (output_path , "w" ) as f :
6479 json .dump (result , f , indent = 4 )
6580
6681
6782if __name__ == '__main__' :
68- word_to_json ("Words/words.txt" , num_normal = 10 , num_hard = 5 , bonus_normal = 1 , bonus_hard = 1 )
83+ word_to_json ("Words/words.txt" , num_normal = 10 , num_hard = 5 , bonus_normal = 1 , bonus_hard = 1 )
0 commit comments