import math
from hanziconv import HanziConv
import pandas as pd
import jieba

df = pd.read_excel('../Data/心血管相似患者实验.xlsx', sheet_name='Sheet1')
text = df['answer'].tolist()
stopwords = [line.strip() for line in open('../Data/hit_stopwords.txt', 'r', encoding='utf-8').readlines()]
result = []
for line in text:
    if isinstance(line, float) and math.isnan(line):
        result.append('')
    else:
        # 精确模式，繁体转换
        seg_list = jieba.cut(HanziConv.toSimplified(line))
        # print(" ".join(seg_list))

        seg_list = [i for i in seg_list if i not in stopwords]
        result.append(' '.join(seg_list))

df_result = pd.DataFrame({'idx': df.idx, '分词': result})
df_result.to_excel('../Result/医生分词结果.xlsx', sheet_name='Sheet1', index=False)
