import pandas as pd
from openpyxl import load_workbook

# 文件路径
file1 = "/root/.openclaw/media/inbound/é_¾åº_æ_æ_å_è_ç³_æ_1.0---8b762144-a4a3-481d-bdb8-b3b0dcbf875a.xlsx"
file2 = "/root/.openclaw/media/inbound/â_¼ï_LV1-å_ç_å_è_åº_-ç¼_å_é_è_ç_è_é---286e16db-d460-460d-95a4-242f28a0429c.xlsx"
output_file = "/root/.openclaw/workspace-xiaoban/单词上下分类结果.xlsx"

# 读取第一个表格（带详细字段的单词表）
df1 = pd.read_excel(file1)
# 读取第二个表格（LV1词汇表）
df2 = pd.read_excel(file2)

# 给第二份表格添加上下分类
def get_category(unit):
    if pd.isna(unit) or unit == '不常见':
        return '其他'
    unit = unit.strip()
    if unit.startswith('S0-'):
        return '上'
    if unit.startswith('S1-U'):
        # 提取单元号
        unit_num = int(unit.split('-')[1][1:])
        if unit_num <= 6:
            return '上'
        else:
            return '下'
    return '其他'

df2['分类'] = df2['占用情况'].apply(get_category)

# 创建单词到分类的映射
word_category_map = df2.drop_duplicates('单词').set_index('单词')['分类'].to_dict()

# 给第一份表格添加分类列
df1['分类'] = df1['单词'].map(word_category_map)

# 拆分分类
df_upper = df1[df1['分类'] == '上'].drop(columns=['分类'])
df_lower = df1[df1['分类'] == '下'].drop(columns=['分类'])
df_other = df1[df1['分类'] == '其他'].drop(columns=['分类'])

# 写入结果到Excel，分三个sheet
with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
    df_upper.to_excel(writer, sheet_name='上册单词', index=False)
    df_lower.to_excel(writer, sheet_name='下册单词', index=False)
    if len(df_other) > 0:
        df_other.to_excel(writer, sheet_name='其他分类单词', index=False)

print(f"处理完成！结果已保存到：{output_file}")
print(f"上册单词数量：{len(df_upper)}")
print(f"下册单词数量：{len(df_lower)}")
print(f"其他分类单词数量：{len(df_other)}")
