import pandas as pd

def analyze_csv(filepath):
   # 读取CSV文件
   df = pd.read_csv(filepath)
   
   # 1. 计算所有错误内容节选的总字数
   total_chars = df['错误内容节选'].str.len().sum()
   
   # 2. 计算未引用文本的错误内容节选字数
   no_quote_chars = df[df['是否引用文章节选（0/1）'] != 1]['错误内容节选'].str.len().sum()
   
   print(f'总字数: {total_chars}')
   print(f'未引用文本字数: {no_quote_chars}')

# 使用文件路径
filepath = '/nfs/zhanghanhaodi/HHDZ_projects/full_book_evaluate/battle_data/202501_book1.csv'
analyze_csv(filepath)

def analyze_csv(filepath):
   df = pd.read_csv(filepath)
   
   # 所有文字
   total_stats = df[['文字扣分', '词语扣分', '语法扣分', '标点扣分']].sum()
   total_all = total_stats.sum()
   print('全部文字扣分统计:')
   print(f'文字扣分: {total_stats["文字扣分"]:.2f}')
   print(f'词语扣分: {total_stats["词语扣分"]:.2f}') 
   print(f'语法扣分: {total_stats["语法扣分"]:.2f}')
   print(f'标点扣分: {total_stats["标点扣分"]:.2f}')
   print(f'总扣分: {total_all:.2f}')
   
   # 非引用文字
   no_quote_df = df[df['是否引用文章节选（0/1）'] != 1]
   no_quote_stats = no_quote_df[['文字扣分', '词语扣分', '语法扣分', '标点扣分']].sum()
   no_quote_total = no_quote_stats.sum()
   print('\n非引用文字扣分统计:') 
   print(f'文字扣分: {no_quote_stats["文字扣分"]:.2f}')
   print(f'词语扣分: {no_quote_stats["词语扣分"]:.2f}')
   print(f'语法扣分: {no_quote_stats["语法扣分"]:.2f}')
   print(f'标点扣分: {no_quote_stats["标点扣分"]:.2f}')
   print(f'总扣分: {no_quote_total:.2f}')

filepath = '/nfs/zhanghanhaodi/HHDZ_projects/full_book_evaluate/battle_data/202501_book1.csv'
analyze_csv(filepath)
# def process_csv(input_filepath, output_filepath):
#    # 读取CSV文件
#    df = pd.read_csv(input_filepath)
   
#    # 筛选未引用的内容
#    no_quote_df = df[df['是否引用文章节选（0/1）'] != 1]
   
#    # 保存到新的CSV文件
#    no_quote_df.to_csv(output_filepath, index=False)
   
#    # 打印统计信息
#    total_chars = no_quote_df['错误内容节选'].str.len().sum()
#    print(f'未引用文本总字数: {total_chars}')
#    print(f'未引用文本行数: {len(no_quote_df)}')

# input_file = '/nfs/zhanghanhaodi/HHDZ_projects/full_book_evaluate/battle_data/202501_book1.csv'
# output_file = '/nfs/zhanghanhaodi/HHDZ_projects/full_book_evaluate/battle_data/202501_book1_no_quote.csv'
# process_csv(input_file, output_file)


# def split_sentences(text):
#    """
#    将文本分割成单独的句子，处理中文引号对和换行符
#    """
#    if pd.isna(text):
#        return []
       
#    text = str(text).strip()
#    if not text:
#        return []
       
#    # 先按换行符分割
#    paragraphs = text.split('\n')
#    sentences = []
   
#    for paragraph in paragraphs:
#        current_sentence = ''
#        in_quotes = False
#        quote_stack = []
       
#        for char in paragraph.strip():
#            current_sentence += char
           
#            if char == '“':
#                in_quotes = True
#                quote_stack.append('“')
#            elif char == '”':
#                if quote_stack and quote_stack[-1] == '“':
#                    quote_stack.pop()
#                    if not quote_stack:
#                        in_quotes = False
#            elif char in '。！？' and not in_quotes:
#                sentences.append(current_sentence.strip())
#                current_sentence = ''
               
#        if current_sentence.strip():
#            sentences.append(current_sentence.strip())
           
#    return sentences



# def process_samples(file_path):
#    df = pd.read_csv(file_path, encoding='utf-8-sig')
#    new_samples = []
#    total_error_sentences = 0
#    total_correct_sentences = 0
   
#    for index, row in df.iterrows():
#        error_sentences = split_sentences(row['错误内容节选'])
#        correct_sentences = split_sentences(row['纠正后的内容【注意标点符号全半角的问题】'])
       
#        total_error_sentences += len(error_sentences) 
#        total_correct_sentences += len(correct_sentences)
       
#        # 取两个列表中较长的那个长度
#        max_len = max(len(error_sentences), len(correct_sentences))
       
#        # 如果长度不一致,用空字符串填充较短的列表
#        if len(error_sentences) < max_len:
#            error_sentences.extend([''] * (max_len - len(error_sentences)))
#        if len(correct_sentences) < max_len:
#            correct_sentences.extend([''] * (max_len - len(correct_sentences)))
           
#        for err_sent, corr_sent in zip(error_sentences, correct_sentences):
#            new_row = row.copy()  
#            new_row['错误内容节选'] = err_sent
#            new_row['纠正后的内容【注意标点符号全半角的问题】'] = corr_sent
#            new_samples.append(new_row)

#    new_df = pd.DataFrame(new_samples)
#    output_path = '202501_book1_no_quote_split.csv'
#    new_df.to_csv(output_path, index=False, encoding='utf-8-sig')
   
#    print(f"原始样本数: {len(df)}")
#    print(f"错误句子总数: {total_error_sentences}")
#    print(f"正确句子总数: {total_correct_sentences}") 
#    print(f"处理后样本数: {len(new_df)}")
   
#    return new_df

# try:
#     processed_df = process_samples('battle_data/202501_book1_no_quote.csv')
# except Exception as e:
#     print(f"处理过程中出现错误: {e}")
    
    
# import pandas as pd

# def process_csv(file_path):
#    # 读取CSV
#    df = pd.read_csv(file_path, encoding='utf-8-sig')
   
#    # 删除错误内容节选为空的行
#    df = df.dropna(subset=['错误内容节选'])
   
#    # 对比文本并更新扣分
#    mask = df['错误内容节选'] == df['纠正后的内容【注意标点符号全半角的问题】']
#    df.loc[mask, ['文字扣分', '词语扣分', '语法扣分', '标点扣分']] = 0
   
#    # 保存结果
#    output_path = 'evaluate_data.csv'
#    df.to_csv(output_path, index=False, encoding='utf-8-sig')
   
#    print(f"原始行数: {len(df)}")
#    print(f"处理后行数: {len(df)}")
#    print(f"已保存到: {output_path}")
   
#    return df

# try:
#    df = process_csv('battle_data/202501_book1_no_quote_split_recheck.csv')
# except Exception as e:
#    print(f"错误: {e}")

# import pandas as pd

# def extract_text(input_path, output_path):
#    # 读取原CSV
#    df = pd.read_csv(input_path, encoding='utf-8-sig')
   
#    # 创建新DataFrame只包含错误内容节选列并重命名
#    new_df = pd.DataFrame()
#    new_df['ori_text'] = df['错误内容节选']
   
#    # 保存结果
#    new_df.to_csv(output_path, index=False, encoding='utf-8-sig')
   
#    print(f"提取的行数: {len(new_df)}")

# try:
#    extract_text('evaluate_data.csv', 'ori_text_data.csv')
# except Exception as e:
#    print(f"错误: {e}")


import pandas as pd
import json

def merge_data(result_path, evaluate_path, output_path):
   # 读取文件
    result_df = pd.read_csv(result_path, encoding='utf-8-sig')
    evaluate_df = pd.read_csv(evaluate_path, encoding='utf-8-sig')
    
    # 解析生成结果JSON，提取字词部分
    result_df['字词结果'] = result_df['生成结果'].apply(lambda x: json.loads(x)['字词'])
    result_df['标点结果'] = result_df['生成结果'].apply(lambda x: json.loads(x)['标点'])
    result_df['语法结果'] = result_df['生成结果'].apply(lambda x: json.loads(x)['语法'])
    
    # 选择需要的列创建新DataFrame
    new_df = pd.DataFrame({
        '错误内容': evaluate_df['错误内容节选'],
        '正确内容': evaluate_df['纠正后的内容【注意标点符号全半角的问题】'],
        '字词结果': result_df['字词结果'],
        '标点结果': result_df['标点结果'],
        '语法结果': result_df['语法结果'],
        '文字扣分': evaluate_df['文字扣分'],
        '词语扣分': evaluate_df['词语扣分'],
        '语法扣分': evaluate_df['语法扣分'],
        '标点扣分': evaluate_df['标点扣分']
    })
    
    # 保存结果
    new_df.to_csv('merged_data.csv', index=False, encoding='utf-8-sig')

try:
    merge_data('/nfs/zhanghanhaodi/HHDZ_projects/full_book_evaluate/result_data/result (7).csv', 'evaluate_data.csv', 'merged_data.csv')
except Exception as e:
    print(f"错误: {e}")