gpt4 book ai didi

python - beautifulsoup 提取句子,如果它包含关键字

转载 作者:行者123 更新时间:2023-11-27 23:16:40 25 4
gpt4 key购买 nike

我想处理一个 html 网站(例如这个:http://www.uni-bremen.de/mscmarbiol/)并保存每个句子,其中包含一个字符串“research”。

这只是我从网站上提取所有文本的代码示例。

from bs4 import BeautifulSoup
from zipfile import ZipFile
import os
html_page = "example.html" #i saved this page as example locally

data = []
with open(html_page, "r") as html:
soup = BeautifulSoup(html, "lxml")
text_group = soup.get_text()

print text_group

执行仅导出包含单词“research”的句子的任务的最佳方法是什么?

有没有比对字符串使用 .split 和分隔符更优雅的方法?“re”能做点什么吗?

非常感谢您的帮助,因为我对这个主题还很陌生。

最好的问候,

特尔戈维克

最佳答案

考虑到文档中没有严格定义“句子”,听起来您需要使用将明文拆分为句子的工具。

NLTK包非常适合这种事情。你会想做类似的事情

import nltk
sentences = nltk.sent_tokenize(text)
result = [sentence for sentence in sentences if "research" in sentence]

它并不完美(例如,它不理解您文档中的“The M.Sc.”不是一个单独的句子),但是句子分割是一项看似复杂的任务,这与您将得到的一样好.

关于python - beautifulsoup 提取句子,如果它包含关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42763924/

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