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

标签 python html text-mining

我是一名 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/

相关文章:

Python:remove() 似乎不起作用

html - 以编程方式从 html5 中的上传中删除 1 个或多个文件?

html - 向用户显示表单的 REST 方式是什么

python - python中的搜索方法和字符串匹配

Python:线性回归中的 'for' 循环和迭代

python - Python 中是否存在用于查找满足某些条件的最佳值的内置函数?

r - 使用 klaR NaiveBayes 时出错

performance - Shingleprinting 在实践中是如何工作的?

python - vs 2017 - python 智能感知不工作

javascript - CSS3 动画图像质量规模