社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

【NLP】一行Python代码中自动化文本处理

机器学习初学者 • 5 年前 • 511 次点击  
作者|Satyam Kumar

编译|VK
来源|Towards Data Science


自然语言处理(NLP)是人工智能的一个子领域,涉及计算机与自然语言之间的交互。它围绕着如何训练一个能够理解和实现自然语言任务使用的数据科学模型展开。

典型的NLP项目遵循管道的各个方面来训练模型。管道中的各个步骤包括文本清理、标记化、词根化、编码为数字向量等,然后是模型训练。

NLP任务的数据集是文本数据,主要来自互联网。大多数情况下,用于NLP建模的文本数据是脏的,需要在数据处理的早期阶段进行清理。数据科学家把大部分时间花在数据预处理上,包括清理文本数据。

在本文中,我们将讨论一个有趣的CleanText库,它简化了清理文本数据的过程,并加快了数据预处理流程。


什么是CleanText


CleanText是一个开放源码的Python库,它可以清除从web或社交媒体中爬取的文本数据。CleanText使开发人员能够创建规范化的文本表示。CleanText使用ftfy、unidecode和各种其他硬编码规则(包括RegEx)将损坏或脏的输入文本转换为干净文本,可以进一步处理这些文本来训练NLP模型。

安装:

可以使用以下命令从PyPl安装CleanText库:

pip install clean-text

安装后,可以使用以下方法导入库:

from cleantext import clean



用法:


CleanText库只提供了一个函数“Clean”,它接受各种参数,这些参数可以进行调优以执行文本清理。clean可执行11种类型的清理,包括:

Unicode:

s1 = 'Zürich'
clean(s1, fix_unicode=True)

# Output: zurich

ASCII:

它将文本转换为最接近的ASCII表示形式。

s2 = "ko\u017eu\u0161\u010dek"
clean(s2, to_ascii=True)

# Output: kozuscek

Lower:

将文本数据转换为小写。




    
s3 = "My Name is SATYAM"
clean(s3, lower=True)

# Output: my name is satyam

替换URL/电子邮件/电话号码:

用特殊标记替换文本数据中的所有URL、电子邮件或电话号码。

s4 = "https://www.Google.com and https://www.Bing.com are popular seach engines. You can mail me at satkr7@gmail.com. If not replied call me at 9876543210"

clean(s4, no_urls=True, replace_with_url="URL",
no_emails=True, replace_with_email="EMAIL"
no_phone_numbers=True, replace_with_email="PHONE")

# Output: url and url are popular search engines. You can mail me at EMAIL. If not replied call me at PHONE

替换货币:

用特殊标记替换文本数据中的所有货币。

s5 = "I want ₹ 40"
clean(s5, no_currency_symbols = True)
clean(s5, no_currency_symbols = True, replace_with_currency_symbol="Rupees")

# Output: i want  40
# Output: i want rupees 40

删除号码:

用特殊标记替换或删除所有数字。

s7 = 'abc123def456ghi789zero0'

clean(s7, no_digits = True)
clean(s7, no_digits = True, replace_with_digit="")

# Output: abc000def000ghi000zero0
# Output: abcdefghizero

替换标点符号:

删除或用特殊标记替换文本数据中的所有标点。

s6 = "40,000 is greater than 30,000."
clean(s6, no_punct = True)

# Output: 40000 is greater than 30000

组合所有参数:

我们已经分别讨论了上述所有参数。现在,让我们在Clean函数中组合所有这些函数,为示例文本调用它,并观察干净的文本结果。

from cleantext import clean

text = """
Zürich has a famous website https://www.zuerich.com/ 
WHICH ACCEPTS 40,000 € and adding a random string, :
abc123def456ghi789zero0 for this demo. Also remove punctions ,. 
my phone number is 9876543210 and mail me at satkr7@gmail.com.' 
     """


clean_text = clean(s8, 
      fix_unicode=True, 
      to_ascii=True, 
      lower=True, 
      no_line_breaks=True,
      no_urls=True, 
      no_numbers=True, 
      no_digits=True, 
      no_currency_symbols=True, 
      no_punct=True, 
      replace_with_punct="", 
      replace_with_url="", 
      replace_with_number="", 
      replace_with_digit="", 
      replace_with_currency_symbol="",
      lang='en')

print(clean_text)

# Output: zurich has a famous website  which accepts   and adding a random string abcdefghizero for this demo also remove punctions my phone number is  and mail me at satkrgmailcom

因此,只需编写一行Python代码,就可以清除脏的文本数据并进行进一步的预处理。


结论


CleanText是一个高效的库,它可以处理或清除爬取的脏数据,只需一行代码就可以获得标准化的干净文本输出。开发人员只需要根据自己的需要调整参数。它简化了数据科学家的工作,因为现在他/她不必写很多行复杂的正则表达式代码来清理文本。

CleanText不仅适用于英语输入文本,而且可以处理德语,只需设置lang='de'。

CleanText库只包含一些文本清理参数,还有改进的余地。尽管如此,开发人员仍然可以将其用于一些清理任务,然后继续手动编码以完成剩余的任务。

阅读下面提到的文章了解AutoNLP-一个自动NLP库。

https://medium.com/swlh/autonlp-sentiment-analysis-in-5-lines-of-python-code-7b2cd2c1e8ab

参考文献:

[1] Clean-Text Repository: https://github.com/jfilter/clean-text

谢谢阅读!

往期精彩回顾




本站qq群851320808,加入微信群请扫码:
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/119255