gpt4 book ai didi

python - 搜索另一个文档中包含字符串的所有句子

转载 作者:行者123 更新时间:2023-11-30 23:32:52 25 4
gpt4 key购买 nike

我有一个包含 200 个单词的文件,每个单词占一个新行。我想在另一个文件中搜索所有这些单词。我希望打印包含这些单词之一的每个句子。现在,仅显示第一个单词的匹配项。之后,它就停止了。

corpus = open('C:\\Users\\Lucas\\Desktop\\HAIT\\Scriptie\\Tweet-corpus\\Corpus.txt', 'r', encoding='utf8')

with open('C:\\Users\\Lucas\\Desktop\\HAIT\\Scriptie\\Tweet-corpus\\MostCommon3.txt', 'r', encoding='utf8') as list:
for line in list:
for a in corpus:
if line in a:
print(a)

最佳答案

# Prepare the list of words
word_file = open('wordfile', 'r', encoding='utf8')
words = [word.strip() for word in word_file.readlines()]
word_file.close()

# Now examine each sentence:
with open('sentencefile') as sentences:
for sentence in sentences:
found = False
for word in words:
if word in sentence:
found = True
break
if found:
print sentence

关于python - 搜索另一个文档中包含字符串的所有句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19185764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com