给定列大约有 13000 个值。下面的函数的工作方式是,输入是字符串列表,并对列表中的每个单词进行 NER 标记。平均而言,列表中可能包含 300 个单词,包含 13000 个值。该函数处理当前列大约需要 1 个多小时。因此,我想要一个处理速度更快的解决方案。我在具有标准 CPU 计算的 azure ml 笔记本上运行。
功能:
def perform_ner_batch(texts):
if not texts: # Check if texts is empty
return []
# Perform NER on the provided texts
list_entity = []
for i in texts:
ner_result = ner_pipeline(i)
if ner_result == []:
list_entity.append('O')
for results in ner_result:
list_entity.append(results['entity_group'])
return list_entity
调用函数:
df['entities'] = df['Tokenized_Abstract_list'].apply(lambda x: perform_ner_batch(x))
也许使用地图直接使用 ner_pipeline 可能会有所帮助,当然我不知道 ner_pipline 函数是如何工作的,这只是假设尝试这个代码
df['entities'] = df['Tokenized_Abstract_list'].apply(lambda x: list(map(lambda y: ner_pipeline(y if y else []), x)))