5个很少被提到但能提高NLP工作效率的Python库
本篇文章将分享5个很棒但是却不被常被提及的Python库,这些库可以帮你解决各种自然语言处理(NLP)工作。
Contractions
Contractions它可以扩展常见的英语缩写和俚语。并且可以快速、高效的处理大多数边缘情况,例如缺少撇号。
例如:以前需要编写一长串正则表达式来扩展文本数据中的(即 don’t → do not;can’t → cannot;haven’t → have not)。Contractions就可以解决这个问题
pip install contractions
使用样例
import contractions s = "ive gotta go! i'll see yall later." text = contractions.fix(s, slang=True) print(text)
结果
ORIGINAL: ive gotta go! i’ll see yall later. OUTPUT: I have got to go! I will see you all later.
文本预处理的一个重要部分是创建一致性并在不失去太多意义的情况下减少单词列表。词袋模型和 TF-IDF 创建大型稀疏矩阵,其中每个变量都是语料库中一个不同的词汇词。将缩略语进行还原可以进一步降低维度,还可以有助于过滤停用词。
Distilbert-Punctuator
将丢失的标点符号的文本进行断句并添加标点符号……听起来很容易,对吧?对于计算机来说,做到这一点肯定要复杂得多。
Distilbert-punctuator 是我能找到的唯一可以执行此任务的 Python 库。而且还超级准!这是因为它使用了 BERT 的精简变体。在结合 20,000 多篇新闻文章和 4,000 份 TED Talk 抄本后,对模型进行了进一步微调,以检测句子边界。在插入句尾标点符号(例如句号)时,模型还会适当地将下一个起始字母大写。
安装
pip install distilbert-punctuator
这个库需要相当多的依赖项,如果只是想测试,可以在 Google Colab 上试用。
使用样例
from dbpunctuator.inference import Inference, InferenceArguments from dbpunctuator.utils import DEFAULT_ENGLISH_TAG_PUNCTUATOR_MAP args = InferenceArguments( model_name_or_path="Qishuai/distilbert_punctuator_en", tokenizer_name="Qishuai/distilbert_punctuator_en", tag2punctuator=DEFAULT_ENGLISH_TAG_PUNCTUATOR_MAP ) punctuator_model = Inference(inference_args=args, verbose=False) text = [ """ however when I am elected I vow to protect our American workforce unlike my opponent I have faith in our perseverance our sense of trust and our democratic principles will you support me """ ] print(punctuator_model.punctuation(text)[0])
结果
ORIGINAL: however when I am elected I vow to protect our American workforce unlike my opponent I have faith in our perseverance our sense of trust and our democratic principles will you support me OUTPUT: However, when I am elected, I vow to protect our American workforce. Unlike my opponent, I have faith in our perseverance, our sense of trust and our democratic principles. Will you support me?
如果你只是希望文本数据在语法上更加正确和易于展示。无论任务是修复凌乱的 Twitter 帖子还是聊天机器人消息,这个库都适合你。
Textstat
Textstat 是一个易于使用的轻量级库,可提供有关文本数据的各种指标,例如阅读水平、阅读时间和字数。
pip install textstat
使用样例
import textstat text = """ Love this dress! it's sooo pretty. i happened to find it in a store, and i'm glad i did bc i never would have ordered it online bc it's petite. """ # Flesch reading ease score print(textstat.flesch_reading_ease(text)) # 90-100 | Very Easy # 80-89 | Easy # 70-79 | Fairly Easy # 60-69 | Standard # 50-59 | Fairly Difficult # 30-49 | Difficult # <30 | Very Confusing # Reading time (output in seconds) # Assuming 70 milliseconds/character print(textstat.reading_time(text, ms_per_char=70))# Word count print(textstat.lexicon_count(text, removepunct=True))
结果
ORIGINAL: Love this dress! it's sooo pretty. i happened to find it in a store, and i'm glad i did bc i never would have ordered it online bc it's petite. OUTPUTS: 74.87 # reading score is considered 'Fairly Easy' 7.98 # 7.98 seconds to read 30 # 30 words
这个库还为这些指标增加了一个额外的分析层。例如,一个八卦杂志上的名人新闻文章的数据集。使用textstat,你会发现阅读速度更快更容易阅读的文章更受欢迎,留存率更高。
Gibberish-Detector
这个低代码库的主要目的是检测难以理解的单词(或胡言乱语)。它在大量英语单词上训练的模型。
pip install gibberish-detector
安装完成后还需要自己训练模型,但这非常简单,只需一分钟。训练步骤如下:
- 从这里下载名为 big.txt 的训练语料库
- 打开你的 CLI 并 cd 到 big.txt 所在的目录
- 运行以下命令:gibberish-detector train .\big.txt > gibberish-detector.model
这将在当前目录中创建一个名为 gibberish-detector.model 的文件。
使用样例
from gibberish_detector import detector # load the gibberish detection model Detector = detector.create_from_model('.\gibberish-detector.model') text1 = "xdnfklskasqd" print(Detector.is_gibberish(text1)) text2 = "apples" print(Detector.is_gibberish(text2))
结果
True # xdnfklskasqd (this is gibberish) False # apples (this is not)
它可以帮助我从数据集中删除不良观察结果。还可以实现对用户输入的错误处理。例如,如果用户在您的 Web 应用程序上输入无意义的胡言乱语文本,这时可以返回一条错误消息。
NLPAug
最好的要留到最后。
首先,什么是数据增强?它是通过添加现有数据的稍微修改的副本来扩展训练集大小的任何技术。当现有数据的多样性有限或不平衡时,通常使用数据增强。对于计算机视觉问题,增强用于通过裁剪、旋转和改变图像的亮度来创建新样本。对于数值数据,可以使用聚类技术创建合成实例。
但是如果我们正在处理文本数据呢?这就是 NLPAug 的用武之地。该库可以通过替换或插入语义关联的单词来扩充文本。通过使用像 BERT 这样的预训练语言模型来进行数据的增强,这是一种强大的方法,因为它考虑了单词的上下文。根据设置的参数,可以使用前 n 个相似词来修改文本。
预训练的词嵌入,如 Word2Vec 和 GloVe,也可用于用同义词替换词。
pip install nlpaug
使用样例
import nlpaug.augmenter.word as naw # main parameters to adjust ACTION = 'substitute' # or use 'insert' TOP_K = 15 # randomly draw from top 15 suggested words AUG_P = 0.40 # augment 40% of words within text aug_bert = naw.ContextualWordEmbsAug( model_path='bert-base-uncased', action=ACTION, top_k=TOP_K, aug_p=AUG_P ) text = """ Come into town with me today to buy food! """ augmented_text = aug_bert.augment(text, n=3) # n: num. of outputs print(augmented_text)
结果
ORIGINAL: Come into town with me today to buy food! OUTPUTS: • drove into denver with me today to purchase groceries! • head off town with dad today to buy coffee! • come up shop with mom today to buy lunch!
假设你正在使用一个具有 15k 条正面评论和仅 4k 条负面评论的数据集上训练监督分类模型。严重不平衡的数据集会在训练期间产生对多数类(正面评价)的模型偏差。
简单地复制少数类的示例(负面评论)不会向模型添加任何新信息。相反,利用 NLPAug 的高级文本增强功能来增加多样性的少数类。该技术已被证明可以提高 AUC 和 F1-Score。
结论
作为数据科学家、Kaggle 参与者或一般程序员,重要的是我们需要找到更多的工具来简化我们的工作流程。这样可以利用这些库来解决问题,增强我们的数据集,并花更多时间思考解决方案而不是编写代码。
作者:Michael Markin
- 天才制造者:独行侠、科技巨头和AI|深度学习崛起十年
- Go内存管理一文足矣
- React如何原生实现防抖?
- 分布式日志存储架构设计方案
- Chrome插件:云音乐听歌识曲
- 全场景AI推理引擎MindSpore Lite, 助力HMS Core视频编辑服务打造更智能的剪辑体验
- 页面搭建系统的那些事儿
- 张文骁:游戏开发的“零件人”梦碎之后|OneFlow U
- App 出海 —— Google 结算系统面面观
- Curve 基于 Raft 的写时延优化
- Pandas 中最常用的 7 个时间戳处理函数
- 实现Nest中参数的联合类型校验
- JDK内置锁深入探究
- Docker 实战教程之从入门到提高 (八)
- 腾讯三面:Cookie的SameSite了解吧,那SameParty呢?
- 实录 | MegEngine 大 Kernel 卷积工程优化实践
- 虚幻引擎 5 来了!不止 Lumen、Nanite 新技术,性能及 UI 均迎来大升级
- 前端新手快速上手APICloud App开发
- 高效使用Java构建工具|Maven篇|云效工程师指北
- 云音乐隐性关系链的探索与实践