gpt4 book ai didi

python - 在 2 个 html 标签之间添加文本

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:46 25 4
gpt4 key购买 nike

我是一名 2 年学生,正在研究文本挖掘。

一般来说,让我告诉您有关代码的信息,它首先接受 pdf 类型的文本并将其转换为 doc.txt 文件,然后我处理该数据数百行,然后在存储所有内容之后该文本中的句子添加到名为 all_text 的列表中(以供将来使用),我还选择一些文本并将它们存储到名为 summary 的列表中。

最后问题出在这部分:

摘要列表如下所示

summary=['Artificial Intelligence (AI) is a science and a set of computational technologies that are inspired by—but typically operate quite differently from—the ways people use their nervous systems and bodies to sense, learn, reason, and take action.','In reality, AI is already changing our daily lives, almost entirely in ways that improve human health, safety,and productivity.','AI is also changing how people interact with technology.']

我想要的是从doc.txt逐句读取,如果该句子在摘要列表中,则修改该句子,将其放入摘要中的所有内容的粗体标记“句子”中这里列出的是我为该特定部分尝试过的小代码,它对完整没有帮助,但在这里它是

while i < len(lis):
if lis[i] in txt:
txt = txt.replace(lis[i], "<b>" + lis[i] + "</b>")

print(lis[i])

i += 1

这段代码没有像我预期的那样工作,我的意思是它适用于一些短句子,但它不适用于像这样的句子,我不知道为什么它不起作用,请帮助我?

最佳答案

为此目的,您可以使用列表理解,例如:

summary = ['sentenceE','sentenceA']
text = ['sentenceA','sentenceB','sentenceC','sentenceD','sentenceE']
output = ['<b>'+i+'</b>' if (i in summary) else i for i in text]
print(output) #prints ['<b>sentenceA</b>', 'sentenceB', 'sentenceC', 'sentenceD', '<b>sentenceE</b>']

请注意,summarytext 应该是 strlist

关于python - 在 2 个 html 标签之间添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389157/

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