在 2 html 标签之间添加文本
Adding text between 2 html tags
我是一名 2 年级的学生,我正在从事文本挖掘工作。
一般来说,让我告诉你代码,它首先接受 pdf 类型的文本并将其转换为 doc.txt 文件,然后我处理该数据几个百行然后我将该文本中的所有句子存储到名为 all_text 的列表(供将来使用)并且我 select 一些文本并将它们存储在到名为 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>']
请注意 summary
和 text
应该是 list
的 str
。
我是一名 2 年级的学生,我正在从事文本挖掘工作。
一般来说,让我告诉你代码,它首先接受 pdf 类型的文本并将其转换为 doc.txt 文件,然后我处理该数据几个百行然后我将该文本中的所有句子存储到名为 all_text 的列表(供将来使用)并且我 select 一些文本并将它们存储在到名为 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>']
请注意 summary
和 text
应该是 list
的 str
。