验证一个单词是否存在于 Context 列表中

Verify that a word exists in a list of Context

我有一个功能,可以将一个短语分成上下文window(如何划分它的长度) 例子:这是最好的时代 这是最坏的时代 我们找到 10 个上下文 所以这是我的结果

我的代码

text = 'it was the best of times it was the worst of times '
#text1='c etait le meilleur des temps'
phrase = text.split()

def PhraseToContexts(phrase, window):     
      return [phrase[i:i+window] for i in range(len(phrase)-(window-1))]
print(PhraseToContexts(phrase, 3))

PS:我正在使用 python (spyder)

现在,我想要创建另一个名为 oneContext(listContexts,phrase,word,window) 的函数来验证这个词是否存在于上下文列表中并且 return 向量包含值 1 表示该词存在于上下文中,否则为 0。

在前面的例子中 如果我们搜索 "it" 我们得到的向量是 [1.0.0.0.0.0.1.0.0.0]

def oneContext(listContexts,phrase,word): 
    l = [1 if word in x[int(len(x)/2)] else 0 for x in listContexts]
    return l

其中,

listContexts = PhraseToContexts(phrase, window)