簡體   English   中英

在Python中查找特定單詞的句子索引(列表中的句子)

[英]Find the sentence’s index (sentences in a list) of a specific word in 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...']

我想通過示例找到單詞“ woke”的句子索引。 在此示例中,答案應為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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM