根据标签列表中单词的索引位置,在字符串中查找该单词的起始和结束位置。

5

我有一个句子

str = 'cold weather gives me cold' 

以及一个列表

 tag = ['O','O','O','O','disease'] 

这表示句子中的第五个单词是一种疾病类型。现在我需要获取第五个单词的起始和结束位置。

如果我只搜索“cold”字符串,它将给出首次出现“cold”的起始位置。


所以你基本上需要字符串的第五个单词?还是你需要它的索引? - dumbPotato21
你想要 COLD 的第一个和最后一个单词吗? - PySaad
我只需要最后一个COLD,即字符串索引中的第5个单词。 - Jithin P James
看起来你只需要 str.split()[4] - Rakesh
顺便提一下,好的,这只是一个例子,但是“str”变量名称会隐藏相应的内置函数。 - JL Peyret
显示剩余2条评论
8个回答

1

首先从标签中找到疾病指数,然后从数据中找到疾病名称,最后找到开始和结束索引:

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

1
这应该可以做到。
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

在此查看 的实际应用。

希望它能帮到你!


0
以下代码将输出给定单词的起始和结束位置,假设单词之间以空格分隔:
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

0
你可以简单地分割字符串,然后再将它们合并,但这有点笨拙。
string_list = string.split(" ")
word_start = len(" ".join(string_list[:4])) + 1
word_end = word_start + len(string_list[4])

0

使用 itertoolsre

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)

0
尝试使用这个函数:
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)

0
如果您需要处理较长的行,则最好使用迭代器,该迭代器将使用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))

0
你可以在列表推导式中使用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]]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接