Python如何纠正字符串中未对齐的子字符串位置信息
我有一个字符串列表以及需要用于训练 nlp 模型的子字符串的起始偏移量和结束偏移量。
其中一些子字符串的位置未对齐。例如:
text = 'Car is blue'
start_offset = 0
end_offset = 2 #misaligned. should be 3.
substring = text[start_offset:end_offset] # should be 'Car' but misaligned to give substring as 'Ca'
目的是检查突出显示的子字符串是否是整个字符串中的整个单词。如果不是,请更正开始和结束偏移。
我可以使用什么Python代码来获取整个单词子串?
I have a list of strings and the start offset and end offset of substrings that need to be used for training a nlp model.
Some of these positions for substring are misaligned. Eg:
text = 'Car is blue'
start_offset = 0
end_offset = 2 #misaligned. should be 3.
substring = text[start_offset:end_offset] # should be 'Car' but misaligned to give substring as 'Ca'
The aim is to check if substring highlighted is a whole word from the whole string. If not, correct the start and end offset.
What python code could I use to get whole word substrings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需执行
end_offset + 1
即可。字符串上的范围选择器包含第一个元素但不包含最后一个元素,因此在这种情况下不采用索引“2”上的字母“r”。如果您想要整个单词,范围应为 0:3。Just do
end_offset + 1
. Range selectors on strings are inclusive of the first element and exclusive of the last, so the letter "r" on index "2" in this case is not taken. If you want the whole word, the range should be 0:3.