我有一个句子
str = 'cold weather gives me cold'
以及一个列表
tag = ['O','O','O','O','disease']
这表示句子中的第五个单词是一种疾病类型。现在我需要获取第五个单词的起始和结束位置。
如果我只搜索“cold”字符串,它将给出首次出现“cold”的起始位置。
首先从标签中找到疾病指数,然后从数据中找到疾病名称,最后找到开始和结束索引:
strData = 'cold weather gives me cold'
tag = ['O','O','O','O','disease']
diseaseIndex = tag.index('disease')
diseaseName = strData.split()[diseaseIndex]
print(diseaseName)
diseaseNameStartIndex = sum(len(word) for (index, word) in enumerate(strData.split()) if index< diseaseIndex ) + diseaseIndex
diseaseNameEndIndex = diseaseNameStartIndex + len(diseaseName) -1
print("diseaseNameStartIndex = ",diseaseNameStartIndex)
print("diseaseNameEndIndex = ",diseaseNameEndIndex)
输出:
cold
diseaseNameStartIndex = 22
diseaseNameEndIndex = 25
def get(str,target_index):
start = len(" ".join(str.split(" ")[:target_index])) + 1
end = start + len(str.replace('.','').split(' ')[target_index])
return (start,end)
str = 'cold weather gives me cold.'
tag = ['O','O','O','O','disease']
start,end = get(str,tag.index('disease'))
print(start,end,str[start:end]) # outputs 22 26 cold
str = 'cold weather gives me cold'
tag = ['O','O','O','O','disease']
start,end = get(str,tag.index('disease'))
print(start,end,str[start:end]) # outputs 22 26 cold
str = 'cold weather gives me cold and cough'
tag = ['O','O','O','O','disease']
start,end = get(str,tag.index('disease'))
print(start,end,str[start:end]) # outputs 22 26 cold
希望它能帮到你!
str = 'cold weather gives me cold'
word_idx = 4 # index of the word we are looking for
split_str = str.split(' ')
print(split_str[word_idx]) # outputs 'cold'
start_pos = 0
for i in range(word_idx):
start_pos += len(split_str[i]) + 1 # add one because of the spaces between words
end_pos = start_pos + len(split_str[word_idx]) - 1
print(start_pos) # prints 22
print(end_pos) # prints 25
string_list = string.split(" ")
word_start = len(" ".join(string_list[:4])) + 1
word_end = word_start + len(string_list[4])
使用 itertools
和 re
:
import re
from itertools import accumulate
def find_index(string, n):
words = string.split()
len_word = len(words[n])
end_index = list(accumulate(map(len, re.split('(\s)' , string))))[::2][n]
return end_index - len_word, end_index - 1
使用它:
find_index('cold weather gives me cold', 4) #5th word means 4 in indexing
输出:
(22, 25)
def find_index(s, n):
length = len(s.split()[n])
index = [(0, len(s.split()[0]) - 1)]
for i in s.split():
index.append((index[-1][0] + len(i), index[-1][1] + len(i)))
return index[n + 1]
print(find_index('cold weather gives me cold', 4))
输出:
(22, 25)
迭代器
,该迭代器将使用re.finditer
方法生成单词的起始和结束位置,然后使用islice
找到迭代器的第n个元素。>>> str = 'cold weather gives me cold'
>>> word_pos = iter((match.group(), match.span(1)) for match in re.finditer(r'(\S+)\S', string))
>>>
>>> n=4
>>> next(islice(word_pos, n, n+1))
('cold', (22, 25))
re
:import re
s = 'cold weather gives me cold'
new_s = re.findall('\w+|\s+', s)
l = [(a, sum(map(len, new_s[:i]))) for i, a in enumerate(new_s) if a != ' ']
tag = ['O','O','O','O','disease']
result = [[c if not c else c, c+len(d)] for a, [d, c] in zip(tag, l) if a == 'disease']
输出:
[[22, 26]]
str.split()[4]
。 - Rakesh