python-3.x - 在 Spacy 中查找开始和结束字符索引
问题描述
我正在 Spacy 中训练自定义模型以提取自定义实体,但是虽然我需要提供包含我的实体以及索引位置的输入训练数据,但我想了解是否有更快的方法来为关键字分配索引值我我在我的训练数据中寻找一个特定的句子。
我的培训数据示例:
TRAIN_DATA = [
('Behaviour Skills include Communication, Conflict Resolution, Work Life Balance,
{'entities': [(25, 37, 'BS'),(40, ,60, 'BS'),(62, 79, 'BS')]
})
]
现在要传递我的训练数据中特定关键字的索引位置,我目前正在手动计算它以给出我的关键字的位置。
例如:在我说行为技能包括沟通等的第一行的情况下,我正在手动计算“沟通”一词的索引位置,即 25,37。
我确信必须有另一种方法来通过其他一些方法来识别这些索引的位置,而不是手动计算它。任何想法我怎样才能做到这一点?
解决方案
使用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()
以防万一您可能需要它。
推荐阅读
- bitbucket - Bitbucket REST Api:如何通过调用 rest api 按日期 desc 对所有分支进行排序?
- typescript - 为什么我无法解构可选参数,即使提供了有效的默认值?
- django - Django 2.0 - 让测试工作者在非应用文件夹中运行测试
- javascript - 为复杂表构建排序器和过滤器
- java - 在新线程中打印的 Java 问题
- android - 为了解决“google-services.json 丢失”问题,我应该在 Flutter 应用程序中的什么位置放置 Googleservices.json
- rust - 如何通过宏将字符串转换为原始字符串?
- java - 在导入 chemaxon jar 时在线程“main”java.lang.NoClassDefFoundError: com/google/common/base/Function 中出现异常
- android - 在具有相同 MediaSession 的两个 MediaController 之间进行通信
- matlab - 在 Matlab 中查找固定间隔上的两个变量函数的最小值和最大值,并将这些点与函数绘制在同一张图中