File size: 3,358 Bytes
1c25c2f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
import pandas as pd
import json
# 读取CSV文件
csv_file_path = '/mnt/afs/niuyazhe/data/lister/meme/quickmeme/table.csv'
df_csv = pd.read_csv(csv_file_path)
# 读取JSON文件
json_file_path = '/mnt/afs/niuyazhe/data/lister/meme/updated_quickmeme_label.json'
with open(json_file_path, 'r') as f:
json_data = json.load(f)
# 创建一个字典来存储JSON数据,方便后续查找
json_dict = {item['uid'].split('/')[-1]: item for item in json_data}
# 定义映射关系
sentiment_category_map = {
'happiness': '1(happiness)',
'love': '2(love)',
'anger': '3(anger)',
'sorrow': '4(sorrow)',
'fear': '5(fear)',
'hate': '6(hate)',
'surprise': '7(surprise)',
'1': '1(happiness)',
'2': '2(love)',
'3': '3(anger)',
'4': '4(sorrow)',
'5': '5(fear)',
'6': '6(hate)',
'7': '7(surprise)'
}
sentiment_degree_map = {
'slightly': '1(slightly)',
'moderately': '2(moderately)',
'very': '3(very)'
}
intention_detection_map = {
'interactive': '1(interactive)',
'expressive': '2(expressive)',
'entertaining': '3(entertaining)',
'offensive': '4(offensive)',
'other': '5(other)',
'1': '1(interactive)',
'2': '2(expressive)',
'3': '3(entertaining)',
'4': '4(offensive)',
'5': '5(other)'
}
offensiveness_detection_map = {
'non-offensive': '0(non-offensive)',
'slightly': '1(slightly)',
'moderately': '2(moderately)',
'very': '3(very)'
}
# 准备输出的数据
output_data = []
# 遍历CSV文件的每一行
for index, row in df_csv.iterrows():
file_name = f"{row['id']}.jpg"
text = row['title']
# 查找对应的JSON数据
json_item = json_dict.get(file_name)
if json_item:
# 映射各个字段
sentiment_category = sentiment_category_map.get(json_item['sentiment_category'], json_item['sentiment_category'])
sentiment_degree = sentiment_degree_map.get(json_item['sentiment_degree'], json_item['sentiment_degree'])
intention_detection = intention_detection_map.get(json_item['intention_detection'], json_item['intention_detection'])
offensiveness_detection = offensiveness_detection_map.get(json_item['offensiveness_detection'], json_item['offensiveness_detection'])
# 添加一行数据到输出列表
output_data.append([
file_name,
sentiment_category,
sentiment_degree,
intention_detection,
offensiveness_detection,
json_item['metaphor_occurrence'],
json_item['metaphor_category'],
json_item['target_domain'],
json_item['source_domain'],
json_item['target_modality'],
json_item['source_modality'],
text
])
# 将输出数据转换为DataFrame
output_df = pd.DataFrame(output_data, columns=[
'file_name',
'sentiment category',
'sentiment degree',
'intention detection',
'offensiveness detection',
'metaphor occurrence',
'metaphor category',
'target domain',
'source domain',
'target modality',
'source modality',
'text'
])
# 保存为新的CSV文件
output_csv_path = '/mnt/afs/xueyingyi/meme/generate/quickmeme/merged_output.csv'
output_df.to_csv(output_csv_path, index=False)
print(f"处理完成,结果已保存到 {output_csv_path}") |