问题描述:我有兴趣查看文本窗口中的术语,例如,左边3个单词,右边3个单词。基本情况具有w-3 w-2 w-1项w + 1 w + 2 w + 3的形式。我想在我的文本上实现一个滑动窗口,我可以用它来记录每个术语的上下文单词。因此,每个单词都被视为一个术语,但是当窗口移动时,它会变成上下文单词等。但是,当该术语是第一个单词时,左边没有上下文单词(t w + 1) w + 2 w + 3),当它是第二个单词时,左边只有一个上下文单词,依此类推。因此,我对实现这个灵活的滑动窗口(在Python中)的任何提示感兴趣,而无需编写和单独指定每种可能的情况。
Problem description: I'm interested in looking at terms in the text window of, say, 3 words to the left and 3 to the right. The base case has the form of w-3 w-2 w-1 term w+1 w+2 w+3. I want to implement a sliding window over my text with which I will be able to record the context words of each term. So, every word is once treated as a term, but when the window moves, it becomes a context word, etc. However, when the term is the 1st word in line, there are no context words on the left (t w+1 w+2 w+3), when it's the 2nd word in line, there's only one context word on the left, and so on. So, I am interested in any hints for implementing this flexible sliding window (in Python) without writing and specifying separately each possible situation.
回顾:
输入示例:
[w1,w2,w3, w4,w5,w6,w7,w8,w9,w10]
["w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8", "w9", "w10"]
输出 :
t1 w2 w3 w4
t1 w2 w3 w4
w1 t2 w3 w4 w5
w1 t2 w3 w4 w5
w1 w2 t3 w4 w5 w6
w1 w2 t3 w4 w5 w6
w1 w2 w3 t4 w5 w6 w7
w1 w2 w3 t4 w5 w6 w7
__ w2 w3 w4 t5 w6 w7 w8
__ w2 w3 w4 t5 w6 w7 w8
__ __等。
我目前的计划是为输出中的每一行提供一个单独的条件。
My current plan is to implement this with a separate condition for each line in the output.
推荐答案如果你想要一个 n 字的滑动窗口,请使用一个最大长度的双端队列 n 实现一个缓冲区。
If you want a sliding window of n words, use a double-ended queue with maximum length n to implement a buffer.
这应该说明这个概念:
mystr = "StackOverflow" from collections import deque window = deque(maxlen=5) for char in mystr: window.append(char) print ( ''.join(list(window)) )输出:
S St Sta Stac Stack tackO ackOv ckOve kOver Overf verfl erflo rflow更多推荐
灵活的滑动窗口(Python)
发布评论