Python 短语标签



假设我有一个句子,比如 发送 ="安全引用了许多故意违反安全的行为,因为许多工人死亡,未能为轨道车顶部的工人提供和确保使用坠落保护。">

vio="未能为轨道车顶部的工人提供并确保使用坠落保护装置而违反安全规定">

inc="工人死亡"。

结果输出应为:

safety_NONE cited_NONE many_NONE for_NONE one_NONE willful_NONE safety_VIO violation_VIO for_VIO failing_VIO to_VIO provide_VIO and_VIO ensure_VIO the_VIO use_VIO of_VIO fall_VIO protection_VIO for_VIO workers_VIO atop_VIO railcars_VIO because_NONE many_NONE workers_INC died_INC ._INC

请让我知道 python 脚本,它将帮助我获得此输出。

vio = re.findall(r"[w']+|[.,!?;]", vio)
inc = re.findall(r"[w']+|[.,!?;]", inc)
sent = re.findall(r"[w']+|[.,!?;]", sent)
labels = {"VIO": vio,
"INC": inc}
labelled = []
for w in sent:
label = "_NONE"
for l, criteria in labels.items():
if w in criteria:
label = "_"+l
labelled.append(w + label)
result = " ".join(labelled)

最新更新