你好,我正在上下文中处理关键字,我已经在给定字符串之后得到了上下文,我想在给定字符串之前也打印3个字符串。
def file(data):
tokens = []
for line in data:
tokens.extend(line.strip().split())
return tokens
lists = []
def keycontext(lines, word):
liste = []
for i in range(len(lines)):
if (lines[i] == word):
lists.append(" ".join(lines[i:i+int(2)+1])) #lines[i-int(0):i]+
return lists
keyword = input("write keyword")
print("You've written ", keyword)
text = file(data['clean_text'])
result = keycontext(text,keyword)
作为列表的输出,我明白了
['faire un nouveau','faire et qui','faire avec les','faire au vrai',....
我还想在放任
之前获得字符串我如何处理此行
lists.append(" ".join(lines[i:i+int(2)+1])) #lines[i-int(0):i]+
答案 0 :(得分:1)
def file(data):
tokens = []
for line in data:
tokens.extend(line.strip().split())
return tokens
lists = []
def keycontext(lines, word):
liste = []
for i in range(len(lines)):
if (lines[i] == word):
lists.append(" ".join(lines[min(0,i-2):i+int(2)+1])) #lines[i-int(0):i]+
return lists
keyword = input("write keyword")
print("You've written ", keyword)
text = file(data['clean_text'])
result = keycontext(text,keyword)
答案 1 :(得分:0)
最后我只需将滑动int
从0更改为2即可。这就是列表的样子。
lists.append(" ".join(lines[i-int(2):i]+lines[i:i+int(2)+1]))