找到句子的指数特定单词(列表中的句子)在Python

我目前有包含一个看起来像找到句子的指数特定单词(列表中的句子)在Python

example = ['Mary had a little lamb' , 

'Jack went up the hill' ,

'Jill followed suit' ,

'i woke up suddenly' ,

'it was a really bad dream...']

我想找到一句与索引列表的文件例如“醒来”一词。 在这个例子中,答案应该是f(“woke”)= 3。 F是一个功能。

我试图来标记每个句子先找到喜欢这个词的索引:

>>> from nltk.tokenize import word_tokenize 

>>> example = ['Mary had a little lamb' ,

... 'Jack went up the hill' ,

... 'Jill followed suit' ,

... 'i woke up suddenly' ,

... 'it was a really bad dream...']

>>> tokenized_sents = [word_tokenize(i) for i in example]

>>> for i in tokenized_sents:

... print i

...

['Mary', 'had', 'a', 'little', 'lamb']

['Jack', 'went', 'up', 'the', 'hill']

['Jill', 'followed', 'suit']

['i', 'woke', 'up', 'suddenly']

['it', 'was', 'a', 'really', 'bad', 'dream', '...']

但我不知道如何最终得到了字的索引以及如何将其链接到句子的索引。有人知道如何做到这一点?

回答:

可以遍历在列表中的每个字符串上,在空白处分割,然后查看您的搜索词是否在该单词列表中。如果在列表理解中这样做,则可以将索引列表返回到满足此要求的字符串。

def f(l, s): 

return [index for index, value in enumerate(l) if s in value.split()]

>>> f(example, 'woke')

[3]

>>> f(example, 'foobar')

[]

>>> f(example, 'a')

[0, 4]

如果您喜欢用nltk

def f(l, s): 

return [index for index, value in enumerate(l) if s in word_tokenize(value)]

回答:

for index, sentence in enumerate(tokenized_sents): 

if 'woke' in sentence:

return index

对于所有的句子:

return [index for index, sentence in enumerate(tokenized_sets) if 'woke' in sentence] 

回答:

如果要求用这个词,你可以使用类似的发生返回的第一句话 -

def func(strs, word): 

for idx, s in enumerate(strs):

if s.find(word) != -1:

return idx

example = ['Mary had a little lamb' ,

'Jack went up the hill' ,

'Jill followed suit' ,

'i woke up suddenly' ,

'it was a really bad dream...']

func(example,"woke")

以上是 找到句子的指数特定单词(列表中的句子)在Python 的全部内容, 来源链接: utcz.com/qa/263515.html

回到顶部