Skip to main content
 首页 » 编程设计

Python 相当于 wordpress sanitize_text

2024年10月24日8mfrbuaa

我需要相当于 wordpress sanitize_text 的 Python

标题:

'mygubbi raises $25 mn seed funding from bigbasket co founder others' 

wordpress 给出

"mygubbi-raises-2-5-mn-seed-funding-bigbasket-co-founder-others" 

Python slugify 给出

"mygubbi-raises-2-5-mn-seed-funding-from-bigbasket-co-founder-others" 

我使用过 python-slugify Python 库。

我是否应该只删除 from、in 和 to 之类的词。我在哪里可以得到这些停用词?

请您参考如下方法:

python-slugify库有一个 stopwords 参数,它可以与 nltk 一起使用,如下所示:

from slugify import slugify 
from nltk.corpus import stopwords 
 
text = 'mygubbi raises $25 mn seed funding from bigbasket co founder others' 
print slugify(text, stopwords=stopwords.words('english')) 

这将打印:

mygubbi-raises-25-mn-seed-funding-bigbasket-co-founder-others 

安装 nltk 后,您可以安装其他语料库,其中之一是 stopwords。为此,请运行其内置的下载实用程序,如下所示:

import nltk 
 
nltk.download() 

选择 Corpora,向下滚动到 stopwords,然后单击 Download 按钮。