我正在Spacy中训练一个自定义模型来提取自定义实体,但当我需要提供由实体和索引位置组成的输入训练数据时,我想了解是否有更快的方法来为我在训练数据中的特定句子中查找的关键字分配索引值。
我的训练数据示例:
TRAIN_DATA = [
('Behaviour Skills include Communication, Conflict Resolution, Work Life Balance,
{'entities': [(25, 37, 'BS'),(40, ,60, 'BS'),(62, 79, 'BS')]
})
]现在,为了在我的训练数据中传递特定关键字的索引位置,我目前正在手动计算它,以给出我的关键字的位置。
例如:在第一行中,我说的行为技能包括沟通等,我手动计算“沟通”一词的索引位置是25,37。
我相信一定有其他方法可以找出这些指数的位置,而不是以人手计算。你有什么想法吗?我怎么才能做到这一点呢?
发布于 2021-02-19 13:17:16
使用str.find()在这里可以帮上忙。但是,您必须遍历句子和关键字
keywords = ['Communication', 'Conflict Resolution', 'Work Life Balance']
texts = ['Behaviour Skills include Communication, Conflict Resolution, Work Life Balance',
'Some sentence where lower case conflict resolution is included']
LABEL = 'BS'
TRAIN_DATA = []
for text in texts:
entities = []
t_low = text.lower()
for keyword in keywords:
k_low = keyword.lower()
begin = t_low.find(k_low) # index if substring found and -1 otherwise
if begin != -1:
end = begin + len(keyword)
entities.append((begin, end, LABEL))
TRAIN_DATA.append((text, {'entities': entities}))输出:
[('Behaviour Skills include Communication, Conflict Resolution, Work Life Balance',
{'entities': [(25, 38, 'BS'), (40, 59, 'BS'), (61, 78, 'BS')]}),
('Some sentence where lower case conflict resolution is included',
{'entities': [(31, 50, 'BS')]})]我添加了str.lower()以防你需要它。
https://stackoverflow.com/questions/66260282
复制相似问题