import pandas as pd
import jieba
from nltk.translate.meteor_score import meteor_score
def preprocess_text(text):
words = jieba.lcut(text)
return words
def calculate_meteor(reference, candidate):
reference = preprocess_text(reference)
candidate = preprocess_text(candidate)
print("-------------------")
print(reference)
print("-------------------")
print(candidate)
print("-------------------")
score = meteor_score([reference], candidate)
return score
def main():
file_path = '路径'
df = pd.read_excel(file_path)
print("Columns in the dataframe:", df.columns)
references = df['参考答案']
candidate_columns = ['one', 'two', 'three', 'four', 'five']
for candidate in candidate_columns:
if candidate not in df.columns:
print(f"Column {candidate} not found in the dataframe.")
continue
meteor_scores = []
for ref, cand in zip(references, df[candidate]):
if pd.isna(ref) or pd.isna(cand):
meteor_scores.append(0)
continue
meteor_score_value = calculate_meteor(ref, cand)
meteor_scores.append(meteor_score_value)
df[f'METEOR_{candidate}'] = meteor_scores
output_path = '路径'
df.to_excel(output_path, index=False)
print("Saved METEOR scores to:", output_path)
if __name__ == "__main__":
main()