102 lines
4.3 KiB
Python
102 lines
4.3 KiB
Python
import json
|
||
import os
|
||
|
||
# 获取当前目录下的JSON文件路径
|
||
file_path = os.path.join(os.path.dirname(__file__), 'Alpha_candidates.json')
|
||
|
||
if not os.path.exists(file_path):
|
||
print('not find json file')
|
||
exit(1)
|
||
|
||
with open(file_path, 'r', encoding='utf-8') as file:
|
||
json_data = json.load(file)
|
||
|
||
# 打开输出文件
|
||
output_path = os.path.join(os.path.dirname(__file__), 'output.txt')
|
||
with open(output_path, 'w', encoding='utf-8') as output_file:
|
||
first_alpha = True # 用于控制空行
|
||
total_expressions = 0 # 统计生成的表达式总数
|
||
|
||
for formula_template, data in json_data.items():
|
||
# 获取占位符和候选数据
|
||
placeholder_candidates = data.get('placeholder_candidates', {})
|
||
|
||
if not placeholder_candidates:
|
||
# 如果没有占位符,直接输出原始公式
|
||
if not first_alpha:
|
||
output_file.write('\n') # 空一行
|
||
output_file.write(formula_template + '\n')
|
||
first_alpha = False
|
||
total_expressions += 1
|
||
continue
|
||
|
||
# 收集所有占位符的候选值
|
||
placeholder_values = []
|
||
placeholder_names = []
|
||
|
||
for placeholder_name, candidate_info in placeholder_candidates.items():
|
||
candidates = candidate_info.get('candidates', [])
|
||
values = [candidate['id'] for candidate in candidates]
|
||
placeholder_values.append(values)
|
||
placeholder_names.append(placeholder_name)
|
||
|
||
# 计算该公式会生成多少表达式(每个占位符候选数的乘积)
|
||
formula_count = 1
|
||
for values in placeholder_values:
|
||
formula_count *= len(values)
|
||
|
||
# 生成所有可能的组合(笛卡尔积)
|
||
def generate_combinations(values_list, current_index=0, current_combination=None):
|
||
if current_combination is None:
|
||
current_combination = {}
|
||
|
||
if current_index == len(values_list):
|
||
# 生成最终的表达式
|
||
expression = formula_template
|
||
for placeholder_name, value in current_combination.items():
|
||
expression = expression.replace(placeholder_name, value)
|
||
yield expression
|
||
else:
|
||
placeholder_name = placeholder_names[current_index]
|
||
for value in values_list[current_index]:
|
||
current_combination[placeholder_name] = value
|
||
yield from generate_combinations(values_list, current_index + 1, current_combination.copy())
|
||
|
||
# 输出所有生成的表达式
|
||
expressions_generated = 0
|
||
for expression in generate_combinations(placeholder_values):
|
||
# if not first_alpha:
|
||
# output_file.write('\n') # 空一行
|
||
output_file.write(expression + '\n')
|
||
first_alpha = False
|
||
expressions_generated += 1
|
||
|
||
# 验证生成数量是否正确
|
||
if expressions_generated != formula_count:
|
||
print(f"警告: 公式 '{formula_template}' 预期生成 {formula_count} 个表达式,实际生成 {expressions_generated} 个")
|
||
|
||
total_expressions += expressions_generated
|
||
|
||
print(f"Alpha表达式已生成到: {output_path}")
|
||
print(f"共处理了 {len(json_data)} 条公式模板")
|
||
print(f"总共生成了 {total_expressions} 个alpha表达式")
|
||
|
||
# 可选:显示每个公式的生成数量统计
|
||
print("\n详细统计:")
|
||
for formula_template, data in json_data.items():
|
||
placeholder_candidates = data.get('placeholder_candidates', {})
|
||
if not placeholder_candidates:
|
||
print(f" '{formula_template}': 1个表达式(无占位符)")
|
||
else:
|
||
placeholder_counts = []
|
||
for placeholder_name, candidate_info in placeholder_candidates.items():
|
||
candidates = candidate_info.get('candidates', [])
|
||
placeholder_counts.append(f"{placeholder_name}({len(candidates)}个候选)")
|
||
|
||
formula_count = 1
|
||
for placeholder_name, candidate_info in placeholder_candidates.items():
|
||
candidates = candidate_info.get('candidates', [])
|
||
formula_count *= len(candidates)
|
||
|
||
placeholders_str = " × ".join(placeholder_counts)
|
||
print(f" '{formula_template}': {formula_count}个表达式 ({placeholders_str})") |